Skip to content

Commit 4a70f65

Browse files
committed
Address PR reivew
1 parent e6948c4 commit 4a70f65

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{BinOpKind, Expr, ExprKind};
99
use rustc_lint::LateContext;
1010
use rustc_middle::ty::{self, FloatTy, Ty};
11+
use rustc_span::Span;
1112
use rustc_target::abi::IntegerType;
1213

1314
use super::{utils, CAST_ENUM_TRUNCATION, CAST_POSSIBLE_TRUNCATION};
@@ -76,7 +77,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
7677
}
7778
}
7879

79-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
80+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, cast_to_span: Span) {
8081
let msg = match (cast_from.kind(), cast_to.is_integral()) {
8182
(ty::Int(_) | ty::Uint(_), true) => {
8283
let from_nbits = apply_reductions(
@@ -155,9 +156,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
155156
_ => return,
156157
};
157158

158-
let snippet = snippet(cx, expr.span, "..");
159-
let name_of_cast_from = snippet.split(" as").next().unwrap_or("..");
160-
let suggestion = format!("{cast_to}::try_from({name_of_cast_from})");
159+
let name_of_cast_from = snippet(cx, cast_expr.span, "..");
160+
let cast_to_snip = snippet(cx, cast_to_span, "..");
161+
let suggestion = format!("{cast_to_snip}::try_from({name_of_cast_from})");
161162

162163
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
163164
diag.help("if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...");

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
728728
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_expr, cast_from, cast_to);
729729

730730
if cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
731-
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);
732732
if cast_from.is_numeric() {
733733
cast_possible_wrap::check(cx, expr, cast_from, cast_to);
734734
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;

tests/ui/cast.stderr

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,52 +135,82 @@ error: casting `f64` to `usize` may lose the sign of the value
135135
LL | 1f64 as usize;
136136
| ^^^^^^^^^^^^^
137137

138+
error: casting `u32` to `u16` may truncate the value
139+
--> $DIR/cast.rs:31:5
140+
|
141+
LL | 1f32 as u32 as u16;
142+
| ^^^^^^^^^^^^^^^^^^
143+
|
144+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
145+
help: ... or use `try_from` and handle the error accordingly
146+
|
147+
LL | u16::try_from(1f32 as u32);
148+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
149+
150+
error: casting `f32` to `u32` may truncate the value
151+
--> $DIR/cast.rs:31:5
152+
|
153+
LL | 1f32 as u32 as u16;
154+
| ^^^^^^^^^^^
155+
|
156+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
157+
help: ... or use `try_from` and handle the error accordingly
158+
|
159+
LL | u32::try_from(1f32) as u16;
160+
| ~~~~~~~~~~~~~~~~~~~
161+
162+
error: casting `f32` to `u32` may lose the sign of the value
163+
--> $DIR/cast.rs:31:5
164+
|
165+
LL | 1f32 as u32 as u16;
166+
| ^^^^^^^^^^^
167+
138168
error: casting `u8` to `i8` may wrap around the value
139-
--> $DIR/cast.rs:32:5
169+
--> $DIR/cast.rs:33:5
140170
|
141171
LL | 1u8 as i8;
142172
| ^^^^^^^^^
143173
|
144174
= note: `-D clippy::cast-possible-wrap` implied by `-D warnings`
145175

146176
error: casting `u16` to `i16` may wrap around the value
147-
--> $DIR/cast.rs:33:5
177+
--> $DIR/cast.rs:34:5
148178
|
149179
LL | 1u16 as i16;
150180
| ^^^^^^^^^^^
151181

152182
error: casting `u32` to `i32` may wrap around the value
153-
--> $DIR/cast.rs:34:5
183+
--> $DIR/cast.rs:35:5
154184
|
155185
LL | 1u32 as i32;
156186
| ^^^^^^^^^^^
157187

158188
error: casting `u64` to `i64` may wrap around the value
159-
--> $DIR/cast.rs:35:5
189+
--> $DIR/cast.rs:36:5
160190
|
161191
LL | 1u64 as i64;
162192
| ^^^^^^^^^^^
163193

164194
error: casting `usize` to `isize` may wrap around the value
165-
--> $DIR/cast.rs:36:5
195+
--> $DIR/cast.rs:37:5
166196
|
167197
LL | 1usize as isize;
168198
| ^^^^^^^^^^^^^^^
169199

170200
error: casting `i32` to `u32` may lose the sign of the value
171-
--> $DIR/cast.rs:39:5
201+
--> $DIR/cast.rs:40:5
172202
|
173203
LL | -1i32 as u32;
174204
| ^^^^^^^^^^^^
175205

176206
error: casting `isize` to `usize` may lose the sign of the value
177-
--> $DIR/cast.rs:41:5
207+
--> $DIR/cast.rs:42:5
178208
|
179209
LL | -1isize as usize;
180210
| ^^^^^^^^^^^^^^^^
181211

182212
error: casting `i64` to `i8` may truncate the value
183-
--> $DIR/cast.rs:108:5
213+
--> $DIR/cast.rs:109:5
184214
|
185215
LL | (-99999999999i64).min(1) as i8; // should be linted because signed
186216
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -192,7 +222,7 @@ LL | i8::try_from((-99999999999i64).min(1)); // should be linted because sig
192222
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193223

194224
error: casting `u64` to `u8` may truncate the value
195-
--> $DIR/cast.rs:120:5
225+
--> $DIR/cast.rs:121:5
196226
|
197227
LL | 999999u64.clamp(0, 256) as u8; // should still be linted
198228
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -204,7 +234,7 @@ LL | u8::try_from(999999u64.clamp(0, 256)); // should still be linted
204234
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205235

206236
error: casting `main::E2` to `u8` may truncate the value
207-
--> $DIR/cast.rs:141:21
237+
--> $DIR/cast.rs:142:21
208238
|
209239
LL | let _ = self as u8;
210240
| ^^^^^^^^^^
@@ -216,15 +246,15 @@ LL | let _ = u8::try_from(self);
216246
| ~~~~~~~~~~~~~~~~~~
217247

218248
error: casting `main::E2::B` to `u8` will truncate the value
219-
--> $DIR/cast.rs:142:21
249+
--> $DIR/cast.rs:143:21
220250
|
221251
LL | let _ = Self::B as u8;
222252
| ^^^^^^^^^^^^^
223253
|
224254
= note: `-D clippy::cast-enum-truncation` implied by `-D warnings`
225255

226256
error: casting `main::E5` to `i8` may truncate the value
227-
--> $DIR/cast.rs:178:21
257+
--> $DIR/cast.rs:179:21
228258
|
229259
LL | let _ = self as i8;
230260
| ^^^^^^^^^^
@@ -236,13 +266,13 @@ LL | let _ = i8::try_from(self);
236266
| ~~~~~~~~~~~~~~~~~~
237267

238268
error: casting `main::E5::A` to `i8` will truncate the value
239-
--> $DIR/cast.rs:179:21
269+
--> $DIR/cast.rs:180:21
240270
|
241271
LL | let _ = Self::A as i8;
242272
| ^^^^^^^^^^^^^
243273

244274
error: casting `main::E6` to `i16` may truncate the value
245-
--> $DIR/cast.rs:193:21
275+
--> $DIR/cast.rs:194:21
246276
|
247277
LL | let _ = self as i16;
248278
| ^^^^^^^^^^^
@@ -254,7 +284,7 @@ LL | let _ = i16::try_from(self);
254284
| ~~~~~~~~~~~~~~~~~~~
255285

256286
error: casting `main::E7` to `usize` may truncate the value on targets with 32-bit wide pointers
257-
--> $DIR/cast.rs:208:21
287+
--> $DIR/cast.rs:209:21
258288
|
259289
LL | let _ = self as usize;
260290
| ^^^^^^^^^^^^^
@@ -266,7 +296,7 @@ LL | let _ = usize::try_from(self);
266296
| ~~~~~~~~~~~~~~~~~~~~~
267297

268298
error: casting `main::E10` to `u16` may truncate the value
269-
--> $DIR/cast.rs:249:21
299+
--> $DIR/cast.rs:250:21
270300
|
271301
LL | let _ = self as u16;
272302
| ^^^^^^^^^^^
@@ -278,7 +308,7 @@ LL | let _ = u16::try_from(self);
278308
| ~~~~~~~~~~~~~~~~~~~
279309

280310
error: casting `u32` to `u8` may truncate the value
281-
--> $DIR/cast.rs:257:13
311+
--> $DIR/cast.rs:258:13
282312
|
283313
LL | let c = (q >> 16) as u8;
284314
| ^^^^^^^^^^^^^^^
@@ -290,7 +320,7 @@ LL | let c = u8::try_from((q >> 16));
290320
| ~~~~~~~~~~~~~~~~~~~~~~~
291321

292322
error: casting `u32` to `u8` may truncate the value
293-
--> $DIR/cast.rs:260:13
323+
--> $DIR/cast.rs:261:13
294324
|
295325
LL | let c = (q / 1000) as u8;
296326
| ^^^^^^^^^^^^^^^^
@@ -301,5 +331,5 @@ help: ... or use `try_from` and handle the error accordingly
301331
LL | let c = u8::try_from((q / 1000));
302332
| ~~~~~~~~~~~~~~~~~~~~~~~~
303333

304-
error: aborting due to 33 previous errors
334+
error: aborting due to 36 previous errors
305335

0 commit comments

Comments
 (0)