]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/reference.rs
Merge pull request #3269 from rust-lang-nursery/relicense
[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
11 use crate::syntax::ast::{Expr, ExprKind, UnOp};
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use if_chain::if_chain;
15 use crate::utils::{snippet, span_lint_and_sugg};
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                 span_lint_and_sugg(
58                     cx,
59                     DEREF_ADDROF,
60                     e.span,
61                     "immediately dereferencing a reference",
62                     "try this",
63                     format!("{}", snippet(cx, addrof_target.span, "_")),
64                 );
65             }
66         }
67     }
68 }
69
70 /// **What it does:** Checks for references in expressions that use
71 /// auto dereference.
72 ///
73 /// **Why is this bad?** The reference is a no-op and is automatically
74 /// dereferenced by the compiler and makes the code less clear.
75 ///
76 /// **Example:**
77 /// ```rust
78 /// struct Point(u32, u32);
79 /// let point = Foo(30, 20);
80 /// let x = (&point).x;
81 /// ```
82 declare_clippy_lint! {
83     pub REF_IN_DEREF,
84     complexity,
85     "Use of reference in auto dereference expression."
86 }
87
88 pub struct DerefPass;
89
90 impl LintPass for DerefPass {
91     fn get_lints(&self) -> LintArray {
92         lint_array!(REF_IN_DEREF)
93     }
94 }
95
96 impl EarlyLintPass for DerefPass {
97     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
98         if_chain! {
99             if let ExprKind::Field(ref object, ref field_name) = e.node;
100             if let ExprKind::Paren(ref parened) = object.node;
101             if let ExprKind::AddrOf(_, ref inner) = parened.node;
102             then {
103                 span_lint_and_sugg(
104                     cx,
105                     REF_IN_DEREF,
106                     object.span,
107                     "Creating a reference that is immediately dereferenced.",
108                     "try this",
109                     format!(
110                         "{}.{}",
111                         snippet(cx, inner.span, "_"),
112                         snippet(cx, field_name.span, "_")
113                     )
114                 );
115             }
116         }
117     }
118 }