]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/reference.rs
rustup https://github.com/rust-lang/rust/pull/61758/files
[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_lint_pass, declare_tool_lint};
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 declare_lint_pass!(DerefAddrOf => [DEREF_ADDROF]);
28
29 fn without_parens(mut e: &Expr) -> &Expr {
30     while let ExprKind::Paren(ref child_e) = e.node {
31         e = child_e;
32     }
33     e
34 }
35
36 impl EarlyLintPass for DerefAddrOf {
37     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
38         if_chain! {
39             if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.node;
40             if let ExprKind::AddrOf(_, ref addrof_target) = without_parens(deref_target).node;
41             then {
42                 let mut applicability = Applicability::MachineApplicable;
43                 span_lint_and_sugg(
44                     cx,
45                     DEREF_ADDROF,
46                     e.span,
47                     "immediately dereferencing a reference",
48                     "try this",
49                     format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)),
50                     applicability,
51                 );
52             }
53         }
54     }
55 }
56
57 declare_clippy_lint! {
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     pub REF_IN_DEREF,
71     complexity,
72     "Use of reference in auto dereference expression."
73 }
74
75 declare_lint_pass!(RefInDeref => [REF_IN_DEREF]);
76
77 impl EarlyLintPass for RefInDeref {
78     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
79         if_chain! {
80             if let ExprKind::Field(ref object, _) = e.node;
81             if let ExprKind::Paren(ref parened) = object.node;
82             if let ExprKind::AddrOf(_, ref inner) = parened.node;
83             then {
84                 let mut applicability = Applicability::MachineApplicable;
85                 span_lint_and_sugg(
86                     cx,
87                     REF_IN_DEREF,
88                     object.span,
89                     "Creating a reference that is immediately dereferenced.",
90                     "try this",
91                     snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(),
92                     applicability,
93                 );
94             }
95         }
96     }
97 }