diff --git a/clippy_lints/src/bool_assert_comparison.rs b/clippy_lints/src/bool_assert_comparison.rs index b5cb2c8d4ec6..82479d0b5900 100644 --- a/clippy_lints/src/bool_assert_comparison.rs +++ b/clippy_lints/src/bool_assert_comparison.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::is_in_const_context; use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, MacroCall}; use clippy_utils::sugg::Sugg; use clippy_utils::ty::{implements_trait, is_copy}; @@ -161,6 +162,9 @@ fn check_eq<'tcx>( /// Checks for `assert!(a == b)` and `assert!(a != b)` fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, macro_call: &MacroCall, macro_name: &str) { + if is_in_const_context(cx) { + return; + } let Some((cond, _)) = find_assert_args(cx, expr, macro_call.expn) else { return; }; diff --git a/tests/ui/assertions_on_constants.fixed b/tests/ui/assertions_on_constants.fixed new file mode 100644 index 000000000000..e75ecf74c6f6 --- /dev/null +++ b/tests/ui/assertions_on_constants.fixed @@ -0,0 +1,64 @@ +#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)] + +macro_rules! assert_const { + ($len:expr) => { + assert!($len > 0); + debug_assert!($len < 0); + }; +} +fn main() { + assert!(true); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + assert!(false); + //~^ ERROR: `assert!(false)` should probably be replaced + assert!(true, "true message"); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + assert!(false, "false message"); + //~^ ERROR: `assert!(false, ..)` should probably be replaced + + let msg = "panic message"; + assert!(false, "{}", msg.to_uppercase()); + //~^ ERROR: `assert!(false, ..)` should probably be replaced + + const B: bool = true; + assert!(B); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + + const C: bool = false; + assert!(C); + //~^ ERROR: `assert!(false)` should probably be replaced + assert!(C, "C message"); + //~^ ERROR: `assert!(false, ..)` should probably be replaced + + debug_assert!(true); + //~^ ERROR: `debug_assert!(true)` will be optimized out by the compiler + // Don't lint this, since there is no better way for expressing "Only panic in debug mode". + debug_assert!(false); // #3948 + assert_const!(3); + assert_const!(-1); + + // Don't lint if based on `cfg!(..)`: + assert!(cfg!(feature = "hey") || cfg!(not(feature = "asdf"))); + + let flag: bool = cfg!(not(feature = "asdf")); + assert!(flag); + + const CFG_FLAG: &bool = &cfg!(feature = "hey"); + assert!(!CFG_FLAG); + + const _: () = assert!(true); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + #[allow(clippy::bool_assert_comparison)] + assert_eq!(8, (7 + 1)); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + + // Don't lint if the value is dependent on a defined constant: + const N: usize = 1024; + const _: () = assert!(N.is_power_of_two()); +} + +const _: () = { + assert!(true); + //~^ ERROR: `assert!(true)` will be optimized out by the compiler + assert!(8 == (7 + 1)); +}; diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index 13b043fcf748..390b2f5296d5 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -1,4 +1,4 @@ -#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op, clippy::bool_assert_comparison)] +#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)] macro_rules! assert_const { ($len:expr) => { @@ -48,7 +48,7 @@ fn main() { const _: () = assert!(true); //~^ ERROR: `assert!(true)` will be optimized out by the compiler - + #[allow(clippy::bool_assert_comparison)] assert!(8 == (7 + 1)); //~^ ERROR: `assert!(true)` will be optimized out by the compiler diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index e164a999c43e..b1f763e1a72a 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -88,6 +88,19 @@ LL | assert!(8 == (7 + 1)); | = help: remove it +error: used `assert!` with an equality comparison + --> tests/ui/assertions_on_constants.rs:52:5 + | +LL | assert!(8 == (7 + 1)); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::bool-assert-comparison` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::bool_assert_comparison)]` +help: replace it with `assert_eq!(..)` + | +LL | assert_eq!(8, (7 + 1)); + | ~~~~~~~~~ ~ + error: `assert!(true)` will be optimized out by the compiler --> tests/ui/assertions_on_constants.rs:61:5 | @@ -96,5 +109,5 @@ LL | assert!(true); | = help: remove it -error: aborting due to 12 previous errors +error: aborting due to 13 previous errors diff --git a/tests/ui/bool_assert_comparison.fixed b/tests/ui/bool_assert_comparison.fixed index 4a5b1a199282..beb42fb40c2c 100644 --- a/tests/ui/bool_assert_comparison.fixed +++ b/tests/ui/bool_assert_comparison.fixed @@ -1,4 +1,4 @@ -#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)] +#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty, clippy::eq_op)] #![warn(clippy::bool_assert_comparison)] use std::ops::Not; @@ -171,4 +171,9 @@ fn main() { assert_eq!("a", "a".to_ascii_lowercase(), "a==a"); assert_ne!("A", "A".to_ascii_lowercase()); assert_ne!("A", "A".to_ascii_lowercase(), "A!=a"); + const _: () = assert!(5 == 2 + 3); } + +const _: () = { + assert!(8 == (7 + 1)); +}; diff --git a/tests/ui/bool_assert_comparison.rs b/tests/ui/bool_assert_comparison.rs index eb49f77554bf..6b4c45635333 100644 --- a/tests/ui/bool_assert_comparison.rs +++ b/tests/ui/bool_assert_comparison.rs @@ -1,4 +1,4 @@ -#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)] +#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty, clippy::eq_op)] #![warn(clippy::bool_assert_comparison)] use std::ops::Not; @@ -171,4 +171,9 @@ fn main() { assert!("a" == "a".to_ascii_lowercase(), "a==a"); assert!("A" != "A".to_ascii_lowercase()); assert!("A" != "A".to_ascii_lowercase(), "A!=a"); + const _: () = assert!(5 == 2 + 3); } + +const _: () = { + assert!(8 == (7 + 1)); +};