Skip to content

Commit

Permalink
Add test cases and test for HELP
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cerevisiae committed Jan 8, 2025
1 parent 330be17 commit 1517a41
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
26 changes: 26 additions & 0 deletions tests/ui/lint/type-overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,46 @@

fn main() {
let error = 255i8; //~WARNING literal out of range for `i8`
//~^ HELP consider using the type `u8` instead

let ok = 0b1000_0001; // should be ok -> i32
let ok = 0b0111_1111i8; // should be ok -> 127i8

let fail = 0b1000_0001i8; //~WARNING literal out of range for `i8`
//~^ HELP consider using the type `u8` instead
//~| HELP consider using the type `u8` for the literal and cast it to `i8`

let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for `i64`
//~^ HELP consider using the type `u64` instead
//~| HELP consider using the type `u64` for the literal and cast it to `i64`

let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for `u32`
//~^ HELP consider using the type `u64` instead

let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
//~^ WARNING literal out of range for `i128`
//~| HELP consider using the type `u128` instead
//~| HELP consider using the type `u128` for the literal and cast it to `i128`

let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
//~^ WARNING literal out of range for `i32`
//~| HELP consider using the type `u128` instead

let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000;
//~^ WARNING literal out of range for `i32`
//~| HELP consider using the type `i128` instead

let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
//~^ WARNING literal out of range for `i128`

let fail = 340282366920938463463374607431768211455i8;
//~^ WARNING literal out of range for `i8`
//~| HELP consider using the type `u128` instead

let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
//~| HELP consider using the type `i128` instead
//~| HELP

let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
//~| HELP consider using the type `i16` instead
}
51 changes: 44 additions & 7 deletions tests/ui/lint/type-overflow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | #![warn(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^

warning: literal out of range for `i8`
--> $DIR/type-overflow.rs:10:16
--> $DIR/type-overflow.rs:11:16
|
LL | let fail = 0b1000_0001i8;
| ^^^^^^^^^^^^^
Expand All @@ -29,7 +29,7 @@ LL | let fail = 0b1000_0001u8 as i8;
| ~~~~~~~~~~~~~~~~~~~

warning: literal out of range for `i64`
--> $DIR/type-overflow.rs:12:16
--> $DIR/type-overflow.rs:15:16
|
LL | let fail = 0x8000_0000_0000_0000i64;
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -45,15 +45,15 @@ LL | let fail = 0x8000_0000_0000_0000u64 as i64;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: literal out of range for `u32`
--> $DIR/type-overflow.rs:14:16
--> $DIR/type-overflow.rs:19:16
|
LL | let fail = 0x1_FFFF_FFFFu32;
| ^^^^^^^^^^^^^^^^ help: consider using the type `u64` instead: `0x1_FFFF_FFFFu64`
|
= note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into the type `u32` and will become `4294967295u32`

warning: literal out of range for `i128`
--> $DIR/type-overflow.rs:16:22
--> $DIR/type-overflow.rs:22:22
|
LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -66,7 +66,44 @@ LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000u128 as i128;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: literal out of range for `i32`
--> $DIR/type-overflow.rs:19:16
--> $DIR/type-overflow.rs:27:16
|
LL | let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32` and will become `0i32`
= help: consider using the type `u128` instead

warning: literal out of range for `i32`
--> $DIR/type-overflow.rs:31:17
|
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32`
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0000` will become `0i32`
= help: consider using the type `i128` instead

warning: literal out of range for `i128`
--> $DIR/type-overflow.rs:35:17
|
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0001i128` (decimal `170141183460469231731687303715884105729`) does not fit into the type `i128`
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0001i128` will become `170141183460469231731687303715884105727i128`

warning: literal out of range for `i8`
--> $DIR/type-overflow.rs:38:16
|
LL | let fail = 340282366920938463463374607431768211455i8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `340282366920938463463374607431768211455i8` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using the type `u128` instead

warning: literal out of range for `i32`
--> $DIR/type-overflow.rs:42:16
|
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -79,13 +116,13 @@ LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: literal out of range for `i8`
--> $DIR/type-overflow.rs:21:17
--> $DIR/type-overflow.rs:46:17
|
LL | let fail = -0b1111_1111i8;
| ^^^^^^^^^^^^^ help: consider using the type `i16` instead: `0b1111_1111i16`
|
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8`
= note: and the value `-0b1111_1111i8` will become `1i8`

warning: 7 warnings emitted
warning: 11 warnings emitted

0 comments on commit 1517a41

Please sign in to comment.