/src/test/ui/lint/lint-exceeding-bitshifts.rs

https://gitlab.com/rust-lang/rust · Rust · 79 lines · 60 code · 12 blank · 7 comment · 1 complexity · afb3438a07e77d23a20bfc73dc859a5b MD5 · raw file

  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. #![crate_type="lib"]
  9. #![warn(arithmetic_overflow, const_err)]
  10. pub trait Foo {
  11. const N: i32;
  12. }
  13. impl<T: Foo> Foo for Vec<T> {
  14. const N: i32 = T::N << 42; //~ WARN: arithmetic operation will overflow
  15. }
  16. pub fn foo(x: i32) {
  17. let _ = x << 42; //~ WARN: arithmetic operation will overflow
  18. }
  19. pub fn main() {
  20. let n = 1u8 << 7;
  21. let n = 1u8 << 8; //~ WARN: arithmetic operation will overflow
  22. let n = 1u16 << 15;
  23. let n = 1u16 << 16; //~ WARN: arithmetic operation will overflow
  24. let n = 1u32 << 31;
  25. let n = 1u32 << 32; //~ WARN: arithmetic operation will overflow
  26. let n = 1u64 << 63;
  27. let n = 1u64 << 64; //~ WARN: arithmetic operation will overflow
  28. let n = 1i8 << 7;
  29. let n = 1i8 << 8; //~ WARN: arithmetic operation will overflow
  30. let n = 1i16 << 15;
  31. let n = 1i16 << 16; //~ WARN: arithmetic operation will overflow
  32. let n = 1i32 << 31;
  33. let n = 1i32 << 32; //~ WARN: arithmetic operation will overflow
  34. let n = 1i64 << 63;
  35. let n = 1i64 << 64; //~ WARN: arithmetic operation will overflow
  36. let n = 1u8 >> 7;
  37. let n = 1u8 >> 8; //~ WARN: arithmetic operation will overflow
  38. let n = 1u16 >> 15;
  39. let n = 1u16 >> 16; //~ WARN: arithmetic operation will overflow
  40. let n = 1u32 >> 31;
  41. let n = 1u32 >> 32; //~ WARN: arithmetic operation will overflow
  42. let n = 1u64 >> 63;
  43. let n = 1u64 >> 64; //~ WARN: arithmetic operation will overflow
  44. let n = 1i8 >> 7;
  45. let n = 1i8 >> 8; //~ WARN: arithmetic operation will overflow
  46. let n = 1i16 >> 15;
  47. let n = 1i16 >> 16; //~ WARN: arithmetic operation will overflow
  48. let n = 1i32 >> 31;
  49. let n = 1i32 >> 32; //~ WARN: arithmetic operation will overflow
  50. let n = 1i64 >> 63;
  51. let n = 1i64 >> 64; //~ WARN: arithmetic operation will overflow
  52. let n = 1u8;
  53. let n = n << 7;
  54. let n = n << 8; //~ WARN: arithmetic operation will overflow
  55. let n = 1u8 << -8; //~ WARN: arithmetic operation will overflow
  56. let n = 1i8<<(1isize+-1);
  57. let n = 1u8 << (4+3);
  58. let n = 1u8 << (4+4); //~ WARN: arithmetic operation will overflow
  59. let n = 1i64 >> [63][0];
  60. let n = 1i64 >> [64][0]; //~ WARN: arithmetic operation will overflow
  61. #[cfg(target_pointer_width = "32")]
  62. const BITS: usize = 32;
  63. #[cfg(target_pointer_width = "64")]
  64. const BITS: usize = 64;
  65. let n = 1_isize << BITS; //~ WARN: arithmetic operation will overflow
  66. let n = 1_usize << BITS; //~ WARN: arithmetic operation will overflow
  67. }