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} ;
45use crate :: utils:: { in_macro, span_lint_and_sugg} ;
56use if_chain:: if_chain;
67
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- /// ```
208declare_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+ /// ```
2129 pub EXPLICIT_DEREF_METHOD ,
2230 pedantic,
2331 "Explicit use of deref or deref_mut method while not in a method chain."
2432}
2533
26- pub struct Pass ;
34+ declare_lint_pass ! ( Dereferencing => [
35+ EXPLICIT_DEREF_METHOD
36+ ] ) ;
2737
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 < ' _ > ) {
3640 if in_macro ( expr. span ) {
3741 return ;
3842 }
3943
4044 if_chain ! {
4145 // 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 ;
4347 // 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 ;
4549 then {
4650 let name = method_name. ident. as_str( ) ;
4751 // alter help slightly to account for _mut
@@ -54,6 +58,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5458 "explicit deref method call" ,
5559 "try this" ,
5660 format!( "&*{}" , path) ,
61+ Applicability :: MachineApplicable
5762 ) ;
5863 } ,
5964 "deref_mut" => {
@@ -64,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
6469 "explicit deref_mut method call" ,
6570 "try this" ,
6671 format!( "&mut *{}" , path) ,
72+ Applicability :: MachineApplicable
6773 ) ;
6874 } ,
6975 _ => ( )
0 commit comments