Skip to content

Commit 029c326

Browse files
committed
Auto merge of #7105 - Jarcho:needless_borrow_pat, r=camsteffen
fix `needless_borrow` suggestion fixes: #2610 While I'm working on this, should needless_borrow be split into two? One lint for expressions and another for patterns. In expression it only lints when the compiler inserts a dereference, but for patterns it's whenever a double reference is created. I think at least the case where a double reference is needed should be split into a new lint as it's not 'needless', it can just be done without a ref binding. For illustration: ```rust fn foo(x: &&str) {} match Some("test") { // ref binding is useless here Some(ref x) => *x, _ => (), } match Some("test") { // ref binding is useless here Some(ref x) => x.len(), _ => (), } match Some("test") { // double reference is needed, but could be `Some(x) => foo(&x)` Some(ref x) => foo(x), _ => (), } ``` changelog: Improve the suggestion for `needless_borrow` in patterns to change all usage sites as needed. changelog: Add lint `ref_binding_to_reference`
2 parents 853f593 + 6d4dc35 commit 029c326

13 files changed

+621
-117
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,7 @@ Released 2018-09-13
26222622
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
26232623
[`redundant_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_slicing
26242624
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
2625+
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
26252626
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
26262627
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
26272628
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
841841
needless_bool::BOOL_COMPARISON,
842842
needless_bool::NEEDLESS_BOOL,
843843
needless_borrow::NEEDLESS_BORROW,
844+
needless_borrow::REF_BINDING_TO_REFERENCE,
844845
needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
845846
needless_continue::NEEDLESS_CONTINUE,
846847
needless_for_each::NEEDLESS_FOR_EACH,
@@ -1116,6 +1117,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11161117
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
11171118
LintId::of(mut_mut::MUT_MUT),
11181119
LintId::of(needless_bitwise_bool::NEEDLESS_BITWISE_BOOL),
1120+
LintId::of(needless_borrow::REF_BINDING_TO_REFERENCE),
11191121
LintId::of(needless_continue::NEEDLESS_CONTINUE),
11201122
LintId::of(needless_for_each::NEEDLESS_FOR_EACH),
11211123
LintId::of(needless_pass_by_value::NEEDLESS_PASS_BY_VALUE),

clippy_lints/src/needless_borrow.rs

Lines changed: 174 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
//! This lint is **warn** by default
44
55
use clippy_utils::diagnostics::span_lint_and_then;
6-
use clippy_utils::is_automatically_derived;
7-
use clippy_utils::source::snippet_opt;
6+
use clippy_utils::source::{snippet_opt, snippet_with_applicability, snippet_with_context};
7+
use clippy_utils::{get_parent_expr, in_macro, path_to_local};
88
use if_chain::if_chain;
9+
use rustc_ast::util::parser::PREC_POSTFIX;
10+
use rustc_data_structures::fx::FxIndexMap;
911
use rustc_errors::Applicability;
10-
use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, Item, Mutability, Pat, PatKind};
12+
use rustc_hir::{BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, PatKind, UnOp};
1113
use rustc_lint::{LateContext, LateLintPass};
1214
use rustc_middle::ty;
1315
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
1416
use rustc_session::{declare_tool_lint, impl_lint_pass};
15-
use rustc_span::def_id::LocalDefId;
17+
use rustc_span::Span;
1618

1719
declare_clippy_lint! {
1820
/// **What it does:** Checks for address of operations (`&`) that are going to
@@ -36,16 +38,66 @@ declare_clippy_lint! {
3638
"taking a reference that is going to be automatically dereferenced"
3739
}
3840

41+
declare_clippy_lint! {
42+
/// **What it does:** Checks for `ref` bindings which create a reference to a reference.
43+
///
44+
/// **Why is this bad?** The address-of operator at the use site is clearer about the need for a reference.
45+
///
46+
/// **Known problems:** None.
47+
///
48+
/// **Example:**
49+
/// ```rust
50+
/// // Bad
51+
/// let x = Some("");
52+
/// if let Some(ref x) = x {
53+
/// // use `x` here
54+
/// }
55+
///
56+
/// // Good
57+
/// let x = Some("");
58+
/// if let Some(x) = x {
59+
/// // use `&x` here
60+
/// }
61+
/// ```
62+
pub REF_BINDING_TO_REFERENCE,
63+
pedantic,
64+
"`ref` binding to a reference"
65+
}
66+
67+
impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW, REF_BINDING_TO_REFERENCE]);
3968
#[derive(Default)]
4069
pub struct NeedlessBorrow {
41-
derived_item: Option<LocalDefId>,
70+
/// The body the first local was found in. Used to emit lints when the traversal of the body has
71+
/// been finished. Note we can't lint at the end of every body as they can be nested within each
72+
/// other.
73+
current_body: Option<BodyId>,
74+
/// The list of locals currently being checked by the lint.
75+
/// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
76+
/// This is needed for or patterns where one of the branches can be linted, but another can not
77+
/// be.
78+
///
79+
/// e.g. `m!(x) | Foo::Bar(ref x)`
80+
ref_locals: FxIndexMap<HirId, Option<RefPat>>,
4281
}
4382

44-
impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]);
83+
struct RefPat {
84+
/// Whether every usage of the binding is dereferenced.
85+
always_deref: bool,
86+
/// The spans of all the ref bindings for this local.
87+
spans: Vec<Span>,
88+
/// The applicability of this suggestion.
89+
app: Applicability,
90+
/// All the replacements which need to be made.
91+
replacements: Vec<(Span, String)>,
92+
}
4593

4694
impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
4795
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
48-
if e.span.from_expansion() || self.derived_item.is_some() {
96+
if let Some(local) = path_to_local(e) {
97+
self.check_local_usage(cx, e, local);
98+
}
99+
100+
if e.span.from_expansion() {
49101
return;
50102
}
51103
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = e.kind {
@@ -85,50 +137,131 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
85137
}
86138
}
87139
}
140+
88141
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
89-
if pat.span.from_expansion() || self.derived_item.is_some() {
90-
return;
142+
if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
143+
if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
144+
// This binding id has been seen before. Add this pattern to the list of changes.
145+
if let Some(prev_pat) = opt_prev_pat {
146+
if in_macro(pat.span) {
147+
// Doesn't match the context of the previous pattern. Can't lint here.
148+
*opt_prev_pat = None;
149+
} else {
150+
prev_pat.spans.push(pat.span);
151+
prev_pat.replacements.push((
152+
pat.span,
153+
snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
154+
.0
155+
.into(),
156+
));
157+
}
158+
}
159+
return;
160+
}
161+
162+
if_chain! {
163+
if !in_macro(pat.span);
164+
if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
165+
// only lint immutable refs, because borrowed `&mut T` cannot be moved out
166+
if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
167+
then {
168+
let mut app = Applicability::MachineApplicable;
169+
let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
170+
self.current_body = self.current_body.or(cx.enclosing_body);
171+
self.ref_locals.insert(
172+
id,
173+
Some(RefPat {
174+
always_deref: true,
175+
spans: vec![pat.span],
176+
app,
177+
replacements: vec![(pat.span, snip.into())],
178+
}),
179+
);
180+
}
181+
}
91182
}
92-
if_chain! {
93-
if let PatKind::Binding(BindingAnnotation::Ref, .., name, _) = pat.kind;
94-
if let ty::Ref(_, tam, mutbl) = *cx.typeck_results().pat_ty(pat).kind();
95-
if mutbl == Mutability::Not;
96-
if let ty::Ref(_, _, mutbl) = *tam.kind();
97-
// only lint immutable refs, because borrowed `&mut T` cannot be moved out
98-
if mutbl == Mutability::Not;
99-
then {
183+
}
184+
185+
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
186+
if Some(body.id()) == self.current_body {
187+
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
188+
let replacements = pat.replacements;
189+
let app = pat.app;
100190
span_lint_and_then(
101191
cx,
102-
NEEDLESS_BORROW,
103-
pat.span,
192+
if pat.always_deref {
193+
NEEDLESS_BORROW
194+
} else {
195+
REF_BINDING_TO_REFERENCE
196+
},
197+
pat.spans,
104198
"this pattern creates a reference to a reference",
105199
|diag| {
106-
if let Some(snippet) = snippet_opt(cx, name.span) {
107-
diag.span_suggestion(
108-
pat.span,
109-
"change this to",
110-
snippet,
111-
Applicability::MachineApplicable,
112-
);
113-
}
114-
}
200+
diag.multipart_suggestion("try this", replacements, app);
201+
},
115202
)
116203
}
204+
self.current_body = None;
117205
}
118206
}
119-
120-
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
121-
let attrs = cx.tcx.hir().attrs(item.hir_id());
122-
if is_automatically_derived(attrs) {
123-
debug_assert!(self.derived_item.is_none());
124-
self.derived_item = Some(item.def_id);
125-
}
126-
}
127-
128-
fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
129-
if let Some(id) = self.derived_item {
130-
if item.def_id == id {
131-
self.derived_item = None;
207+
}
208+
impl NeedlessBorrow {
209+
fn check_local_usage(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, local: HirId) {
210+
if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
211+
if let Some(pat) = outer_pat {
212+
// Check for auto-deref
213+
if !matches!(
214+
cx.typeck_results().expr_adjustments(e),
215+
[
216+
Adjustment {
217+
kind: Adjust::Deref(_),
218+
..
219+
},
220+
Adjustment {
221+
kind: Adjust::Deref(_),
222+
..
223+
},
224+
..
225+
]
226+
) {
227+
match get_parent_expr(cx, e) {
228+
// Field accesses are the same no matter the number of references.
229+
Some(Expr {
230+
kind: ExprKind::Field(..),
231+
..
232+
}) => (),
233+
Some(&Expr {
234+
span,
235+
kind: ExprKind::Unary(UnOp::Deref, _),
236+
..
237+
}) if !in_macro(span) => {
238+
// Remove explicit deref.
239+
let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
240+
pat.replacements.push((span, snip.into()));
241+
},
242+
Some(parent) if !in_macro(parent.span) => {
243+
// Double reference might be needed at this point.
244+
if parent.precedence().order() == PREC_POSTFIX {
245+
// Parentheses would be needed here, don't lint.
246+
*outer_pat = None;
247+
} else {
248+
pat.always_deref = false;
249+
let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
250+
pat.replacements.push((e.span, format!("&{}", snip)));
251+
}
252+
},
253+
_ if !in_macro(e.span) => {
254+
// Double reference might be needed at this point.
255+
pat.always_deref = false;
256+
let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
257+
pat.replacements.push((e.span, format!("&{}", snip)));
258+
},
259+
// Edge case for macros. The span of the identifier will usually match the context of the
260+
// binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
261+
// macros
262+
_ => *outer_pat = None,
263+
}
264+
}
132265
}
133266
}
134267
}

clippy_utils/src/higher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option<Range<'a>> {
7070
limits: ast::RangeLimits::Closed,
7171
})
7272
},
73-
hir::ExprKind::Struct(ref path, ref fields, None) => match path {
73+
hir::ExprKind::Struct(path, ref fields, None) => match path {
7474
hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range {
7575
start: None,
7676
end: None,

clippy_utils/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,12 +1254,12 @@ pub fn match_function_call<'tcx>(
12541254
path: &[&str],
12551255
) -> Option<&'tcx [Expr<'tcx>]> {
12561256
if_chain! {
1257-
if let ExprKind::Call(ref fun, ref args) = expr.kind;
1257+
if let ExprKind::Call(ref fun, args) = expr.kind;
12581258
if let ExprKind::Path(ref qpath) = fun.kind;
12591259
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
12601260
if match_def_path(cx, fun_def_id, path);
12611261
then {
1262-
return Some(&args)
1262+
return Some(args)
12631263
}
12641264
};
12651265
None

clippy_utils/src/visitors.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,34 +189,21 @@ impl<'v> Visitor<'v> for LocalUsedVisitor<'v> {
189189
}
190190
}
191191

192+
/// A type which can be visited.
192193
pub trait Visitable<'tcx> {
193-
fn visit<V: Visitor<'tcx>>(self, v: &mut V);
194+
/// Calls the corresponding `visit_*` function on the visitor.
195+
fn visit<V: Visitor<'tcx>>(self, visitor: &mut V);
194196
}
195-
impl Visitable<'tcx> for &'tcx Expr<'tcx> {
196-
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
197-
v.visit_expr(self)
198-
}
199-
}
200-
impl Visitable<'tcx> for &'tcx Block<'tcx> {
201-
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
202-
v.visit_block(self)
203-
}
204-
}
205-
impl<'tcx> Visitable<'tcx> for &'tcx Stmt<'tcx> {
206-
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
207-
v.visit_stmt(self)
208-
}
209-
}
210-
impl<'tcx> Visitable<'tcx> for &'tcx Body<'tcx> {
211-
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
212-
v.visit_body(self)
213-
}
214-
}
215-
impl<'tcx> Visitable<'tcx> for &'tcx Arm<'tcx> {
216-
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
217-
v.visit_arm(self)
218-
}
197+
macro_rules! visitable_ref {
198+
($t:ident, $f:ident) => {
199+
impl Visitable<'tcx> for &'tcx $t<'tcx> {
200+
fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
201+
visitor.$f(self);
202+
}
203+
}
204+
};
219205
}
206+
visitable_ref!(Block, visit_block);
220207

221208
/// Calls the given function for each break expression.
222209
pub fn visit_break_exprs<'tcx>(

tests/ui/needless_borrow.fixed

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fn main() {
1818
let vec = Vec::new();
1919
let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
2020
h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
21-
if let Some(cake) = Some(&5) {}
2221
let garbl = match 42 {
2322
44 => &a,
2423
45 => {
@@ -43,19 +42,3 @@ trait Trait {}
4342
impl<'a> Trait for &'a str {}
4443

4544
fn h(_: &dyn Trait) {}
46-
#[warn(clippy::needless_borrow)]
47-
#[allow(dead_code)]
48-
fn issue_1432() {
49-
let mut v = Vec::<String>::new();
50-
let _ = v.iter_mut().filter(|&ref a| a.is_empty());
51-
let _ = v.iter().filter(|&a| a.is_empty());
52-
53-
let _ = v.iter().filter(|&a| a.is_empty());
54-
}
55-
56-
#[allow(dead_code)]
57-
#[warn(clippy::needless_borrow)]
58-
#[derive(Debug)]
59-
enum Foo<'a> {
60-
Str(&'a str),
61-
}

0 commit comments

Comments
 (0)