Skip to content

Commit 483b7ac

Browse files
committed
Auto merge of #10038 - xFrednet:9231-sugg-try-from, r=Jarcho
`cast_possible_truncation` Suggest TryFrom when truncation possible This fixes the last issues from #9664 as the author seems to be inactive. The PR author was sadly kept during the rebase, due to the conflict resolution. IDK if it's worth it do to a full review, I only added the last commit, everything else remained the same, besides a rebase. --- changelog: Sugg: [`cast_possible_truncation`]: Now suggests using `try_from` or allowing the lint [#10038](#10038) <!-- changelog_checked --> closes: #9231
2 parents 91c8ecc + 5cb6246 commit 483b7ac

File tree

5 files changed

+244
-25
lines changed

5 files changed

+244
-25
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use clippy_utils::consts::{constant, Constant};
2-
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::expr_or_init;
4+
use clippy_utils::source::snippet;
45
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6+
use rustc_errors::{Applicability, SuggestionStyle};
57
use rustc_hir::def::{DefKind, Res};
68
use rustc_hir::{BinOpKind, Expr, ExprKind};
79
use rustc_lint::LateContext;
810
use rustc_middle::ty::{self, FloatTy, Ty};
11+
use rustc_span::Span;
912
use rustc_target::abi::IntegerType;
1013

1114
use super::{utils, CAST_ENUM_TRUNCATION, CAST_POSSIBLE_TRUNCATION};
@@ -74,7 +77,14 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
7477
}
7578
}
7679

77-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
80+
pub(super) fn check(
81+
cx: &LateContext<'_>,
82+
expr: &Expr<'_>,
83+
cast_expr: &Expr<'_>,
84+
cast_from: Ty<'_>,
85+
cast_to: Ty<'_>,
86+
cast_to_span: Span,
87+
) {
7888
let msg = match (cast_from.kind(), cast_to.is_integral()) {
7989
(ty::Int(_) | ty::Uint(_), true) => {
8090
let from_nbits = apply_reductions(
@@ -139,7 +149,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
139149
);
140150
return;
141151
}
142-
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}",)
152+
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}")
143153
},
144154

145155
(ty::Float(_), true) => {
@@ -153,5 +163,19 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
153163
_ => return,
154164
};
155165

156-
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg);
166+
let name_of_cast_from = snippet(cx, cast_expr.span, "..");
167+
let cast_to_snip = snippet(cx, cast_to_span, "..");
168+
let suggestion = format!("{cast_to_snip}::try_from({name_of_cast_from})");
169+
170+
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
171+
diag.help("if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...");
172+
diag.span_suggestion_with_style(
173+
expr.span,
174+
"... or use `try_from` and handle the error accordingly",
175+
suggestion,
176+
Applicability::Unspecified,
177+
// always show the suggestion in a separate line
178+
SuggestionStyle::ShowAlways,
179+
);
180+
});
157181
}

clippy_lints/src/casts/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ declare_clippy_lint! {
8080
/// ### What it does
8181
/// Checks for casts between numerical types that may
8282
/// truncate large values. This is expected behavior, so the cast is `Allow` by
83-
/// default.
83+
/// default. It suggests user either explicitly ignore the lint,
84+
/// or use `try_from()` and handle the truncation, default, or panic explicitly.
8485
///
8586
/// ### Why is this bad?
8687
/// In some problem domains, it is good practice to avoid
@@ -93,6 +94,21 @@ declare_clippy_lint! {
9394
/// x as u8
9495
/// }
9596
/// ```
97+
/// Use instead:
98+
/// ```
99+
/// fn as_u8(x: u64) -> u8 {
100+
/// if let Ok(x) = u8::try_from(x) {
101+
/// x
102+
/// } else {
103+
/// todo!();
104+
/// }
105+
/// }
106+
/// // Or
107+
/// #[allow(clippy::cast_possible_truncation)]
108+
/// fn as_u16(x: u64) -> u16 {
109+
/// x as u16
110+
/// }
111+
/// ```
96112
#[clippy::version = "pre 1.29.0"]
97113
pub CAST_POSSIBLE_TRUNCATION,
98114
pedantic,
@@ -712,7 +728,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
712728
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_expr, cast_from, cast_to);
713729

714730
if cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
715-
cast_possible_truncation::check(cx, expr, cast_expr, cast_from, cast_to);
731+
cast_possible_truncation::check(cx, expr, cast_expr, cast_from, cast_to, cast_to_hir.span);
716732
if cast_from.is_numeric() {
717733
cast_possible_wrap::check(cx, expr, cast_from, cast_to);
718734
cast_precision_loss::check(cx, expr, cast_from, cast_to);

tests/ui/cast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn main() {
2828
1i32 as u8;
2929
1f64 as isize;
3030
1f64 as usize;
31+
1f32 as u32 as u16;
3132
// Test clippy::cast_possible_wrap
3233
1u8 as i8;
3334
1u16 as i16;

0 commit comments

Comments
 (0)