Skip to content

Commit 013b1f5

Browse files
authored
Merge pull request #2056 from topecongiro/issue-1851
Add suggestion to needless_borrow
2 parents f64bae4 + d8afe2c commit 013b1f5

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

clippy_lints/src/needless_borrow.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::lint::*;
66
use rustc::hir::{BindingAnnotation, Expr, ExprAddrOf, MutImmutable, Pat, PatKind};
77
use rustc::ty;
88
use rustc::ty::adjustment::{Adjust, Adjustment};
9-
use utils::{in_macro, span_lint};
9+
use utils::{in_macro, snippet_opt, span_lint_and_then};
1010

1111
/// **What it does:** Checks for address of operations (`&`) that are going to
1212
/// be dereferenced immediately by the compiler.
@@ -54,12 +54,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
5454
..
5555
}] = *adj3
5656
{
57-
span_lint(
57+
span_lint_and_then(
5858
cx,
5959
NEEDLESS_BORROW,
6060
e.span,
61-
"this expression borrows a reference that is immediately dereferenced by the \
62-
compiler",
61+
"this expression borrows a reference that is immediately dereferenced \
62+
by the compiler",
63+
|db| {
64+
if let Some(snippet) = snippet_opt(cx, inner.span) {
65+
db.span_suggestion(e.span, "change this to", snippet);
66+
}
67+
}
6368
);
6469
}
6570
}
@@ -71,14 +76,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
7176
return;
7277
}
7378
if_let_chain! {[
74-
let PatKind::Binding(BindingAnnotation::Ref, _, _, _) = pat.node,
79+
let PatKind::Binding(BindingAnnotation::Ref, _, name, _) = pat.node,
7580
let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
7681
tam.mutbl == MutImmutable,
7782
let ty::TyRef(_, ref tam) = tam.ty.sty,
7883
// only lint immutable refs, because borrowed `&mut T` cannot be moved out
7984
tam.mutbl == MutImmutable,
8085
], {
81-
span_lint(cx, NEEDLESS_BORROW, pat.span, "this pattern creates a reference to a reference")
86+
span_lint_and_then(
87+
cx,
88+
NEEDLESS_BORROW,
89+
pat.span,
90+
"this pattern creates a reference to a reference",
91+
|db| {
92+
if let Some(snippet) = snippet_opt(cx, name.span) {
93+
db.span_suggestion(pat.span, "change this to", snippet);
94+
}
95+
}
96+
)
8297
}}
8398
}
8499
}

tests/ui/eta.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error: this expression borrows a reference that is immediately dereferenced by t
2222
--> $DIR/eta.rs:11:21
2323
|
2424
11 | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
25-
| ^^^
25+
| ^^^ help: change this to: `&2`
2626
|
2727
= note: `-D needless-borrow` implied by `-D warnings`
2828

tests/ui/needless_borrow.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ error: this expression borrows a reference that is immediately dereferenced by t
22
--> $DIR/needless_borrow.rs:13:15
33
|
44
13 | let c = x(&&a);
5-
| ^^^
5+
| ^^^ help: change this to: `&a`
66
|
77
= note: `-D needless-borrow` implied by `-D warnings`
88

99
error: this pattern creates a reference to a reference
1010
--> $DIR/needless_borrow.rs:20:17
1111
|
1212
20 | if let Some(ref cake) = Some(&5) {}
13-
| ^^^^^^^^
13+
| ^^^^^^^^ help: change this to: `cake`
1414

1515
error: this expression borrows a reference that is immediately dereferenced by the compiler
1616
--> $DIR/needless_borrow.rs:27:15
1717
|
1818
27 | 46 => &&a,
19-
| ^^^
19+
| ^^^ help: change this to: `&a`
2020

2121
error: this pattern takes a reference on something that is being de-referenced
2222
--> $DIR/needless_borrow.rs:49:34
@@ -36,7 +36,7 @@ error: this pattern creates a reference to a reference
3636
--> $DIR/needless_borrow.rs:50:31
3737
|
3838
50 | let _ = v.iter().filter(|&ref a| a.is_empty());
39-
| ^^^^^
39+
| ^^^^^ help: change this to: `a`
4040

4141
error: aborting due to 6 previous errors
4242

0 commit comments

Comments
 (0)