- span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg);
+ let snippet = snippet(cx, expr.span, "..");
+ let name_of_cast_from = snippet.split(" as").next().unwrap_or("..");
+ let suggestion = format!("{cast_to}::try_from({name_of_cast_from})");
+
+ span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
+ diag.help("if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...");
+ diag.span_suggestion_with_style(
+ expr.span,
+ "... or use `try_from` and handle the error accordingly",
+ suggestion,
+ Applicability::Unspecified,
+ // always show the suggestion in a separate line
+ SuggestionStyle::ShowAlways,
+ );
+ });