File tree 2 files changed +16
-8
lines changed
2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change 3
3
//! This lint is **warn** by default
4
4
5
5
use rustc:: lint:: * ;
6
- use rustc:: hir:: * ;
6
+ use rustc:: hir:: { ExprAddrOf , Expr , MutImmutable } ;
7
7
use rustc:: ty:: TyRef ;
8
8
use utils:: { span_lint, in_macro} ;
9
+ use rustc:: ty:: adjustment:: AutoAdjustment :: AdjustDerefRef ;
9
10
10
11
/// **What it does:** This lint checks for address of operations (`&`) that are going to be dereferenced immediately by the compiler
11
12
///
@@ -36,13 +37,13 @@ impl LateLintPass for NeedlessBorrow {
36
37
}
37
38
if let ExprAddrOf ( MutImmutable , ref inner) = e. node {
38
39
if let TyRef ( ..) = cx. tcx . expr_ty ( inner) . sty {
39
- let ty = cx. tcx . expr_ty ( e ) ;
40
- let adj_ty = cx . tcx . expr_ty_adjusted ( e ) ;
41
- if ty != adj_ty {
42
- span_lint ( cx ,
43
- NEEDLESS_BORROW ,
44
- e . span ,
45
- "this expression borrows a reference that is immediately dereferenced by the compiler" ) ;
40
+ if let Some ( & AdjustDerefRef ( ref deref ) ) = cx. tcx . tables . borrow ( ) . adjustments . get ( & e . id ) {
41
+ if deref . autoderefs > 1 && deref . autoref . is_some ( ) {
42
+ span_lint ( cx ,
43
+ NEEDLESS_BORROW ,
44
+ e . span ,
45
+ "this expression borrows a reference that is immediately dereferenced by the compiler" ) ;
46
+ }
46
47
}
47
48
}
48
49
}
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ fn main() {
16
16
let g_val = g ( & Vec :: new ( ) ) ; // should not error, because `&Vec<T>` derefs to `&[T]`
17
17
let vec = Vec :: new ( ) ;
18
18
let vec_val = g ( & vec) ; // should not error, because `&Vec<T>` derefs to `&[T]`
19
+ h ( & "foo" ) ; // should not error, because the `&&str` is required, due to `&Trait`
19
20
}
20
21
21
22
fn f < T : Copy > ( y : & T ) -> T {
@@ -25,3 +26,9 @@ fn f<T:Copy>(y: &T) -> T {
25
26
fn g ( y : & [ u8 ] ) -> u8 {
26
27
y[ 0 ]
27
28
}
29
+
30
+ trait Trait { }
31
+
32
+ impl < ' a > Trait for & ' a str { }
33
+
34
+ fn h ( _: & Trait ) { }
You can’t perform that action at this time.
0 commit comments