Skip to content

Commit 8f4d34e

Browse files
committed
wip
1 parent a20cb02 commit 8f4d34e

File tree

6 files changed

+96
-5
lines changed

6 files changed

+96
-5
lines changed

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_in_const_context;
23
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, MacroCall};
34
use clippy_utils::sugg::Sugg;
45
use clippy_utils::ty::{implements_trait, is_copy};
@@ -161,6 +162,9 @@ fn check_eq<'tcx>(
161162

162163
/// Checks for `assert!(a == b)` and `assert!(a != b)`
163164
fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, macro_call: &MacroCall, macro_name: &str) {
165+
if is_in_const_context(cx) {
166+
return;
167+
}
164168
let Some((cond, _)) = find_assert_args(cx, expr, macro_call.expn) else {
165169
return;
166170
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
};

tests/ui/assertions_on_constants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op, clippy::bool_assert_comparison)]
1+
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)]
22

33
macro_rules! assert_const {
44
($len:expr) => {
@@ -48,7 +48,7 @@ fn main() {
4848

4949
const _: () = assert!(true);
5050
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
51-
51+
#[allow(clippy::bool_assert_comparison)]
5252
assert!(8 == (7 + 1));
5353
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
5454

tests/ui/assertions_on_constants.stderr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ LL | assert!(8 == (7 + 1));
8888
|
8989
= help: remove it
9090

91+
error: used `assert!` with an equality comparison
92+
--> tests/ui/assertions_on_constants.rs:52:5
93+
|
94+
LL | assert!(8 == (7 + 1));
95+
| ^^^^^^^^^^^^^^^^^^^^^
96+
|
97+
= note: `-D clippy::bool-assert-comparison` implied by `-D warnings`
98+
= help: to override `-D warnings` add `#[allow(clippy::bool_assert_comparison)]`
99+
help: replace it with `assert_eq!(..)`
100+
|
101+
LL | assert_eq!(8, (7 + 1));
102+
| ~~~~~~~~~ ~
103+
91104
error: `assert!(true)` will be optimized out by the compiler
92105
--> tests/ui/assertions_on_constants.rs:61:5
93106
|
@@ -96,5 +109,5 @@ LL | assert!(true);
96109
|
97110
= help: remove it
98111

99-
error: aborting due to 12 previous errors
112+
error: aborting due to 13 previous errors
100113

tests/ui/bool_assert_comparison.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
1+
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty, clippy::eq_op)]
22
#![warn(clippy::bool_assert_comparison)]
33

44
use std::ops::Not;
@@ -171,4 +171,9 @@ fn main() {
171171
assert_eq!("a", "a".to_ascii_lowercase(), "a==a");
172172
assert_ne!("A", "A".to_ascii_lowercase());
173173
assert_ne!("A", "A".to_ascii_lowercase(), "A!=a");
174+
const _: () = assert!(5 == 2 + 3);
174175
}
176+
177+
const _: () = {
178+
assert!(8 == (7 + 1));
179+
};

tests/ui/bool_assert_comparison.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
1+
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty, clippy::eq_op)]
22
#![warn(clippy::bool_assert_comparison)]
33

44
use std::ops::Not;
@@ -171,4 +171,9 @@ fn main() {
171171
assert!("a" == "a".to_ascii_lowercase(), "a==a");
172172
assert!("A" != "A".to_ascii_lowercase());
173173
assert!("A" != "A".to_ascii_lowercase(), "A!=a");
174+
const _: () = assert!(5 == 2 + 3);
174175
}
176+
177+
const _: () = {
178+
assert!(8 == (7 + 1));
179+
};

0 commit comments

Comments
 (0)