Skip to content

Commit

Permalink
Fix needless_borrow not linting mutable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
dswij committed Sep 13, 2021
1 parent 6b4b77a commit 46c3076
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clippy_lints/src/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
if e.span.from_expansion() {
return;
}
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = e.kind {
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, inner) = e.kind {
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
if let [Adjustment {
Expand All @@ -116,14 +116,20 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
..
}] = *adj3
{
let help_msg_ty = if matches!(mutability, Mutability::Not) {
format!("&{}", ty)
} else {
format!("&mut {}", ty)
};

span_lint_and_then(
cx,
NEEDLESS_BORROW,
e.span,
&format!(
"this expression borrows a reference (`&{}`) that is immediately dereferenced \
"this expression borrows a reference (`{}`) that is immediately dereferenced \
by the compiler",
ty
help_msg_ty
),
|diag| {
if let Some(snippet) = snippet_opt(cx, inner.span) {
Expand Down

0 comments on commit 46c3076

Please sign in to comment.