1
- use crate :: rustc:: hir:: { Expr , ExprKind , QPath } ;
2
- use crate :: rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
3
- use crate :: rustc:: { declare_tool_lint, lint_array} ;
1
+ use rustc_hir:: { Expr , ExprKind , QPath } ;
2
+ use rustc_errors:: Applicability ;
3
+ use rustc_lint:: { LateContext , LateLintPass } ;
4
+ use rustc_session:: { declare_tool_lint, declare_lint_pass} ;
4
5
use crate :: utils:: { in_macro, span_lint_and_sugg} ;
5
6
use if_chain:: if_chain;
6
7
7
- /// **What it does:** Checks for explicit deref() or deref_mut() method calls.
8
- ///
9
- /// **Why is this bad?** Derefencing by &*x or &mut *x is clearer and more concise,
10
- /// when not part of a method chain.
11
- ///
12
- /// **Example:**
13
- /// ```rust
14
- /// let b = a.deref();
15
- /// let c = a.deref_mut();
16
- ///
17
- /// // excludes
18
- /// let e = d.unwrap().deref();
19
- /// ```
20
8
declare_clippy_lint ! {
9
+ /// **What it does:** Checks for explicit `deref()` or `deref_mut()` method calls.
10
+ ///
11
+ /// **Why is this bad?** Derefencing by `&*x` or `&mut *x` is clearer and more concise,
12
+ /// when not part of a method chain.
13
+ ///
14
+ /// **Example:**
15
+ /// ```rust
16
+ /// let b = a.deref();
17
+ /// let c = a.deref_mut();
18
+ /// ```
19
+ /// Could be written as:
20
+ /// ```rust
21
+ /// let b = &*a;
22
+ /// let c = &mut *a;
23
+ /// ```
24
+ ///
25
+ /// This lint excludes
26
+ /// ```rust
27
+ /// let e = d.unwrap().deref();
28
+ /// ```
21
29
pub EXPLICIT_DEREF_METHOD ,
22
30
pedantic,
23
31
"Explicit use of deref or deref_mut method while not in a method chain."
24
32
}
25
33
26
- pub struct Pass ;
34
+ declare_lint_pass ! ( Dereferencing => [
35
+ EXPLICIT_DEREF_METHOD
36
+ ] ) ;
27
37
28
- impl LintPass for Pass {
29
- fn get_lints ( & self ) -> LintArray {
30
- lint_array ! ( EXPLICIT_DEREF_METHOD )
31
- }
32
- }
33
-
34
- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for Pass {
35
- fn check_expr ( & mut self , cx : & LateContext < ' _ , ' _ > , expr : & Expr ) {
38
+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for Dereferencing {
39
+ fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
36
40
if in_macro ( expr. span ) {
37
41
return ;
38
42
}
39
43
40
44
if_chain ! {
41
45
// if this is a method call
42
- if let ExprKind :: MethodCall ( ref method_name, _, ref args) = & expr. node ;
46
+ if let ExprKind :: MethodCall ( ref method_name, _, ref args) = & expr. kind ;
43
47
// on a Path (i.e. a variable/name, not another method)
44
- if let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = & args[ 0 ] . node ;
48
+ if let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = & args[ 0 ] . kind ;
45
49
then {
46
50
let name = method_name. ident. as_str( ) ;
47
51
// alter help slightly to account for _mut
@@ -54,6 +58,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
54
58
"explicit deref method call" ,
55
59
"try this" ,
56
60
format!( "&*{}" , path) ,
61
+ Applicability :: MachineApplicable
57
62
) ;
58
63
} ,
59
64
"deref_mut" => {
@@ -64,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
64
69
"explicit deref_mut method call" ,
65
70
"try this" ,
66
71
format!( "&mut *{}" , path) ,
72
+ Applicability :: MachineApplicable
67
73
) ;
68
74
} ,
69
75
_ => ( )
0 commit comments