]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs
Rollup merge of #89789 - jkugelman:must-use-thread-builder, r=joshtriplett
[rust.git] / src / tools / clippy / clippy_lints / src / needless_borrowed_ref.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet_with_applicability;
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{BindingAnnotation, Mutability, Node, Pat, PatKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for bindings that destructure a reference and borrow the inner
12     /// value with `&ref`.
13     ///
14     /// ### Why is this bad?
15     /// This pattern has no effect in almost all cases.
16     ///
17     /// ### Known problems
18     /// In some cases, `&ref` is needed to avoid a lifetime mismatch error.
19     /// Example:
20     /// ```rust
21     /// fn foo(a: &Option<String>, b: &Option<String>) {
22     ///     match (a, b) {
23     ///         (None, &ref c) | (&ref c, None) => (),
24     ///         (&Some(ref c), _) => (),
25     ///     };
26     /// }
27     /// ```
28     ///
29     /// ### Example
30     /// Bad:
31     /// ```rust
32     /// let mut v = Vec::<String>::new();
33     /// let _ = v.iter_mut().filter(|&ref a| a.is_empty());
34     /// ```
35     ///
36     /// Good:
37     /// ```rust
38     /// let mut v = Vec::<String>::new();
39     /// let _ = v.iter_mut().filter(|a| a.is_empty());
40     /// ```
41     pub NEEDLESS_BORROWED_REFERENCE,
42     complexity,
43     "destructuring a reference and borrowing the inner value"
44 }
45
46 declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]);
47
48 impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef {
49     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
50         if pat.span.from_expansion() {
51             // OK, simple enough, lints doesn't check in macro.
52             return;
53         }
54
55         if_chain! {
56             // Only lint immutable refs, because `&mut ref T` may be useful.
57             if let PatKind::Ref(sub_pat, Mutability::Not) = pat.kind;
58
59             // Check sub_pat got a `ref` keyword (excluding `ref mut`).
60             if let PatKind::Binding(BindingAnnotation::Ref, .., spanned_name, _) = sub_pat.kind;
61             let parent_id = cx.tcx.hir().get_parent_node(pat.hir_id);
62             if let Some(parent_node) = cx.tcx.hir().find(parent_id);
63             then {
64                 // do not recurse within patterns, as they may have other references
65                 // XXXManishearth we can relax this constraint if we only check patterns
66                 // with a single ref pattern inside them
67                 if let Node::Pat(_) = parent_node {
68                     return;
69                 }
70                 let mut applicability = Applicability::MachineApplicable;
71                 span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, pat.span,
72                                    "this pattern takes a reference on something that is being de-referenced",
73                                    |diag| {
74                                        let hint = snippet_with_applicability(cx, spanned_name.span, "..", &mut applicability).into_owned();
75                                        diag.span_suggestion(
76                                            pat.span,
77                                            "try removing the `&ref` part and just keep",
78                                            hint,
79                                            applicability,
80                                        );
81                                    });
82             }
83         }
84     }
85 }