|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 |
| -use clippy_utils::source::snippet; |
| 2 | +use clippy_utils::sugg::{self, Sugg}; |
3 | 3 | use clippy_utils::sym;
|
4 | 4 | use rustc_errors::Applicability;
|
5 | 5 | use rustc_hir::{Expr, ExprKind};
|
@@ -74,18 +74,22 @@ impl<'tcx> LateLintPass<'tcx> for CoerceContainerToAny {
|
74 | 74 | }
|
75 | 75 |
|
76 | 76 | // ... that's probably not intended.
|
77 |
| - let (span, deref_count) = match e.kind { |
| 77 | + let (target_expr, deref_count) = match e.kind { |
78 | 78 | // 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), |
81 | 81 | };
|
| 82 | + let sugg = sugg::make_unop( |
| 83 | + &format!("&{}", str::repeat("*", deref_count)), |
| 84 | + Sugg::hir(cx, target_expr, ".."), |
| 85 | + ); |
82 | 86 | span_lint_and_sugg(
|
83 | 87 | cx,
|
84 | 88 | COERCE_CONTAINER_TO_ANY,
|
85 | 89 | e.span,
|
86 | 90 | format!("coercing `{expr_ty}` to `&dyn Any`"),
|
87 | 91 | "consider dereferencing",
|
88 |
| - format!("&{}{}", str::repeat("*", deref_count), snippet(cx, span, "..")), |
| 92 | + sugg.to_string(), |
89 | 93 | Applicability::MaybeIncorrect,
|
90 | 94 | );
|
91 | 95 | }
|
|
0 commit comments