Skip to content

Commit 5ed64d4

Browse files
committed
Auto merge of #10496 - J-ZhengLi:issue_10366, r=Alexendoo
fix [`cast_possible_truncation`] offering wrong suggestion for casting float to integer fixes: #10366 --- changelog: [`cast_possible_truncation`] Fix incorrect suggestions when casting from float types or to `_`
2 parents 27edc2a + 52c4dc6 commit 5ed64d4

File tree

3 files changed

+100
-60
lines changed

3 files changed

+100
-60
lines changed

Diff for: clippy_lints/src/casts/cast_possible_truncation.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::expr_or_init;
44
use clippy_utils::source::snippet;
5+
use clippy_utils::sugg::Sugg;
56
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6-
use rustc_errors::{Applicability, SuggestionStyle};
7+
use rustc_errors::{Applicability, Diagnostic, SuggestionStyle};
78
use rustc_hir::def::{DefKind, Res};
89
use rustc_hir::{BinOpKind, Expr, ExprKind};
910
use rustc_lint::LateContext;
@@ -163,19 +164,34 @@ pub(super) fn check(
163164
_ => return,
164165
};
165166

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-
170167
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
171168
diag.help("if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...");
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-
);
169+
if !cast_from.is_floating_point() {
170+
offer_suggestion(cx, expr, cast_expr, cast_to_span, diag);
171+
}
180172
});
181173
}
174+
175+
fn offer_suggestion(
176+
cx: &LateContext<'_>,
177+
expr: &Expr<'_>,
178+
cast_expr: &Expr<'_>,
179+
cast_to_span: Span,
180+
diag: &mut Diagnostic,
181+
) {
182+
let cast_to_snip = snippet(cx, cast_to_span, "..");
183+
let suggestion = if cast_to_snip == "_" {
184+
format!("{}.try_into()", Sugg::hir(cx, cast_expr, "..").maybe_par())
185+
} else {
186+
format!("{cast_to_snip}::try_from({})", Sugg::hir(cx, cast_expr, ".."))
187+
};
188+
189+
diag.span_suggestion_with_style(
190+
expr.span,
191+
"... or use `try_from` and handle the error accordingly",
192+
suggestion,
193+
Applicability::Unspecified,
194+
// always show the suggestion in a separate line
195+
SuggestionStyle::ShowAlways,
196+
);
197+
}

Diff for: tests/ui/cast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ fn main() {
2929
1f64 as isize;
3030
1f64 as usize;
3131
1f32 as u32 as u16;
32+
{
33+
let _x: i8 = 1i32 as _;
34+
1f32 as i32;
35+
1f64 as i32;
36+
1f32 as u8;
37+
}
3238
// Test clippy::cast_possible_wrap
3339
1u8 as i8;
3440
1u16 as i16;

Diff for: tests/ui/cast.stderr

+65-47
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ LL | 1f32 as i32;
4444
|
4545
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
4646
= note: `-D clippy::cast-possible-truncation` implied by `-D warnings`
47-
help: ... or use `try_from` and handle the error accordingly
48-
|
49-
LL | i32::try_from(1f32);
50-
| ~~~~~~~~~~~~~~~~~~~
5147

5248
error: casting `f32` to `u32` may truncate the value
5349
--> $DIR/cast.rs:25:5
@@ -56,10 +52,6 @@ LL | 1f32 as u32;
5652
| ^^^^^^^^^^^
5753
|
5854
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
59-
help: ... or use `try_from` and handle the error accordingly
60-
|
61-
LL | u32::try_from(1f32);
62-
| ~~~~~~~~~~~~~~~~~~~
6355

6456
error: casting `f32` to `u32` may lose the sign of the value
6557
--> $DIR/cast.rs:25:5
@@ -76,10 +68,6 @@ LL | 1f64 as f32;
7668
| ^^^^^^^^^^^
7769
|
7870
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
79-
help: ... or use `try_from` and handle the error accordingly
80-
|
81-
LL | f32::try_from(1f64);
82-
| ~~~~~~~~~~~~~~~~~~~
8371

8472
error: casting `i32` to `i8` may truncate the value
8573
--> $DIR/cast.rs:27:5
@@ -112,10 +100,6 @@ LL | 1f64 as isize;
112100
| ^^^^^^^^^^^^^
113101
|
114102
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
115-
help: ... or use `try_from` and handle the error accordingly
116-
|
117-
LL | isize::try_from(1f64);
118-
| ~~~~~~~~~~~~~~~~~~~~~
119103

120104
error: casting `f64` to `usize` may truncate the value
121105
--> $DIR/cast.rs:30:5
@@ -124,10 +108,6 @@ LL | 1f64 as usize;
124108
| ^^^^^^^^^^^^^
125109
|
126110
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
127-
help: ... or use `try_from` and handle the error accordingly
128-
|
129-
LL | usize::try_from(1f64);
130-
| ~~~~~~~~~~~~~~~~~~~~~
131111

132112
error: casting `f64` to `usize` may lose the sign of the value
133113
--> $DIR/cast.rs:30:5
@@ -154,63 +134,101 @@ LL | 1f32 as u32 as u16;
154134
| ^^^^^^^^^^^
155135
|
156136
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
157-
help: ... or use `try_from` and handle the error accordingly
158-
|
159-
LL | u32::try_from(1f32) as u16;
160-
| ~~~~~~~~~~~~~~~~~~~
161137

162138
error: casting `f32` to `u32` may lose the sign of the value
163139
--> $DIR/cast.rs:31:5
164140
|
165141
LL | 1f32 as u32 as u16;
166142
| ^^^^^^^^^^^
167143

144+
error: casting `i32` to `i8` may truncate the value
145+
--> $DIR/cast.rs:33:22
146+
|
147+
LL | let _x: i8 = 1i32 as _;
148+
| ^^^^^^^^^
149+
|
150+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
151+
help: ... or use `try_from` and handle the error accordingly
152+
|
153+
LL | let _x: i8 = 1i32.try_into();
154+
| ~~~~~~~~~~~~~~~
155+
156+
error: casting `f32` to `i32` may truncate the value
157+
--> $DIR/cast.rs:34:9
158+
|
159+
LL | 1f32 as i32;
160+
| ^^^^^^^^^^^
161+
|
162+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
163+
164+
error: casting `f64` to `i32` may truncate the value
165+
--> $DIR/cast.rs:35:9
166+
|
167+
LL | 1f64 as i32;
168+
| ^^^^^^^^^^^
169+
|
170+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
171+
172+
error: casting `f32` to `u8` may truncate the value
173+
--> $DIR/cast.rs:36:9
174+
|
175+
LL | 1f32 as u8;
176+
| ^^^^^^^^^^
177+
|
178+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
179+
180+
error: casting `f32` to `u8` may lose the sign of the value
181+
--> $DIR/cast.rs:36:9
182+
|
183+
LL | 1f32 as u8;
184+
| ^^^^^^^^^^
185+
168186
error: casting `u8` to `i8` may wrap around the value
169-
--> $DIR/cast.rs:33:5
187+
--> $DIR/cast.rs:39:5
170188
|
171189
LL | 1u8 as i8;
172190
| ^^^^^^^^^
173191
|
174192
= note: `-D clippy::cast-possible-wrap` implied by `-D warnings`
175193

176194
error: casting `u16` to `i16` may wrap around the value
177-
--> $DIR/cast.rs:34:5
195+
--> $DIR/cast.rs:40:5
178196
|
179197
LL | 1u16 as i16;
180198
| ^^^^^^^^^^^
181199

182200
error: casting `u32` to `i32` may wrap around the value
183-
--> $DIR/cast.rs:35:5
201+
--> $DIR/cast.rs:41:5
184202
|
185203
LL | 1u32 as i32;
186204
| ^^^^^^^^^^^
187205

188206
error: casting `u64` to `i64` may wrap around the value
189-
--> $DIR/cast.rs:36:5
207+
--> $DIR/cast.rs:42:5
190208
|
191209
LL | 1u64 as i64;
192210
| ^^^^^^^^^^^
193211

194212
error: casting `usize` to `isize` may wrap around the value
195-
--> $DIR/cast.rs:37:5
213+
--> $DIR/cast.rs:43:5
196214
|
197215
LL | 1usize as isize;
198216
| ^^^^^^^^^^^^^^^
199217

200218
error: casting `i32` to `u32` may lose the sign of the value
201-
--> $DIR/cast.rs:40:5
219+
--> $DIR/cast.rs:46:5
202220
|
203221
LL | -1i32 as u32;
204222
| ^^^^^^^^^^^^
205223

206224
error: casting `isize` to `usize` may lose the sign of the value
207-
--> $DIR/cast.rs:42:5
225+
--> $DIR/cast.rs:48:5
208226
|
209227
LL | -1isize as usize;
210228
| ^^^^^^^^^^^^^^^^
211229

212230
error: casting `i64` to `i8` may truncate the value
213-
--> $DIR/cast.rs:109:5
231+
--> $DIR/cast.rs:115:5
214232
|
215233
LL | (-99999999999i64).min(1) as i8; // should be linted because signed
216234
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -222,7 +240,7 @@ LL | i8::try_from((-99999999999i64).min(1)); // should be linted because sig
222240
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223241

224242
error: casting `u64` to `u8` may truncate the value
225-
--> $DIR/cast.rs:121:5
243+
--> $DIR/cast.rs:127:5
226244
|
227245
LL | 999999u64.clamp(0, 256) as u8; // should still be linted
228246
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -234,7 +252,7 @@ LL | u8::try_from(999999u64.clamp(0, 256)); // should still be linted
234252
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235253

236254
error: casting `main::E2` to `u8` may truncate the value
237-
--> $DIR/cast.rs:142:21
255+
--> $DIR/cast.rs:148:21
238256
|
239257
LL | let _ = self as u8;
240258
| ^^^^^^^^^^
@@ -246,15 +264,15 @@ LL | let _ = u8::try_from(self);
246264
| ~~~~~~~~~~~~~~~~~~
247265

248266
error: casting `main::E2::B` to `u8` will truncate the value
249-
--> $DIR/cast.rs:143:21
267+
--> $DIR/cast.rs:149:21
250268
|
251269
LL | let _ = Self::B as u8;
252270
| ^^^^^^^^^^^^^
253271
|
254272
= note: `-D clippy::cast-enum-truncation` implied by `-D warnings`
255273

256274
error: casting `main::E5` to `i8` may truncate the value
257-
--> $DIR/cast.rs:179:21
275+
--> $DIR/cast.rs:185:21
258276
|
259277
LL | let _ = self as i8;
260278
| ^^^^^^^^^^
@@ -266,13 +284,13 @@ LL | let _ = i8::try_from(self);
266284
| ~~~~~~~~~~~~~~~~~~
267285

268286
error: casting `main::E5::A` to `i8` will truncate the value
269-
--> $DIR/cast.rs:180:21
287+
--> $DIR/cast.rs:186:21
270288
|
271289
LL | let _ = Self::A as i8;
272290
| ^^^^^^^^^^^^^
273291

274292
error: casting `main::E6` to `i16` may truncate the value
275-
--> $DIR/cast.rs:194:21
293+
--> $DIR/cast.rs:200:21
276294
|
277295
LL | let _ = self as i16;
278296
| ^^^^^^^^^^^
@@ -284,7 +302,7 @@ LL | let _ = i16::try_from(self);
284302
| ~~~~~~~~~~~~~~~~~~~
285303

286304
error: casting `main::E7` to `usize` may truncate the value on targets with 32-bit wide pointers
287-
--> $DIR/cast.rs:209:21
305+
--> $DIR/cast.rs:215:21
288306
|
289307
LL | let _ = self as usize;
290308
| ^^^^^^^^^^^^^
@@ -296,7 +314,7 @@ LL | let _ = usize::try_from(self);
296314
| ~~~~~~~~~~~~~~~~~~~~~
297315

298316
error: casting `main::E10` to `u16` may truncate the value
299-
--> $DIR/cast.rs:250:21
317+
--> $DIR/cast.rs:256:21
300318
|
301319
LL | let _ = self as u16;
302320
| ^^^^^^^^^^^
@@ -308,28 +326,28 @@ LL | let _ = u16::try_from(self);
308326
| ~~~~~~~~~~~~~~~~~~~
309327

310328
error: casting `u32` to `u8` may truncate the value
311-
--> $DIR/cast.rs:258:13
329+
--> $DIR/cast.rs:264:13
312330
|
313331
LL | let c = (q >> 16) as u8;
314332
| ^^^^^^^^^^^^^^^
315333
|
316334
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
317335
help: ... or use `try_from` and handle the error accordingly
318336
|
319-
LL | let c = u8::try_from((q >> 16));
320-
| ~~~~~~~~~~~~~~~~~~~~~~~
337+
LL | let c = u8::try_from(q >> 16);
338+
| ~~~~~~~~~~~~~~~~~~~~~
321339

322340
error: casting `u32` to `u8` may truncate the value
323-
--> $DIR/cast.rs:261:13
341+
--> $DIR/cast.rs:267:13
324342
|
325343
LL | let c = (q / 1000) as u8;
326344
| ^^^^^^^^^^^^^^^^
327345
|
328346
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
329347
help: ... or use `try_from` and handle the error accordingly
330348
|
331-
LL | let c = u8::try_from((q / 1000));
332-
| ~~~~~~~~~~~~~~~~~~~~~~~~
349+
LL | let c = u8::try_from(q / 1000);
350+
| ~~~~~~~~~~~~~~~~~~~~~~
333351

334-
error: aborting due to 36 previous errors
352+
error: aborting due to 41 previous errors
335353

0 commit comments

Comments
 (0)