]> git.proxmox.com Git - rustc.git/blob - tests/ui/lint/lint-exceeding-bitshifts.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / lint / lint-exceeding-bitshifts.rs
1 // revisions: noopt opt opt_with_overflow_checks
2 //[noopt]compile-flags: -C opt-level=0
3 //[opt]compile-flags: -O
4 //[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
5 // build-pass
6 // ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
7 // normalize-stderr-test "shift left by `(64|32)_usize`, which" -> "shift left by `%BITS%`, which"
8
9 #![crate_type="lib"]
10 #![warn(arithmetic_overflow)]
11
12
13 pub trait Foo {
14 const N: i32;
15 }
16
17 impl<T: Foo> Foo for Vec<T> {
18 const N: i32 = T::N << 42; //~ WARN: arithmetic operation will overflow
19 }
20
21 pub fn foo(x: i32) {
22 let _ = x << 42; //~ WARN: arithmetic operation will overflow
23 }
24
25 pub fn main() {
26 let n = 1u8 << 7;
27 let n = 1u8 << 8; //~ WARN: arithmetic operation will overflow
28 let n = 1u16 << 15;
29 let n = 1u16 << 16; //~ WARN: arithmetic operation will overflow
30 let n = 1u32 << 31;
31 let n = 1u32 << 32; //~ WARN: arithmetic operation will overflow
32 let n = 1u64 << 63;
33 let n = 1u64 << 64; //~ WARN: arithmetic operation will overflow
34 let n = 1i8 << 7;
35 let n = 1i8 << 8; //~ WARN: arithmetic operation will overflow
36 let n = 1i16 << 15;
37 let n = 1i16 << 16; //~ WARN: arithmetic operation will overflow
38 let n = 1i32 << 31;
39 let n = 1i32 << 32; //~ WARN: arithmetic operation will overflow
40 let n = 1i64 << 63;
41 let n = 1i64 << 64; //~ WARN: arithmetic operation will overflow
42
43 let n = 1u8 >> 7;
44 let n = 1u8 >> 8; //~ WARN: arithmetic operation will overflow
45 let n = 1u16 >> 15;
46 let n = 1u16 >> 16; //~ WARN: arithmetic operation will overflow
47 let n = 1u32 >> 31;
48 let n = 1u32 >> 32; //~ WARN: arithmetic operation will overflow
49 let n = 1u64 >> 63;
50 let n = 1u64 >> 64; //~ WARN: arithmetic operation will overflow
51 let n = 1i8 >> 7;
52 let n = 1i8 >> 8; //~ WARN: arithmetic operation will overflow
53 let n = 1i16 >> 15;
54 let n = 1i16 >> 16; //~ WARN: arithmetic operation will overflow
55 let n = 1i32 >> 31;
56 let n = 1i32 >> 32; //~ WARN: arithmetic operation will overflow
57 let n = 1i64 >> 63;
58 let n = 1i64 >> 64; //~ WARN: arithmetic operation will overflow
59
60 let n = 1u8;
61 let n = n << 7;
62 let n = n << 8; //~ WARN: arithmetic operation will overflow
63
64 let n = 1u8 << -8; //~ WARN: arithmetic operation will overflow
65
66 let n = 1i8<<(1isize+-1);
67
68 let n = 1u8 << (4+3);
69 let n = 1u8 << (4+4); //~ WARN: arithmetic operation will overflow
70 let n = 1i64 >> [63][0];
71 let n = 1i64 >> [64][0]; //~ WARN: arithmetic operation will overflow
72
73 #[cfg(target_pointer_width = "32")]
74 const BITS: usize = 32;
75 #[cfg(target_pointer_width = "64")]
76 const BITS: usize = 64;
77 let n = 1_isize << BITS; //~ WARN: arithmetic operation will overflow
78 let n = 1_usize << BITS; //~ WARN: arithmetic operation will overflow
79 }