Skip to content

Avoid linting unsized mutable reference #9835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions clippy_lints/src/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
expr.span,
"generally you want to avoid `&mut &mut _` if possible",
);
} else if let ty::Ref(_, _, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind() {
span_lint(
self.cx,
MUT_MUT,
expr.span,
"this expression mutably borrows a mutable reference. Consider reborrowing",
);
} else if let ty::Ref(_, ty, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind() {
if ty.peel_refs().is_sized(self.cx.tcx.at(expr.span), self.cx.param_env) {
span_lint(
self.cx,
MUT_MUT,
expr.span,
"this expression mutably borrows a mutable reference. Consider reborrowing",
);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,20 @@ fn issue6922() {
// do not lint from an external macro
mut_mut!();
}

mod issue9035 {
use std::fmt::Display;

struct Foo<'a> {
inner: &'a mut dyn Display,
}

impl Foo<'_> {
fn foo(&mut self) {
let hlp = &mut self.inner;
bar(hlp);
}
}

fn bar(_: &mut impl Display) {}
}