From 93edc127a0d9a5ecd403a2f72ff6c02c5fadb696 Mon Sep 17 00:00:00 2001 From: koka Date: Sat, 12 Nov 2022 20:31:25 +0900 Subject: [PATCH] Avoid lint to unsized mutable reference --- clippy_lints/src/mut_mut.rs | 16 +++++++++------- tests/ui/mut_mut.rs | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/mut_mut.rs b/clippy_lints/src/mut_mut.rs index cb16f00047a..ae17327fd2a 100644 --- a/clippy_lints/src/mut_mut.rs +++ b/clippy_lints/src/mut_mut.rs @@ -68,13 +68,15 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) { 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", + ); + } } } } diff --git a/tests/ui/mut_mut.rs b/tests/ui/mut_mut.rs index ac8fd9d8fb0..ee3a856566c 100644 --- a/tests/ui/mut_mut.rs +++ b/tests/ui/mut_mut.rs @@ -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) {} +}