Skip to content

Commit 6ec2388

Browse files
committed
Auto merge of #10979 - y21:issue9909, r=giraffate
[`get_unwrap`]: include a borrow in the suggestion if argument is not an integer literal Fixes #9909 I have to say, I don't really understand what the previous logic was trying to do, but this fixes the linked bug. It was checking if the argument passed to `.get()` can be parsed as a usize (i.e. if it's an integer literal, probably?), and if not, it wouldn't include a borrow? I don't know how we came to that conclusion, but that logic doesn't work: ```rs let slice = &[1, 2]; let _r: &i32 = slice.get({ 1 }).unwrap(); // previous suggestion: slice[{ 1 }] // the suggestion should be: &slice[{ 1 }] ``` Here the argument passed to it isn't an integer literal, but it should still include a borrow, because it would otherwise change the type from `&i32` to `i32`. The exception is that if the parent of the `get().unwrap()` expr is a dereference or a method call or the like, we don't need an explicit borrow because it's automatically inserted by the compiler changelog: [`get_unwrap`]: include a borrow in the suggestion if argument is not an integer literal
2 parents 9fa4089 + bdb2a17 commit 6ec2388

File tree

4 files changed

+119
-18
lines changed

4 files changed

+119
-18
lines changed

clippy_lints/src/methods/get_unwrap.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::get_parent_expr;
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::is_type_diagnostic_item;
6-
use if_chain::if_chain;
76
use rustc_errors::Applicability;
87
use rustc_hir as hir;
98
use rustc_lint::LateContext;
@@ -23,40 +22,40 @@ pub(super) fn check<'tcx>(
2322
let mut applicability = Applicability::MachineApplicable;
2423
let expr_ty = cx.typeck_results().expr_ty(recv);
2524
let get_args_str = snippet_with_applicability(cx, get_arg.span, "..", &mut applicability);
26-
let mut needs_ref;
2725
let caller_type = if derefs_to_slice(cx, recv, expr_ty).is_some() {
28-
needs_ref = get_args_str.parse::<usize>().is_ok();
2926
"slice"
3027
} else if is_type_diagnostic_item(cx, expr_ty, sym::Vec) {
31-
needs_ref = get_args_str.parse::<usize>().is_ok();
3228
"Vec"
3329
} else if is_type_diagnostic_item(cx, expr_ty, sym::VecDeque) {
34-
needs_ref = get_args_str.parse::<usize>().is_ok();
3530
"VecDeque"
3631
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::HashMap) {
37-
needs_ref = true;
3832
"HashMap"
3933
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::BTreeMap) {
40-
needs_ref = true;
4134
"BTreeMap"
4235
} else {
4336
return; // caller is not a type that we want to lint
4437
};
4538

4639
let mut span = expr.span;
4740

48-
// Handle the case where the result is immediately dereferenced
49-
// by not requiring ref and pulling the dereference into the
50-
// suggestion.
51-
if_chain! {
52-
if needs_ref;
53-
if let Some(parent) = get_parent_expr(cx, expr);
54-
if let hir::ExprKind::Unary(hir::UnOp::Deref, _) = parent.kind;
55-
then {
56-
needs_ref = false;
41+
// Handle the case where the result is immediately dereferenced,
42+
// either directly be the user, or as a result of a method call or the like
43+
// by not requiring an explicit reference
44+
let needs_ref = if let Some(parent) = get_parent_expr(cx, expr)
45+
&& let hir::ExprKind::Unary(hir::UnOp::Deref, _)
46+
| hir::ExprKind::MethodCall(..)
47+
| hir::ExprKind::Field(..)
48+
| hir::ExprKind::Index(..) = parent.kind
49+
{
50+
if let hir::ExprKind::Unary(hir::UnOp::Deref, _) = parent.kind {
51+
// if the user explicitly dereferences the result, we can adjust
52+
// the span to also include the deref part
5753
span = parent.span;
5854
}
59-
}
55+
false
56+
} else {
57+
true
58+
};
6059

6160
let mut_str = if is_mut { "_mut" } else { "" };
6261
let borrow_str = if !needs_ref {

tests/ui/get_unwrap.fixed

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,42 @@ fn main() {
7070
let _ = some_vec[0..1].to_vec();
7171
}
7272
}
73+
mod issue9909 {
74+
#![allow(clippy::identity_op, clippy::unwrap_used, dead_code)]
75+
76+
fn reduced() {
77+
let f = &[1, 2, 3];
78+
79+
let _x: &i32 = &f[1 + 2];
80+
// ^ include a borrow in the suggestion, even if the argument is not just a numeric literal
81+
82+
let _x = f[1 + 2].to_string();
83+
// ^ don't include a borrow here
84+
85+
let _x = f[1 + 2].abs();
86+
// ^ don't include a borrow here
87+
}
88+
89+
// original code:
90+
fn linidx(row: usize, col: usize) -> usize {
91+
row * 1 + col * 3
92+
}
93+
94+
fn main_() {
95+
let mut mat = [1.0f32, 5.0, 9.0, 2.0, 6.0, 10.0, 3.0, 7.0, 11.0, 4.0, 8.0, 12.0];
96+
97+
for i in 0..2 {
98+
for j in i + 1..3 {
99+
if mat[linidx(j, 3)] > mat[linidx(i, 3)] {
100+
for k in 0..4 {
101+
let (x, rest) = mat.split_at_mut(linidx(i, k) + 1);
102+
let a = x.last_mut().unwrap();
103+
let b = &mut rest[linidx(j, k) - linidx(i, k) - 1];
104+
::std::mem::swap(a, b);
105+
}
106+
}
107+
}
108+
}
109+
assert_eq!([9.0, 5.0, 1.0, 10.0, 6.0, 2.0, 11.0, 7.0, 3.0, 12.0, 8.0, 4.0], mat);
110+
}
111+
}

tests/ui/get_unwrap.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,42 @@ fn main() {
7070
let _ = some_vec.get_mut(0..1).unwrap().to_vec();
7171
}
7272
}
73+
mod issue9909 {
74+
#![allow(clippy::identity_op, clippy::unwrap_used, dead_code)]
75+
76+
fn reduced() {
77+
let f = &[1, 2, 3];
78+
79+
let _x: &i32 = f.get(1 + 2).unwrap();
80+
// ^ include a borrow in the suggestion, even if the argument is not just a numeric literal
81+
82+
let _x = f.get(1 + 2).unwrap().to_string();
83+
// ^ don't include a borrow here
84+
85+
let _x = f.get(1 + 2).unwrap().abs();
86+
// ^ don't include a borrow here
87+
}
88+
89+
// original code:
90+
fn linidx(row: usize, col: usize) -> usize {
91+
row * 1 + col * 3
92+
}
93+
94+
fn main_() {
95+
let mut mat = [1.0f32, 5.0, 9.0, 2.0, 6.0, 10.0, 3.0, 7.0, 11.0, 4.0, 8.0, 12.0];
96+
97+
for i in 0..2 {
98+
for j in i + 1..3 {
99+
if mat[linidx(j, 3)] > mat[linidx(i, 3)] {
100+
for k in 0..4 {
101+
let (x, rest) = mat.split_at_mut(linidx(i, k) + 1);
102+
let a = x.last_mut().unwrap();
103+
let b = rest.get_mut(linidx(j, k) - linidx(i, k) - 1).unwrap();
104+
::std::mem::swap(a, b);
105+
}
106+
}
107+
}
108+
}
109+
assert_eq!([9.0, 5.0, 1.0, 10.0, 6.0, 2.0, 11.0, 7.0, 3.0, 12.0, 8.0, 4.0], mat);
110+
}
111+
}

tests/ui/get_unwrap.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,29 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
187187
|
188188
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
189189

190-
error: aborting due to 26 previous errors
190+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
191+
--> $DIR/get_unwrap.rs:79:24
192+
|
193+
LL | let _x: &i32 = f.get(1 + 2).unwrap();
194+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `&f[1 + 2]`
195+
196+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
197+
--> $DIR/get_unwrap.rs:82:18
198+
|
199+
LL | let _x = f.get(1 + 2).unwrap().to_string();
200+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `f[1 + 2]`
201+
202+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
203+
--> $DIR/get_unwrap.rs:85:18
204+
|
205+
LL | let _x = f.get(1 + 2).unwrap().abs();
206+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `f[1 + 2]`
207+
208+
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
209+
--> $DIR/get_unwrap.rs:103:33
210+
|
211+
LL | let b = rest.get_mut(linidx(j, k) - linidx(i, k) - 1).unwrap();
212+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut rest[linidx(j, k) - linidx(i, k) - 1]`
213+
214+
error: aborting due to 30 previous errors
191215

0 commit comments

Comments
 (0)