]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/get_unwrap.txt
Auto merge of #102529 - colinba:master, r=joshtriplett
[rust.git] / src / tools / clippy / src / docs / get_unwrap.txt
1 ### What it does
2 Checks for use of `.get().unwrap()` (or
3 `.get_mut().unwrap`) on a standard library type which implements `Index`
4
5 ### Why is this bad?
6 Using the Index trait (`[]`) is more clear and more
7 concise.
8
9 ### Known problems
10 Not a replacement for error handling: Using either
11 `.unwrap()` or the Index trait (`[]`) carries the risk of causing a `panic`
12 if the value being accessed is `None`. If the use of `.get().unwrap()` is a
13 temporary placeholder for dealing with the `Option` type, then this does
14 not mitigate the need for error handling. If there is a chance that `.get()`
15 will be `None` in your program, then it is advisable that the `None` case
16 is handled in a future refactor instead of using `.unwrap()` or the Index
17 trait.
18
19 ### Example
20 ```
21 let mut some_vec = vec![0, 1, 2, 3];
22 let last = some_vec.get(3).unwrap();
23 *some_vec.get_mut(0).unwrap() = 1;
24 ```
25 The correct use would be:
26 ```
27 let mut some_vec = vec![0, 1, 2, 3];
28 let last = some_vec[3];
29 some_vec[0] = 1;
30 ```