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