Skip to content

Commit bc39ddc

Browse files
committed
remove suggesting for float to float conversion in [cast_possible_truncation]
1 parent 7253230 commit bc39ddc

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::consts::{constant, Constant};
2-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
2+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::expr_or_init;
44
use clippy_utils::source::snippet;
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
7-
use rustc_errors::{Applicability, SuggestionStyle};
7+
use rustc_errors::{Applicability, Diagnostic, SuggestionStyle};
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{BinOpKind, Expr, ExprKind};
1010
use rustc_lint::LateContext;
@@ -155,14 +155,7 @@ pub(super) fn check(
155155
},
156156

157157
(ty::Float(_), true) => {
158-
return span_lint_and_help(
159-
cx,
160-
CAST_POSSIBLE_TRUNCATION,
161-
expr.span,
162-
&format!("casting `{cast_from}` to `{cast_to}` may truncate the value"),
163-
None,
164-
help,
165-
);
158+
format!("casting `{cast_from}` to `{cast_to}` may truncate the value")
166159
},
167160

168161
(ty::Float(FloatTy::F64), false) if matches!(cast_to.kind(), &ty::Float(FloatTy::F32)) => {
@@ -172,22 +165,34 @@ pub(super) fn check(
172165
_ => return,
173166
};
174167

168+
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
169+
diag.help(help);
170+
if !cast_from.is_floating_point() {
171+
offer_suggestion(cx, expr, cast_expr, cast_to_span, diag);
172+
}
173+
});
174+
}
175+
176+
fn offer_suggestion(
177+
cx: &LateContext<'_>,
178+
expr: &Expr<'_>,
179+
cast_expr: &Expr<'_>,
180+
cast_to_span: Span,
181+
diag: &mut Diagnostic,
182+
) {
175183
let cast_to_snip = snippet(cx, cast_to_span, "..");
176184
let suggestion = if cast_to_snip == "_" {
177185
format!("{}.try_into()", Sugg::hir(cx, cast_expr, "..").maybe_par())
178186
} else {
179187
format!("{cast_to_snip}::try_from({})", Sugg::hir(cx, cast_expr, ".."))
180188
};
181189

182-
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
183-
diag.help(help);
184-
diag.span_suggestion_with_style(
185-
expr.span,
186-
"... or use `try_from` and handle the error accordingly",
187-
suggestion,
188-
Applicability::Unspecified,
189-
// always show the suggestion in a separate line
190-
SuggestionStyle::ShowAlways,
191-
);
192-
});
190+
diag.span_suggestion_with_style(
191+
expr.span,
192+
"... or use `try_from` and handle the error accordingly",
193+
suggestion,
194+
Applicability::Unspecified,
195+
// always show the suggestion in a separate line
196+
SuggestionStyle::ShowAlways,
197+
);
193198
}

tests/ui/cast.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ LL | 1f64 as f32;
6868
| ^^^^^^^^^^^
6969
|
7070
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
71-
help: ... or use `try_from` and handle the error accordingly
72-
|
73-
LL | f32::try_from(1f64);
74-
| ~~~~~~~~~~~~~~~~~~~
7571

7672
error: casting `i32` to `i8` may truncate the value
7773
--> $DIR/cast.rs:27:5

0 commit comments

Comments
 (0)