Skip to content

Commit 05bffc7

Browse files
committed
#3006 : Fixing for .get().unwrap().foo()
1 parent d366710 commit 05bffc7

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

clippy_lints/src/methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
14151415
),
14161416
"try this",
14171417
format!(
1418-
"{}{}[{}]",
1418+
"({}{}[{}])",
14191419
borrow_str,
14201420
snippet(cx, get_args[0].span, "_"),
14211421
snippet(cx, get_args[1].span, "_")

tests/ui/get_unwrap.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ fn main() {
4343
*some_btreemap.get_mut(&1).unwrap() = 'b';
4444
*false_positive.get_mut(0).unwrap() = 1;
4545
}
46+
47+
{ // Test `get().unwrap().foo()` and `get_mut().unwrap().bar()`
48+
let _ = some_vec.get(0..1).unwrap().to_vec();
49+
let _ = some_vec.get_mut(0..1).unwrap().to_vec();
50+
}
4651
}

tests/ui/get_unwrap.stderr

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,75 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co
22
--> $DIR/get_unwrap.rs:27:17
33
|
44
27 | let _ = boxed_slice.get(1).unwrap();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&boxed_slice[1])`
66
|
77
= note: `-D clippy::get-unwrap` implied by `-D warnings`
88

99
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
1010
--> $DIR/get_unwrap.rs:28:17
1111
|
1212
28 | let _ = some_slice.get(0).unwrap();
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_slice[0])`
1414

1515
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
1616
--> $DIR/get_unwrap.rs:29:17
1717
|
1818
29 | let _ = some_vec.get(0).unwrap();
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_vec[0])`
2020

2121
error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
2222
--> $DIR/get_unwrap.rs:30:17
2323
|
2424
30 | let _ = some_vecdeque.get(0).unwrap();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_vecdeque[0])`
2626

2727
error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
2828
--> $DIR/get_unwrap.rs:31:17
2929
|
3030
31 | let _ = some_hashmap.get(&1).unwrap();
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_hashmap[&1])`
3232

3333
error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
3434
--> $DIR/get_unwrap.rs:32:17
3535
|
3636
32 | let _ = some_btreemap.get(&1).unwrap();
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]`
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_btreemap[&1])`
3838

3939
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
4040
--> $DIR/get_unwrap.rs:37:10
4141
|
4242
37 | *boxed_slice.get_mut(0).unwrap() = 1;
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut boxed_slice[0]`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut boxed_slice[0])`
4444

4545
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
4646
--> $DIR/get_unwrap.rs:38:10
4747
|
4848
38 | *some_slice.get_mut(0).unwrap() = 1;
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_slice[0]`
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut some_slice[0])`
5050

5151
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
5252
--> $DIR/get_unwrap.rs:39:10
5353
|
5454
39 | *some_vec.get_mut(0).unwrap() = 1;
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vec[0]`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut some_vec[0])`
5656

5757
error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
5858
--> $DIR/get_unwrap.rs:40:10
5959
|
6060
40 | *some_vecdeque.get_mut(0).unwrap() = 1;
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vecdeque[0]`
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut some_vecdeque[0])`
6262

63-
error: aborting due to 10 previous errors
63+
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
64+
--> $DIR/get_unwrap.rs:48:17
65+
|
66+
48 | let _ = some_vec.get(0..1).unwrap().to_vec();
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_vec[0..1])`
68+
69+
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
70+
--> $DIR/get_unwrap.rs:49:17
71+
|
72+
49 | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut some_vec[0..1])`
74+
75+
error: aborting due to 12 previous errors
6476

0 commit comments

Comments
 (0)