summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/get_unwrap.txt
blob: 8defc2224416957864a017e09222554cdf5d1fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
### What it does
Checks for use of `.get().unwrap()` (or
`.get_mut().unwrap`) on a standard library type which implements `Index`

### Why is this bad?
Using the Index trait (`[]`) is more clear and more
concise.

### Known problems
Not a replacement for error handling: Using either
`.unwrap()` or the Index trait (`[]`) carries the risk of causing a `panic`
if the value being accessed is `None`. If the use of `.get().unwrap()` is a
temporary placeholder for dealing with the `Option` type, then this does
not mitigate the need for error handling. If there is a chance that `.get()`
will be `None` in your program, then it is advisable that the `None` case
is handled in a future refactor instead of using `.unwrap()` or the Index
trait.

### Example
```
let mut some_vec = vec![0, 1, 2, 3];
let last = some_vec.get(3).unwrap();
*some_vec.get_mut(0).unwrap() = 1;
```
The correct use would be:
```
let mut some_vec = vec![0, 1, 2, 3];
let last = some_vec[3];
some_vec[0] = 1;
```