File tree 4 files changed +51
-23
lines changed
4 files changed +51
-23
lines changed Original file line number Diff line number Diff line change @@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
104
104
if e. span . from_expansion ( ) {
105
105
return ;
106
106
}
107
- if let ExprKind :: AddrOf ( BorrowKind :: Ref , Mutability :: Not , inner) = e. kind {
107
+ if let ExprKind :: AddrOf ( BorrowKind :: Ref , mutability , inner) = e. kind {
108
108
if let ty:: Ref ( _, ty, _) = cx. typeck_results ( ) . expr_ty ( inner) . kind ( ) {
109
109
for adj3 in cx. typeck_results ( ) . expr_adjustments ( e) . windows ( 3 ) {
110
110
if let [ Adjustment {
@@ -116,14 +116,20 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
116
116
..
117
117
} ] = * adj3
118
118
{
119
+ let help_msg_ty = if matches ! ( mutability, Mutability :: Not ) {
120
+ format ! ( "&{}" , ty)
121
+ } else {
122
+ format ! ( "&mut {}" , ty)
123
+ } ;
124
+
119
125
span_lint_and_then (
120
126
cx,
121
127
NEEDLESS_BORROW ,
122
128
e. span ,
123
129
& format ! (
124
- "this expression borrows a reference (`& {}`) that is immediately dereferenced \
130
+ "this expression borrows a reference (`{}`) that is immediately dereferenced \
125
131
by the compiler",
126
- ty
132
+ help_msg_ty
127
133
) ,
128
134
|diag| {
129
135
if let Some ( snippet) = snippet_opt ( cx, inner. span ) {
Original file line number Diff line number Diff line change 1
1
// run-rustfix
2
2
3
- #![allow(clippy::needless_borrowed_reference)]
4
-
5
- fn x(y: &i32) -> i32 {
6
- *y
7
- }
8
-
9
3
#[warn(clippy::all, clippy::needless_borrow)]
10
4
#[allow(unused_variables)]
11
5
fn main() {
12
6
let a = 5;
13
- let b = x(&a);
14
- let c = x(&a);
7
+ let _ = x(&a); // no warning
8
+ let _ = x(&a); // warn
9
+
10
+ let mut b = 5;
11
+ mut_ref(&mut b); // no warning
12
+ mut_ref(&mut b); // warn
13
+
15
14
let s = &String::from("hi");
16
15
let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
17
16
let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
@@ -29,6 +28,15 @@ fn main() {
29
28
};
30
29
}
31
30
31
+ #[allow(clippy::needless_borrowed_reference)]
32
+ fn x(y: &i32) -> i32 {
33
+ *y
34
+ }
35
+
36
+ fn mut_ref(y: &mut i32) {
37
+ *y = 5;
38
+ }
39
+
32
40
fn f<T: Copy>(y: &T) -> T {
33
41
*y
34
42
}
Original file line number Diff line number Diff line change 1
1
// run-rustfix
2
2
3
- #![ allow( clippy:: needless_borrowed_reference) ]
4
-
5
- fn x ( y : & i32 ) -> i32 {
6
- * y
7
- }
8
-
9
3
#[ warn( clippy:: all, clippy:: needless_borrow) ]
10
4
#[ allow( unused_variables) ]
11
5
fn main ( ) {
12
6
let a = 5 ;
13
- let b = x ( & a) ;
14
- let c = x ( & & a) ;
7
+ let _ = x ( & a) ; // no warning
8
+ let _ = x ( & & a) ; // warn
9
+
10
+ let mut b = 5 ;
11
+ mut_ref ( & mut b) ; // no warning
12
+ mut_ref ( & mut & mut b) ; // warn
13
+
15
14
let s = & String :: from ( "hi" ) ;
16
15
let s_ident = f ( & s) ; // should not error, because `&String` implements Copy, but `String` does not
17
16
let g_val = g ( & Vec :: new ( ) ) ; // should not error, because `&Vec<T>` derefs to `&[T]`
@@ -29,6 +28,15 @@ fn main() {
29
28
} ;
30
29
}
31
30
31
+ #[ allow( clippy:: needless_borrowed_reference) ]
32
+ fn x ( y : & i32 ) -> i32 {
33
+ * y
34
+ }
35
+
36
+ fn mut_ref ( y : & mut i32 ) {
37
+ * y = 5 ;
38
+ }
39
+
32
40
fn f < T : Copy > ( y : & T ) -> T {
33
41
* y
34
42
}
Original file line number Diff line number Diff line change 1
1
error: this expression borrows a reference (`&i32`) that is immediately dereferenced by the compiler
2
- --> $DIR/needless_borrow.rs:14 :15
2
+ --> $DIR/needless_borrow.rs:8 :15
3
3
|
4
- LL | let c = x(&&a);
4
+ LL | let _ = x(&&a); // warn
5
5
| ^^^ help: change this to: `&a`
6
6
|
7
7
= note: `-D clippy::needless-borrow` implied by `-D warnings`
8
8
9
+ error: this expression borrows a reference (`&mut i32`) that is immediately dereferenced by the compiler
10
+ --> $DIR/needless_borrow.rs:12:13
11
+ |
12
+ LL | mut_ref(&mut &mut b); // warn
13
+ | ^^^^^^^^^^^ help: change this to: `&mut b`
14
+
9
15
error: this expression borrows a reference (`&i32`) that is immediately dereferenced by the compiler
10
- --> $DIR/needless_borrow.rs:27 :15
16
+ --> $DIR/needless_borrow.rs:26 :15
11
17
|
12
18
LL | 46 => &&a,
13
19
| ^^^ help: change this to: `&a`
14
20
15
- error: aborting due to 2 previous errors
21
+ error: aborting due to 3 previous errors
16
22
You can’t perform that action at this time.
0 commit comments