]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/reference.rs
clippy: support `QPath::LangItem`
[rust.git] / clippy_lints / src / reference.rs
1 use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_ast::ast::{Expr, ExprKind, UnOp};
4 use rustc_errors::Applicability;
5 use rustc_lint::{EarlyContext, EarlyLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
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,ignore
19     /// // Bad
20     /// let a = f(*&mut b);
21     /// let c = *&d;
22     ///
23     /// // Good
24     /// let a = f(b);
25     /// let c = d;
26     /// ```
27     pub DEREF_ADDROF,
28     complexity,
29     "use of `*&` or `*&mut` in an expression"
30 }
31
32 declare_lint_pass!(DerefAddrOf => [DEREF_ADDROF]);
33
34 fn without_parens(mut e: &Expr) -> &Expr {
35     while let ExprKind::Paren(ref child_e) = e.kind {
36         e = child_e;
37     }
38     e
39 }
40
41 impl EarlyLintPass for DerefAddrOf {
42     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
43         if_chain! {
44             if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind;
45             if let ExprKind::AddrOf(_, _, ref addrof_target) = without_parens(deref_target).kind;
46             if !in_macro(addrof_target.span);
47             then {
48                 let mut applicability = Applicability::MachineApplicable;
49                 span_lint_and_sugg(
50                     cx,
51                     DEREF_ADDROF,
52                     e.span,
53                     "immediately dereferencing a reference",
54                     "try this",
55                     format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)),
56                     applicability,
57                 );
58             }
59         }
60     }
61 }
62
63 declare_clippy_lint! {
64     /// **What it does:** Checks for references in expressions that use
65     /// auto dereference.
66     ///
67     /// **Why is this bad?** The reference is a no-op and is automatically
68     /// dereferenced by the compiler and makes the code less clear.
69     ///
70     /// **Example:**
71     /// ```rust
72     /// struct Point(u32, u32);
73     /// let point = Point(30, 20);
74     /// let x = (&point).0;
75     /// ```
76     pub REF_IN_DEREF,
77     complexity,
78     "Use of reference in auto dereference expression."
79 }
80
81 declare_lint_pass!(RefInDeref => [REF_IN_DEREF]);
82
83 impl EarlyLintPass for RefInDeref {
84     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
85         if_chain! {
86             if let ExprKind::Field(ref object, _) = e.kind;
87             if let ExprKind::Paren(ref parened) = object.kind;
88             if let ExprKind::AddrOf(_, _, ref inner) = parened.kind;
89             then {
90                 let mut applicability = Applicability::MachineApplicable;
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                     snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(),
98                     applicability,
99                 );
100             }
101         }
102     }
103 }