|
| 1 | +#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)] |
| 2 | + |
| 3 | +macro_rules! assert_const { |
| 4 | + ($len:expr) => { |
| 5 | + assert!($len > 0); |
| 6 | + debug_assert!($len < 0); |
| 7 | + }; |
| 8 | +} |
| 9 | +fn main() { |
| 10 | + assert!(true); |
| 11 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 12 | + assert!(false); |
| 13 | + //~^ ERROR: `assert!(false)` should probably be replaced |
| 14 | + assert!(true, "true message"); |
| 15 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 16 | + assert!(false, "false message"); |
| 17 | + //~^ ERROR: `assert!(false, ..)` should probably be replaced |
| 18 | + |
| 19 | + let msg = "panic message"; |
| 20 | + assert!(false, "{}", msg.to_uppercase()); |
| 21 | + //~^ ERROR: `assert!(false, ..)` should probably be replaced |
| 22 | + |
| 23 | + const B: bool = true; |
| 24 | + assert!(B); |
| 25 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 26 | + |
| 27 | + const C: bool = false; |
| 28 | + assert!(C); |
| 29 | + //~^ ERROR: `assert!(false)` should probably be replaced |
| 30 | + assert!(C, "C message"); |
| 31 | + //~^ ERROR: `assert!(false, ..)` should probably be replaced |
| 32 | + |
| 33 | + debug_assert!(true); |
| 34 | + //~^ ERROR: `debug_assert!(true)` will be optimized out by the compiler |
| 35 | + // Don't lint this, since there is no better way for expressing "Only panic in debug mode". |
| 36 | + debug_assert!(false); // #3948 |
| 37 | + assert_const!(3); |
| 38 | + assert_const!(-1); |
| 39 | + |
| 40 | + // Don't lint if based on `cfg!(..)`: |
| 41 | + assert!(cfg!(feature = "hey") || cfg!(not(feature = "asdf"))); |
| 42 | + |
| 43 | + let flag: bool = cfg!(not(feature = "asdf")); |
| 44 | + assert!(flag); |
| 45 | + |
| 46 | + const CFG_FLAG: &bool = &cfg!(feature = "hey"); |
| 47 | + assert!(!CFG_FLAG); |
| 48 | + |
| 49 | + const _: () = assert!(true); |
| 50 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 51 | + #[allow(clippy::bool_assert_comparison)] |
| 52 | + assert_eq!(8, (7 + 1)); |
| 53 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 54 | + |
| 55 | + // Don't lint if the value is dependent on a defined constant: |
| 56 | + const N: usize = 1024; |
| 57 | + const _: () = assert!(N.is_power_of_two()); |
| 58 | +} |
| 59 | + |
| 60 | +const _: () = { |
| 61 | + assert!(true); |
| 62 | + //~^ ERROR: `assert!(true)` will be optimized out by the compiler |
| 63 | + assert!(8 == (7 + 1)); |
| 64 | +}; |
0 commit comments