Skip to content

Commit dc1b9ce

Browse files
committed
Fix reference mutability in coerce_container_to_any diagnostics
1 parent 8e285d6 commit dc1b9ce

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

clippy_lints/src/coerce_container_to_any.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ impl<'tcx> LateLintPass<'tcx> for CoerceContainerToAny {
7979
ExprKind::AddrOf(_, _, referent) => (referent, depth),
8080
_ => (e, depth + 1),
8181
};
82+
let ty::Ref(_, _, mutability) = *cx.typeck_results().expr_ty_adjusted(e).kind() else {
83+
return;
84+
};
8285
let sugg = sugg::make_unop(
83-
&format!("&{}", str::repeat("*", deref_count)),
86+
&format!("{}{}", mutability.ref_prefix_str(), str::repeat("*", deref_count)),
8487
Sugg::hir(cx, target_expr, ".."),
8588
);
8689
span_lint_and_sugg(
8790
cx,
8891
COERCE_CONTAINER_TO_ANY,
8992
e.span,
90-
format!("coercing `{expr_ty}` to `&dyn Any`"),
93+
format!("coercing `{expr_ty}` to `{}dyn Any`", mutability.ref_prefix_str()),
9194
"consider dereferencing",
9295
sugg.to_string(),
9396
Applicability::MaybeIncorrect,

tests/ui/coerce_container_to_any.fixed

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::any::Any;
44

55
fn main() {
6-
let x: Box<dyn Any> = Box::new(());
6+
let mut x: Box<dyn Any> = Box::new(());
77
let ref_x = &x;
88

99
f(&*x);
@@ -15,9 +15,16 @@ fn main() {
1515
let _: &dyn Any = &*x;
1616
//~^ coerce_container_to_any
1717

18+
let _: &dyn Any = &*x;
19+
//~^ coerce_container_to_any
20+
21+
let _: &mut dyn Any = &mut *x;
22+
//~^ coerce_container_to_any
23+
1824
f(&42);
1925
f(&Box::new(()));
2026
f(&Box::new(Box::new(())));
27+
let ref_x = &x;
2128
f(&**ref_x);
2229
f(&*x);
2330
let _: &dyn Any = &*x;

tests/ui/coerce_container_to_any.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::any::Any;
44

55
fn main() {
6-
let x: Box<dyn Any> = Box::new(());
6+
let mut x: Box<dyn Any> = Box::new(());
77
let ref_x = &x;
88

99
f(&x);
@@ -15,9 +15,16 @@ fn main() {
1515
let _: &dyn Any = &x;
1616
//~^ coerce_container_to_any
1717

18+
let _: &dyn Any = &mut x;
19+
//~^ coerce_container_to_any
20+
21+
let _: &mut dyn Any = &mut x;
22+
//~^ coerce_container_to_any
23+
1824
f(&42);
1925
f(&Box::new(()));
2026
f(&Box::new(Box::new(())));
27+
let ref_x = &x;
2128
f(&**ref_x);
2229
f(&*x);
2330
let _: &dyn Any = &*x;

tests/ui/coerce_container_to_any.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,17 @@ error: coercing `&std::boxed::Box<dyn std::any::Any>` to `&dyn Any`
1919
LL | let _: &dyn Any = &x;
2020
| ^^ help: consider dereferencing: `&*x`
2121

22-
error: aborting due to 3 previous errors
22+
error: coercing `&mut std::boxed::Box<dyn std::any::Any>` to `&dyn Any`
23+
--> tests/ui/coerce_container_to_any.rs:18:23
24+
|
25+
LL | let _: &dyn Any = &mut x;
26+
| ^^^^^^ help: consider dereferencing: `&*x`
27+
28+
error: coercing `&mut std::boxed::Box<dyn std::any::Any>` to `&mut dyn Any`
29+
--> tests/ui/coerce_container_to_any.rs:21:27
30+
|
31+
LL | let _: &mut dyn Any = &mut x;
32+
| ^^^^^^ help: consider dereferencing: `&mut *x`
33+
34+
error: aborting due to 5 previous errors
2335

0 commit comments

Comments
 (0)