Skip to content

Commit ff46c48

Browse files
committed
Full fix of get unwrap issue
1 parent 61965c3 commit ff46c48

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

clippy_lints/src/methods.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,22 +1388,33 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
13881388
// Note: we don't want to lint `get_mut().unwrap` for HashMap or BTreeMap,
13891389
// because they do not implement `IndexMut`
13901390
let expr_ty = cx.tables.expr_ty(&get_args[0]);
1391+
let get_args_str = if get_args.len() > 1 {
1392+
snippet(cx, get_args[1].span, "_")
1393+
} else {
1394+
return; // not linting on a .get().unwrap() chain or variant
1395+
};
1396+
let needs_ref;
13911397
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
1398+
needs_ref = get_args_str.parse::<usize>().is_ok();
13921399
"slice"
13931400
} else if match_type(cx, expr_ty, &paths::VEC) {
1401+
needs_ref = get_args_str.parse::<usize>().is_ok();
13941402
"Vec"
13951403
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
1404+
needs_ref = get_args_str.parse::<usize>().is_ok();
13961405
"VecDeque"
13971406
} else if !is_mut && match_type(cx, expr_ty, &paths::HASHMAP) {
1407+
needs_ref = true;
13981408
"HashMap"
13991409
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
1410+
needs_ref = true;
14001411
"BTreeMap"
14011412
} else {
14021413
return; // caller is not a type that we want to lint
14031414
};
14041415

14051416
let mut_str = if is_mut { "_mut" } else { "" };
1406-
let borrow_str = if is_mut { "&mut " } else { "&" };
1417+
let borrow_str = if !needs_ref { "" } else if is_mut { "&mut " } else { "&" };
14071418
span_lint_and_sugg(
14081419
cx,
14091420
GET_UNWRAP,
@@ -1415,10 +1426,10 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
14151426
),
14161427
"try this",
14171428
format!(
1418-
"({}{}[{}])",
1429+
"{}{}[{}]",
14191430
borrow_str,
14201431
snippet(cx, get_args[0].span, "_"),
1421-
snippet(cx, get_args[1].span, "_")
1432+
get_args_str
14221433
),
14231434
);
14241435
}

tests/ui/get_unwrap.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +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

6363
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
6464
--> $DIR/get_unwrap.rs:48:17
6565
|
6666
48 | let _ = some_vec.get(0..1).unwrap().to_vec();
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&some_vec[0..1])`
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]`
6868

6969
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
7070
--> $DIR/get_unwrap.rs:49:17
7171
|
7272
49 | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&mut some_vec[0..1])`
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]`
7474

7575
error: aborting due to 12 previous errors
7676

0 commit comments

Comments
 (0)