@@ -6,30 +6,34 @@ use rustc_errors::Applicability;
6
6
use rustc_hir:: ExprKind ;
7
7
use rustc_hir:: UnOp ;
8
8
use rustc_lint:: { LateContext , LateLintPass } ;
9
+ use rustc_middle:: mir:: Mutability ;
9
10
use rustc_middle:: ty;
10
11
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
12
12
13
declare_clippy_lint ! {
13
14
/// ### What it does
14
- /// Checks for manual deref in function(no generic type) parameters .
15
+ /// Checks for `&*(&T)` .
15
16
///
16
17
/// ### Why is this bad?
17
- /// There is no need to deref manually. Compiler will auto deref.
18
+ /// If you just want to reborrow, `&T` is enough (`&T` is Copy).
19
+ /// if you want to deref explicitly, `&** (&T)` is what you need.
18
20
///
19
21
/// ### Known problems
20
- /// Complicate type is not handled. For example `foo(&*****(&&T))` .
22
+ /// None .
21
23
///
22
24
/// ### Example
23
25
/// ```rust
24
- /// fn foo(_: &str) {}
25
- /// let pf = &String::new();
26
+ /// let s = &String::new();
27
+ ///
26
28
/// // Bad
27
- /// foo(&**pf) ;
28
- /// foo(&*String::new() );
29
+ /// let a: &String = &* s ;
30
+ /// foo(&*s );
29
31
///
30
32
/// // Good
31
- /// foo(pf);
32
- /// foo(&String::new());
33
+ /// let a: &String = s;
34
+ /// foo(&**s);
35
+ ///
36
+ /// fn foo(_: &str){ }
33
37
/// ```
34
38
pub NEEDLESS_DEREF ,
35
39
complexity,
@@ -42,12 +46,11 @@ impl LateLintPass<'_> for NeedlessDeref {
42
46
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx rustc_hir:: Expr < ' _ > ) {
43
47
if_chain ! {
44
48
if !e. span. from_expansion( ) && !in_macro( e. span) ;
45
- if let ExprKind :: AddrOf ( _, ref outer_mutability , addrof_target) = e. kind;
49
+ if let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind;
46
50
if let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind;
47
51
if !matches!( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) ) ;
48
52
let inner_ty = cx. typeck_results( ) . expr_ty( deref_target) ;
49
- if let ty:: Ref ( _, _, inner_mutability) = inner_ty. kind( ) ;
50
- if inner_mutability == outer_mutability ;
53
+ if let ty:: Ref ( _, _, Mutability :: Not ) = inner_ty. kind( ) ;
51
54
then{
52
55
53
56
let map=cx. tcx. hir( ) ;
0 commit comments