]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/reference.rs
Changes lint sugg to bitwise and operator `&`
[rust.git] / clippy_lints / src / reference.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
11 use crate::rustc::{declare_tool_lint, lint_array};
12 use crate::rustc_errors::Applicability;
13 use crate::syntax::ast::{Expr, ExprKind, UnOp};
14 use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
15 use if_chain::if_chain;
16
17 /// **What it does:** Checks for usage of `*&` and `*&mut` in expressions.
18 ///
19 /// **Why is this bad?** Immediately dereferencing a reference is no-op and
20 /// makes the code less clear.
21 ///
22 /// **Known problems:** Multiple dereference/addrof pairs are not handled so
23 /// the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect.
24 ///
25 /// **Example:**
26 /// ```rust
27 /// let a = f(*&mut b);
28 /// let c = *&d;
29 /// ```
30 declare_clippy_lint! {
31     pub DEREF_ADDROF,
32     complexity,
33     "use of `*&` or `*&mut` in an expression"
34 }
35
36 pub struct Pass;
37
38 impl LintPass for Pass {
39     fn get_lints(&self) -> LintArray {
40         lint_array!(DEREF_ADDROF)
41     }
42 }
43
44 fn without_parens(mut e: &Expr) -> &Expr {
45     while let ExprKind::Paren(ref child_e) = e.node {
46         e = child_e;
47     }
48     e
49 }
50
51 impl EarlyLintPass for Pass {
52     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
53         if_chain! {
54             if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.node;
55             if let ExprKind::AddrOf(_, ref addrof_target) = without_parens(deref_target).node;
56             then {
57                 let mut applicability = Applicability::MachineApplicable;
58                 span_lint_and_sugg(
59                     cx,
60                     DEREF_ADDROF,
61                     e.span,
62                     "immediately dereferencing a reference",
63                     "try this",
64                     format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)),
65                     applicability,
66                 );
67             }
68         }
69     }
70 }
71
72 /// **What it does:** Checks for references in expressions that use
73 /// auto dereference.
74 ///
75 /// **Why is this bad?** The reference is a no-op and is automatically
76 /// dereferenced by the compiler and makes the code less clear.
77 ///
78 /// **Example:**
79 /// ```rust
80 /// struct Point(u32, u32);
81 /// let point = Foo(30, 20);
82 /// let x = (&point).x;
83 /// ```
84 declare_clippy_lint! {
85     pub REF_IN_DEREF,
86     complexity,
87     "Use of reference in auto dereference expression."
88 }
89
90 pub struct DerefPass;
91
92 impl LintPass for DerefPass {
93     fn get_lints(&self) -> LintArray {
94         lint_array!(REF_IN_DEREF)
95     }
96 }
97
98 impl EarlyLintPass for DerefPass {
99     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
100         if_chain! {
101             if let ExprKind::Field(ref object, ref field_name) = e.node;
102             if let ExprKind::Paren(ref parened) = object.node;
103             if let ExprKind::AddrOf(_, ref inner) = parened.node;
104             then {
105                 let mut applicability = Applicability::MachineApplicable;
106                 span_lint_and_sugg(
107                     cx,
108                     REF_IN_DEREF,
109                     object.span,
110                     "Creating a reference that is immediately dereferenced.",
111                     "try this",
112                     format!(
113                         "{}.{}",
114                         snippet_with_applicability(cx, inner.span, "_", &mut applicability),
115                         snippet_with_applicability(cx, field_name.span, "_", &mut applicability)
116                     ),
117                     applicability,
118                 );
119             }
120         }
121     }
122 }