]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/reference.rs
Rollup merge of #97415 - cjgillot:is-late-bound-solo, r=estebank
[rust.git] / clippy_lints / src / reference.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::{snippet_opt, snippet_with_applicability};
3 use if_chain::if_chain;
4 use rustc_ast::ast::{Expr, ExprKind, Mutability, UnOp};
5 use rustc_errors::Applicability;
6 use rustc_lint::{EarlyContext, EarlyLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::BytePos;
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for usage of `*&` and `*&mut` in expressions.
13     ///
14     /// ### Why is this bad?
15     /// Immediately dereferencing a reference is no-op and
16     /// makes the code less clear.
17     ///
18     /// ### Known problems
19     /// Multiple dereference/addrof pairs are not handled so
20     /// the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect.
21     ///
22     /// ### Example
23     /// ```rust,ignore
24     /// // Bad
25     /// let a = f(*&mut b);
26     /// let c = *&d;
27     ///
28     /// // Good
29     /// let a = f(b);
30     /// let c = d;
31     /// ```
32     #[clippy::version = "pre 1.29.0"]
33     pub DEREF_ADDROF,
34     complexity,
35     "use of `*&` or `*&mut` in an expression"
36 }
37
38 declare_lint_pass!(DerefAddrOf => [DEREF_ADDROF]);
39
40 fn without_parens(mut e: &Expr) -> &Expr {
41     while let ExprKind::Paren(ref child_e) = e.kind {
42         e = child_e;
43     }
44     e
45 }
46
47 impl EarlyLintPass for DerefAddrOf {
48     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
49         if_chain! {
50             if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind;
51             if let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind;
52             if deref_target.span.ctxt() == e.span.ctxt();
53             if !addrof_target.span.from_expansion();
54             then {
55                 let mut applicability = Applicability::MachineApplicable;
56                 let sugg = if e.span.from_expansion() {
57                     if let Some(macro_source) = snippet_opt(cx, e.span) {
58                         // Remove leading whitespace from the given span
59                         // e.g: ` $visitor` turns into `$visitor`
60                         let trim_leading_whitespaces = |span| {
61                             snippet_opt(cx, span).and_then(|snip| {
62                                 #[expect(clippy::cast_possible_truncation)]
63                                 snip.find(|c: char| !c.is_whitespace()).map(|pos| {
64                                     span.lo() + BytePos(pos as u32)
65                                 })
66                             }).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace))
67                         };
68
69                         let mut generate_snippet = |pattern: &str| {
70                             #[expect(clippy::cast_possible_truncation)]
71                             macro_source.rfind(pattern).map(|pattern_pos| {
72                                 let rpos = pattern_pos + pattern.len();
73                                 let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32));
74                                 let span = trim_leading_whitespaces(span_after_ref);
75                                 snippet_with_applicability(cx, span, "_", &mut applicability)
76                             })
77                         };
78
79                         if *mutability == Mutability::Mut {
80                             generate_snippet("mut")
81                         } else {
82                             generate_snippet("&")
83                         }
84                     } else {
85                         Some(snippet_with_applicability(cx, e.span, "_", &mut applicability))
86                     }
87                 } else {
88                     Some(snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability))
89                 };
90                 if let Some(sugg) = sugg {
91                     span_lint_and_sugg(
92                         cx,
93                         DEREF_ADDROF,
94                         e.span,
95                         "immediately dereferencing a reference",
96                         "try this",
97                         sugg.to_string(),
98                         applicability,
99                     );
100                 }
101             }
102         }
103     }
104 }