Skip to content

Commit c8a0565

Browse files
committed
Auto merge of #10564 - asquared31415:cast_doesnt_wrap, r=giraffate
make cast_possible_wrap work correctly for 16 bit {u,i}size These changes make `cast_possible_wrap` aware of the different pointer widths and fixes the implementation to print the correct pointer widths. Fixes #9337 changelog: `cast_possible_wrap` does not lint on `u8 as isize` or `usize as i8`, since these can never wrap. `cast_possible_wrap` now properly considers 16 bit pointer size and prints the correct bit widths.
2 parents cc8ead2 + 7cd0ec5 commit c8a0565

File tree

4 files changed

+187
-40
lines changed

4 files changed

+187
-40
lines changed
+71-23
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,89 @@
1-
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::ty::is_isize_or_usize;
31
use rustc_hir::Expr;
4-
use rustc_lint::LateContext;
2+
use rustc_lint::{LateContext, LintContext};
53
use rustc_middle::ty::Ty;
64

75
use super::{utils, CAST_POSSIBLE_WRAP};
86

7+
// this should be kept in sync with the allowed bit widths of `usize` and `isize`
8+
const ALLOWED_POINTER_SIZES: [u64; 3] = [16, 32, 64];
9+
10+
// whether the lint should be emitted, and the required pointer size, if it matters
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
12+
enum EmitState {
13+
NoLint,
14+
LintAlways,
15+
LintOnPtrSize(u64),
16+
}
17+
918
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
1019
if !(cast_from.is_integral() && cast_to.is_integral()) {
1120
return;
1221
}
1322

14-
let arch_64_suffix = " on targets with 64-bit wide pointers";
15-
let arch_32_suffix = " on targets with 32-bit wide pointers";
16-
let cast_unsigned_to_signed = !cast_from.is_signed() && cast_to.is_signed();
23+
// emit a lint if a cast is:
24+
// 1. unsigned to signed
25+
// and
26+
// 2. either:
27+
// 2a. between two types of constant size that are always the same size
28+
// 2b. between one target-dependent size and one constant size integer,
29+
// and the constant integer is in the allowed set of target dependent sizes
30+
// (the ptr size could be chosen to be the same as the constant size)
31+
32+
if cast_from.is_signed() || !cast_to.is_signed() {
33+
return;
34+
}
35+
1736
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
1837
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
1938

20-
let (should_lint, suffix) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
21-
(true, true) | (false, false) => (to_nbits == from_nbits && cast_unsigned_to_signed, ""),
22-
(true, false) => (to_nbits <= 32 && cast_unsigned_to_signed, arch_32_suffix),
23-
(false, true) => (
24-
cast_unsigned_to_signed,
25-
if from_nbits == 64 {
26-
arch_64_suffix
39+
let should_lint = match (cast_from.is_ptr_sized_integral(), cast_to.is_ptr_sized_integral()) {
40+
(true, true) => {
41+
// casts between two ptr sized integers are trivially always the same size
42+
// so do not depend on any specific pointer size to be the same
43+
EmitState::LintAlways
44+
},
45+
(true, false) => {
46+
// the first type is `usize` and the second is a constant sized signed integer
47+
if ALLOWED_POINTER_SIZES.contains(&to_nbits) {
48+
EmitState::LintOnPtrSize(to_nbits)
49+
} else {
50+
EmitState::NoLint
51+
}
52+
},
53+
(false, true) => {
54+
// the first type is a constant sized unsigned integer, and the second is `isize`
55+
if ALLOWED_POINTER_SIZES.contains(&from_nbits) {
56+
EmitState::LintOnPtrSize(from_nbits)
57+
} else {
58+
EmitState::NoLint
59+
}
60+
},
61+
(false, false) => {
62+
// the types are both a constant known size
63+
// and do not depend on any specific pointer size to be the same
64+
if from_nbits == to_nbits {
65+
EmitState::LintAlways
2766
} else {
28-
arch_32_suffix
29-
},
67+
EmitState::NoLint
68+
}
69+
},
70+
};
71+
72+
let message = match should_lint {
73+
EmitState::NoLint => return,
74+
EmitState::LintAlways => format!("casting `{cast_from}` to `{cast_to}` may wrap around the value"),
75+
EmitState::LintOnPtrSize(ptr_size) => format!(
76+
"casting `{cast_from}` to `{cast_to}` may wrap around the value on targets with {ptr_size}-bit wide pointers",
3077
),
3178
};
3279

33-
if should_lint {
34-
span_lint(
35-
cx,
36-
CAST_POSSIBLE_WRAP,
37-
expr.span,
38-
&format!("casting `{cast_from}` to `{cast_to}` may wrap around the value{suffix}",),
39-
);
40-
}
80+
cx.struct_span_lint(CAST_POSSIBLE_WRAP, expr.span, message, |diag| {
81+
if let EmitState::LintOnPtrSize(16) = should_lint {
82+
diag
83+
.note("`usize` and `isize` may be as small as 16 bits on some platforms")
84+
.note("for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types")
85+
} else {
86+
diag
87+
}
88+
});
4189
}

clippy_lints/src/casts/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ declare_clippy_lint! {
118118
declare_clippy_lint! {
119119
/// ### What it does
120120
/// Checks for casts from an unsigned type to a signed type of
121-
/// the same size. Performing such a cast is a 'no-op' for the compiler,
122-
/// i.e., nothing is changed at the bit level, and the binary representation of
123-
/// the value is reinterpreted. This can cause wrapping if the value is too big
121+
/// the same size, or possibly smaller due to target dependent integers.
122+
/// Performing such a cast is a 'no-op' for the compiler, i.e., nothing is
123+
/// changed at the bit level, and the binary representation of the value is
124+
/// reinterpreted. This can cause wrapping if the value is too big
124125
/// for the target signed type. However, the cast works as defined, so this lint
125126
/// is `Allow` by default.
126127
///

tests/ui/cast.rs

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ fn main() {
4141
1u32 as i32;
4242
1u64 as i64;
4343
1usize as isize;
44+
1usize as i8; // should not wrap, usize is never 8 bits
45+
1usize as i16; // wraps on 16 bit ptr size
46+
1usize as i32; // wraps on 32 bit ptr size
47+
1usize as i64; // wraps on 64 bit ptr size
48+
1u8 as isize; // should not wrap, isize is never 8 bits
49+
1u16 as isize; // wraps on 16 bit ptr size
50+
1u32 as isize; // wraps on 32 bit ptr size
51+
1u64 as isize; // wraps on 64 bit ptr size
4452
// Test clippy::cast_sign_loss
4553
1i32 as u32;
4654
-1i32 as u32;

tests/ui/cast.stderr

+104-14
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,110 @@ error: casting `usize` to `isize` may wrap around the value
215215
LL | 1usize as isize;
216216
| ^^^^^^^^^^^^^^^
217217

218-
error: casting `i32` to `u32` may lose the sign of the value
218+
error: casting `usize` to `i8` may truncate the value
219+
--> $DIR/cast.rs:44:5
220+
|
221+
LL | 1usize as i8; // should not wrap, usize is never 8 bits
222+
| ^^^^^^^^^^^^
223+
|
224+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
225+
help: ... or use `try_from` and handle the error accordingly
226+
|
227+
LL | i8::try_from(1usize); // should not wrap, usize is never 8 bits
228+
| ~~~~~~~~~~~~~~~~~~~~
229+
230+
error: casting `usize` to `i16` may truncate the value
231+
--> $DIR/cast.rs:45:5
232+
|
233+
LL | 1usize as i16; // wraps on 16 bit ptr size
234+
| ^^^^^^^^^^^^^
235+
|
236+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
237+
help: ... or use `try_from` and handle the error accordingly
238+
|
239+
LL | i16::try_from(1usize); // wraps on 16 bit ptr size
240+
| ~~~~~~~~~~~~~~~~~~~~~
241+
242+
error: casting `usize` to `i16` may wrap around the value on targets with 16-bit wide pointers
243+
--> $DIR/cast.rs:45:5
244+
|
245+
LL | 1usize as i16; // wraps on 16 bit ptr size
246+
| ^^^^^^^^^^^^^
247+
|
248+
= note: `usize` and `isize` may be as small as 16 bits on some platforms
249+
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
250+
251+
error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers
219252
--> $DIR/cast.rs:46:5
220253
|
254+
LL | 1usize as i32; // wraps on 32 bit ptr size
255+
| ^^^^^^^^^^^^^
256+
|
257+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
258+
help: ... or use `try_from` and handle the error accordingly
259+
|
260+
LL | i32::try_from(1usize); // wraps on 32 bit ptr size
261+
| ~~~~~~~~~~~~~~~~~~~~~
262+
263+
error: casting `usize` to `i32` may wrap around the value on targets with 32-bit wide pointers
264+
--> $DIR/cast.rs:46:5
265+
|
266+
LL | 1usize as i32; // wraps on 32 bit ptr size
267+
| ^^^^^^^^^^^^^
268+
269+
error: casting `usize` to `i64` may wrap around the value on targets with 64-bit wide pointers
270+
--> $DIR/cast.rs:47:5
271+
|
272+
LL | 1usize as i64; // wraps on 64 bit ptr size
273+
| ^^^^^^^^^^^^^
274+
275+
error: casting `u16` to `isize` may wrap around the value on targets with 16-bit wide pointers
276+
--> $DIR/cast.rs:49:5
277+
|
278+
LL | 1u16 as isize; // wraps on 16 bit ptr size
279+
| ^^^^^^^^^^^^^
280+
|
281+
= note: `usize` and `isize` may be as small as 16 bits on some platforms
282+
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
283+
284+
error: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
285+
--> $DIR/cast.rs:50:5
286+
|
287+
LL | 1u32 as isize; // wraps on 32 bit ptr size
288+
| ^^^^^^^^^^^^^
289+
290+
error: casting `u64` to `isize` may truncate the value on targets with 32-bit wide pointers
291+
--> $DIR/cast.rs:51:5
292+
|
293+
LL | 1u64 as isize; // wraps on 64 bit ptr size
294+
| ^^^^^^^^^^^^^
295+
|
296+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
297+
help: ... or use `try_from` and handle the error accordingly
298+
|
299+
LL | isize::try_from(1u64); // wraps on 64 bit ptr size
300+
| ~~~~~~~~~~~~~~~~~~~~~
301+
302+
error: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
303+
--> $DIR/cast.rs:51:5
304+
|
305+
LL | 1u64 as isize; // wraps on 64 bit ptr size
306+
| ^^^^^^^^^^^^^
307+
308+
error: casting `i32` to `u32` may lose the sign of the value
309+
--> $DIR/cast.rs:54:5
310+
|
221311
LL | -1i32 as u32;
222312
| ^^^^^^^^^^^^
223313

224314
error: casting `isize` to `usize` may lose the sign of the value
225-
--> $DIR/cast.rs:48:5
315+
--> $DIR/cast.rs:56:5
226316
|
227317
LL | -1isize as usize;
228318
| ^^^^^^^^^^^^^^^^
229319

230320
error: casting `i64` to `i8` may truncate the value
231-
--> $DIR/cast.rs:115:5
321+
--> $DIR/cast.rs:123:5
232322
|
233323
LL | (-99999999999i64).min(1) as i8; // should be linted because signed
234324
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -240,7 +330,7 @@ LL | i8::try_from((-99999999999i64).min(1)); // should be linted because sig
240330
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241331

242332
error: casting `u64` to `u8` may truncate the value
243-
--> $DIR/cast.rs:127:5
333+
--> $DIR/cast.rs:135:5
244334
|
245335
LL | 999999u64.clamp(0, 256) as u8; // should still be linted
246336
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -252,7 +342,7 @@ LL | u8::try_from(999999u64.clamp(0, 256)); // should still be linted
252342
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
253343

254344
error: casting `main::E2` to `u8` may truncate the value
255-
--> $DIR/cast.rs:148:21
345+
--> $DIR/cast.rs:156:21
256346
|
257347
LL | let _ = self as u8;
258348
| ^^^^^^^^^^
@@ -264,15 +354,15 @@ LL | let _ = u8::try_from(self);
264354
| ~~~~~~~~~~~~~~~~~~
265355

266356
error: casting `main::E2::B` to `u8` will truncate the value
267-
--> $DIR/cast.rs:149:21
357+
--> $DIR/cast.rs:157:21
268358
|
269359
LL | let _ = Self::B as u8;
270360
| ^^^^^^^^^^^^^
271361
|
272362
= note: `-D clippy::cast-enum-truncation` implied by `-D warnings`
273363

274364
error: casting `main::E5` to `i8` may truncate the value
275-
--> $DIR/cast.rs:185:21
365+
--> $DIR/cast.rs:193:21
276366
|
277367
LL | let _ = self as i8;
278368
| ^^^^^^^^^^
@@ -284,13 +374,13 @@ LL | let _ = i8::try_from(self);
284374
| ~~~~~~~~~~~~~~~~~~
285375

286376
error: casting `main::E5::A` to `i8` will truncate the value
287-
--> $DIR/cast.rs:186:21
377+
--> $DIR/cast.rs:194:21
288378
|
289379
LL | let _ = Self::A as i8;
290380
| ^^^^^^^^^^^^^
291381

292382
error: casting `main::E6` to `i16` may truncate the value
293-
--> $DIR/cast.rs:200:21
383+
--> $DIR/cast.rs:208:21
294384
|
295385
LL | let _ = self as i16;
296386
| ^^^^^^^^^^^
@@ -302,7 +392,7 @@ LL | let _ = i16::try_from(self);
302392
| ~~~~~~~~~~~~~~~~~~~
303393

304394
error: casting `main::E7` to `usize` may truncate the value on targets with 32-bit wide pointers
305-
--> $DIR/cast.rs:215:21
395+
--> $DIR/cast.rs:223:21
306396
|
307397
LL | let _ = self as usize;
308398
| ^^^^^^^^^^^^^
@@ -314,7 +404,7 @@ LL | let _ = usize::try_from(self);
314404
| ~~~~~~~~~~~~~~~~~~~~~
315405

316406
error: casting `main::E10` to `u16` may truncate the value
317-
--> $DIR/cast.rs:256:21
407+
--> $DIR/cast.rs:264:21
318408
|
319409
LL | let _ = self as u16;
320410
| ^^^^^^^^^^^
@@ -326,7 +416,7 @@ LL | let _ = u16::try_from(self);
326416
| ~~~~~~~~~~~~~~~~~~~
327417

328418
error: casting `u32` to `u8` may truncate the value
329-
--> $DIR/cast.rs:264:13
419+
--> $DIR/cast.rs:272:13
330420
|
331421
LL | let c = (q >> 16) as u8;
332422
| ^^^^^^^^^^^^^^^
@@ -338,7 +428,7 @@ LL | let c = u8::try_from(q >> 16);
338428
| ~~~~~~~~~~~~~~~~~~~~~
339429

340430
error: casting `u32` to `u8` may truncate the value
341-
--> $DIR/cast.rs:267:13
431+
--> $DIR/cast.rs:275:13
342432
|
343433
LL | let c = (q / 1000) as u8;
344434
| ^^^^^^^^^^^^^^^^
@@ -349,5 +439,5 @@ help: ... or use `try_from` and handle the error accordingly
349439
LL | let c = u8::try_from(q / 1000);
350440
| ~~~~~~~~~~~~~~~~~~~~~~
351441

352-
error: aborting due to 41 previous errors
442+
error: aborting due to 51 previous errors
353443

0 commit comments

Comments
 (0)