1 use clippy_utils::diagnostics::span_lint_and_then;
2 use rustc_errors::Applicability;
3 use rustc_hir::{BindingAnnotation, Mutability, Node, Pat, PatKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// Checks for bindings that needlessly destructure a reference and borrow the inner
10 /// value with `&ref`.
12 /// ### Why is this bad?
13 /// This pattern has no effect in almost all cases.
17 /// let mut v = Vec::<String>::new();
18 /// v.iter_mut().filter(|&ref a| a.is_empty());
20 /// if let &[ref first, ref second] = v.as_slice() {}
25 /// let mut v = Vec::<String>::new();
26 /// v.iter_mut().filter(|a| a.is_empty());
28 /// if let [first, second] = v.as_slice() {}
30 #[clippy::version = "pre 1.29.0"]
31 pub NEEDLESS_BORROWED_REFERENCE,
33 "destructuring a reference and borrowing the inner value"
36 declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]);
38 impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef {
39 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
40 if pat.span.from_expansion() {
41 // OK, simple enough, lints doesn't check in macro.
45 // Do not lint patterns that are part of an OR `|` pattern, the binding mode must match in all arms
46 for (_, node) in cx.tcx.hir().parent_iter(pat.hir_id) {
47 let Node::Pat(pat) = node else { break };
49 if matches!(pat.kind, PatKind::Or(_)) {
54 // Only lint immutable refs, because `&mut ref T` may be useful.
55 let PatKind::Ref(sub_pat, Mutability::Not) = pat.kind else { return };
58 // Check sub_pat got a `ref` keyword (excluding `ref mut`).
59 PatKind::Binding(BindingAnnotation::REF, _, ident, None) => {
62 NEEDLESS_BORROWED_REFERENCE,
64 "this pattern takes a reference on something that is being dereferenced",
68 let span = pat.span.until(ident.span);
69 diag.span_suggestion_verbose(
71 "try removing the `&ref` part",
73 Applicability::MachineApplicable,
78 // Slices where each element is `ref`: `&[ref a, ref b, ..., ref z]`
83 kind: PatKind::Wild, ..
87 let mut suggestions = Vec::new();
89 for element_pat in itertools::chain(before, after) {
90 if let PatKind::Binding(BindingAnnotation::REF, _, ident, None) = element_pat.kind {
91 // `&[..., ref ident, ...]`
93 let span = element_pat.span.until(ident.span);
94 suggestions.push((span, String::new()));
100 if !suggestions.is_empty() {
103 NEEDLESS_BORROWED_REFERENCE,
105 "dereferencing a slice pattern where every element takes a reference",
109 let span = pat.span.until(sub_pat.span);
110 suggestions.push((span, String::new()));
112 diag.multipart_suggestion(
113 "try removing the `&` and `ref` parts",
115 Applicability::MachineApplicable,