Skip to content

Commit e94a2e7

Browse files
committed
New message for needless_borrow when a borrow could be replaced by auto-borrow
1 parent 3f4090f commit e94a2e7

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

clippy_lints/src/dereference.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ enum State {
145145
DerefedBorrow {
146146
count: usize,
147147
required_precedence: i8,
148+
msg: &'static str,
148149
},
149150
}
150151

@@ -259,20 +260,25 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
259260
// None => y
260261
// };
261262
// }
262-
let (required_refs, required_precedence) = if is_auto_borrow_position(parent, expr.hir_id) {
263-
(1, PREC_POSTFIX)
263+
let deref_msg =
264+
"this expression creates a reference which is immediately dereferenced by the compiler";
265+
let borrow_msg = "this expression borrows a value the compiler would automatically borrow";
266+
267+
let (required_refs, required_precedence, msg) = if is_auto_borrow_position(parent, expr.hir_id)
268+
{
269+
(1, PREC_POSTFIX, if deref_count == 1 { borrow_msg } else { deref_msg })
264270
} else if let Some(&Adjust::Borrow(AutoBorrow::Ref(_, mutability))) =
265271
next_adjust.map(|a| &a.kind)
266272
{
267273
if matches!(mutability, AutoBorrowMutability::Mut { .. })
268274
&& !is_auto_reborrow_position(parent)
269275
{
270-
(3, 0)
276+
(3, 0, deref_msg)
271277
} else {
272-
(2, 0)
278+
(2, 0, deref_msg)
273279
}
274280
} else {
275-
(2, 0)
281+
(2, 0, deref_msg)
276282
};
277283

278284
if deref_count >= required_refs {
@@ -282,6 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
282288
// can't be removed without breaking the code. See earlier comment.
283289
count: deref_count - required_refs,
284290
required_precedence,
291+
msg,
285292
},
286293
StateData { span: expr.span },
287294
));
@@ -319,6 +326,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
319326
State::DerefedBorrow {
320327
count,
321328
required_precedence,
329+
msg,
322330
},
323331
data,
324332
)),
@@ -328,6 +336,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
328336
State::DerefedBorrow {
329337
count: count - 1,
330338
required_precedence,
339+
msg,
331340
},
332341
data,
333342
));
@@ -624,15 +633,17 @@ fn report(cx: &LateContext<'_>, expr: &Expr<'_>, state: State, data: StateData)
624633
);
625634
},
626635
State::DerefedBorrow {
627-
required_precedence, ..
636+
required_precedence,
637+
msg,
638+
..
628639
} => {
629640
let mut app = Applicability::MachineApplicable;
630641
let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
631642
span_lint_and_sugg(
632643
cx,
633644
NEEDLESS_BORROW,
634645
data.span,
635-
"this expression creates a reference which is immediately dereferenced by the compiler",
646+
msg,
636647
"change this to",
637648
if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
638649
format!("({})", snip)

tests/ui/needless_borrow.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ error: this expression creates a reference which is immediately dereferenced by
8484
LL | let y: &mut i32 = &mut &mut x;
8585
| ^^^^^^^^^^^ help: change this to: `x`
8686

87-
error: this expression creates a reference which is immediately dereferenced by the compiler
87+
error: this expression borrows a value the compiler would automatically borrow
8888
--> $DIR/needless_borrow.rs:67:13
8989
|
9090
LL | let _ = (&s).len();
9191
| ^^^^ help: change this to: `s`
9292

93-
error: this expression creates a reference which is immediately dereferenced by the compiler
93+
error: this expression borrows a value the compiler would automatically borrow
9494
--> $DIR/needless_borrow.rs:68:13
9595
|
9696
LL | let _ = (&s).capacity();
@@ -102,13 +102,13 @@ error: this expression creates a reference which is immediately dereferenced by
102102
LL | let _ = (&&s).capacity();
103103
| ^^^^^ help: change this to: `s`
104104

105-
error: this expression creates a reference which is immediately dereferenced by the compiler
105+
error: this expression borrows a value the compiler would automatically borrow
106106
--> $DIR/needless_borrow.rs:72:13
107107
|
108108
LL | let _ = (&x).0;
109109
| ^^^^ help: change this to: `x`
110110

111-
error: this expression creates a reference which is immediately dereferenced by the compiler
111+
error: this expression borrows a value the compiler would automatically borrow
112112
--> $DIR/needless_borrow.rs:74:22
113113
|
114114
LL | let _ = unsafe { (&*x).0 };

0 commit comments

Comments
 (0)