Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4add1e2

Browse files
author
Michael Wright
committed
Improve get_unwrap suggestion
Handle case where a reference is immediately dereferenced. Fixes 3625
1 parent c63b634 commit 4add1e2

File tree

4 files changed

+104
-18
lines changed

4 files changed

+104
-18
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
16031603
} else {
16041604
return; // not linting on a .get().unwrap() chain or variant
16051605
};
1606-
let needs_ref;
1606+
let mut needs_ref;
16071607
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
16081608
needs_ref = get_args_str.parse::<usize>().is_ok();
16091609
"slice"
@@ -1623,6 +1623,22 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
16231623
return; // caller is not a type that we want to lint
16241624
};
16251625

1626+
let mut span = expr.span;
1627+
1628+
// Handle the case where the result is immedately dereferenced
1629+
// by not requiring ref and pulling the dereference into the
1630+
// suggestion.
1631+
if needs_ref {
1632+
if let Some(parent) = get_parent_expr(cx, expr) {
1633+
if let hir::ExprKind::Unary(op, _) = parent.node {
1634+
if op == hir::UnOp::UnDeref {
1635+
needs_ref = false;
1636+
span = parent.span;
1637+
}
1638+
}
1639+
}
1640+
}
1641+
16261642
let mut_str = if is_mut { "_mut" } else { "" };
16271643
let borrow_str = if !needs_ref {
16281644
""
@@ -1631,10 +1647,11 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
16311647
} else {
16321648
"&"
16331649
};
1650+
16341651
span_lint_and_sugg(
16351652
cx,
16361653
GET_UNWRAP,
1637-
expr.span,
1654+
span,
16381655
&format!(
16391656
"called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
16401657
mut_str, caller_type

tests/ui/get_unwrap.fixed

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
// run-rustfix
11+
#![allow(unused_mut)]
12+
13+
use std::collections::BTreeMap;
14+
use std::collections::HashMap;
15+
use std::collections::VecDeque;
16+
use std::iter::FromIterator;
17+
18+
struct GetFalsePositive {
19+
arr: [u32; 3],
20+
}
21+
22+
impl GetFalsePositive {
23+
fn get(&self, pos: usize) -> Option<&u32> {
24+
self.arr.get(pos)
25+
}
26+
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> {
27+
self.arr.get_mut(pos)
28+
}
29+
}
30+
31+
fn main() {
32+
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
33+
let mut some_slice = &mut [0, 1, 2, 3];
34+
let mut some_vec = vec![0, 1, 2, 3];
35+
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
36+
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
37+
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
38+
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
39+
40+
{
41+
// Test `get().unwrap()`
42+
let _ = &boxed_slice[1];
43+
let _ = &some_slice[0];
44+
let _ = &some_vec[0];
45+
let _ = &some_vecdeque[0];
46+
let _ = &some_hashmap[&1];
47+
let _ = &some_btreemap[&1];
48+
let _ = false_positive.get(0).unwrap();
49+
}
50+
51+
{
52+
// Test `get_mut().unwrap()`
53+
boxed_slice[0] = 1;
54+
some_slice[0] = 1;
55+
some_vec[0] = 1;
56+
some_vecdeque[0] = 1;
57+
// Check false positives
58+
*some_hashmap.get_mut(&1).unwrap() = 'b';
59+
*some_btreemap.get_mut(&1).unwrap() = 'b';
60+
*false_positive.get_mut(0).unwrap() = 1;
61+
}
62+
63+
{
64+
// Test `get().unwrap().foo()` and `get_mut().unwrap().bar()`
65+
let _ = some_vec[0..1].to_vec();
66+
let _ = some_vec[0..1].to_vec();
67+
}
68+
}

tests/ui/get_unwrap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
// run-rustfix
1011
#![allow(unused_mut)]
1112

1213
use std::collections::BTreeMap;

tests/ui/get_unwrap.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
2-
--> $DIR/get_unwrap.rs:41:17
2+
--> $DIR/get_unwrap.rs:42:17
33
|
44
LL | let _ = boxed_slice.get(1).unwrap();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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
10-
--> $DIR/get_unwrap.rs:42:17
10+
--> $DIR/get_unwrap.rs:43:17
1111
|
1212
LL | let _ = some_slice.get(0).unwrap();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`
1414

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)