Skip to content

Commit 46c3076

Browse files
committed
Fix needless_borrow not linting mutable reference
1 parent 6b4b77a commit 46c3076

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

clippy_lints/src/needless_borrow.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
104104
if e.span.from_expansion() {
105105
return;
106106
}
107-
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = e.kind {
107+
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, inner) = e.kind {
108108
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
109109
for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
110110
if let [Adjustment {
@@ -116,14 +116,20 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
116116
..
117117
}] = *adj3
118118
{
119+
let help_msg_ty = if matches!(mutability, Mutability::Not) {
120+
format!("&{}", ty)
121+
} else {
122+
format!("&mut {}", ty)
123+
};
124+
119125
span_lint_and_then(
120126
cx,
121127
NEEDLESS_BORROW,
122128
e.span,
123129
&format!(
124-
"this expression borrows a reference (`&{}`) that is immediately dereferenced \
130+
"this expression borrows a reference (`{}`) that is immediately dereferenced \
125131
by the compiler",
126-
ty
132+
help_msg_ty
127133
),
128134
|diag| {
129135
if let Some(snippet) = snippet_opt(cx, inner.span) {

0 commit comments

Comments
 (0)