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