Skip to content

Commit 8e285d6

Browse files
committed
Use precedence-aware formatting for coerce_container_to_any suggestions
1 parent a2b2345 commit 8e285d6

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

clippy_lints/src/coerce_container_to_any.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::sugg::{self, Sugg};
33
use clippy_utils::sym;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
@@ -74,18 +74,22 @@ impl<'tcx> LateLintPass<'tcx> for CoerceContainerToAny {
7474
}
7575

7676
// ... that's probably not intended.
77-
let (span, deref_count) = match e.kind {
77+
let (target_expr, deref_count) = match e.kind {
7878
// If `e` was already an `&` expression, skip `*&` in the suggestion
79-
ExprKind::AddrOf(_, _, referent) => (referent.span, depth),
80-
_ => (e.span, depth + 1),
79+
ExprKind::AddrOf(_, _, referent) => (referent, depth),
80+
_ => (e, depth + 1),
8181
};
82+
let sugg = sugg::make_unop(
83+
&format!("&{}", str::repeat("*", deref_count)),
84+
Sugg::hir(cx, target_expr, ".."),
85+
);
8286
span_lint_and_sugg(
8387
cx,
8488
COERCE_CONTAINER_TO_ANY,
8589
e.span,
8690
format!("coercing `{expr_ty}` to `&dyn Any`"),
8791
"consider dereferencing",
88-
format!("&{}{}", str::repeat("*", deref_count), snippet(cx, span, "..")),
92+
sugg.to_string(),
8993
Applicability::MaybeIncorrect,
9094
);
9195
}

0 commit comments

Comments
 (0)