Auto merge of #7083 - GuillaumeGomez:bool-assert-eq, r=camsteffen
Add lint to check for boolean comparison in assert macro calls
This PR adds a lint to check if an assert macro is using a boolean as "comparison value". For example:
```rust
assert_eq!("a".is_empty(), false);
```
Could be rewritten as:
```rust
assert!(!"a".is_empty());
```
PS: The dev guidelines are amazing. Thanks a lot for writing them!
changelog: Add `bool_assert_comparison` lint
This commit is contained in:
commit
bbc22e2ef3
6 changed files with 284 additions and 0 deletions
59
tests/ui/bool_assert_comparison.rs
Normal file
59
tests/ui/bool_assert_comparison.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#![warn(clippy::bool_assert_comparison)]
|
||||
|
||||
macro_rules! a {
|
||||
() => {
|
||||
true
|
||||
};
|
||||
}
|
||||
macro_rules! b {
|
||||
() => {
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!("a".len(), 1);
|
||||
assert_eq!("a".is_empty(), false);
|
||||
assert_eq!("".is_empty(), true);
|
||||
assert_eq!(true, "".is_empty());
|
||||
assert_eq!(a!(), b!());
|
||||
assert_eq!(a!(), "".is_empty());
|
||||
assert_eq!("".is_empty(), b!());
|
||||
|
||||
assert_ne!("a".len(), 1);
|
||||
assert_ne!("a".is_empty(), false);
|
||||
assert_ne!("".is_empty(), true);
|
||||
assert_ne!(true, "".is_empty());
|
||||
assert_ne!(a!(), b!());
|
||||
assert_ne!(a!(), "".is_empty());
|
||||
assert_ne!("".is_empty(), b!());
|
||||
|
||||
debug_assert_eq!("a".len(), 1);
|
||||
debug_assert_eq!("a".is_empty(), false);
|
||||
debug_assert_eq!("".is_empty(), true);
|
||||
debug_assert_eq!(true, "".is_empty());
|
||||
debug_assert_eq!(a!(), b!());
|
||||
debug_assert_eq!(a!(), "".is_empty());
|
||||
debug_assert_eq!("".is_empty(), b!());
|
||||
|
||||
debug_assert_ne!("a".len(), 1);
|
||||
debug_assert_ne!("a".is_empty(), false);
|
||||
debug_assert_ne!("".is_empty(), true);
|
||||
debug_assert_ne!(true, "".is_empty());
|
||||
debug_assert_ne!(a!(), b!());
|
||||
debug_assert_ne!(a!(), "".is_empty());
|
||||
debug_assert_ne!("".is_empty(), b!());
|
||||
|
||||
// assert with error messages
|
||||
assert_eq!("a".len(), 1, "tadam {}", 1);
|
||||
assert_eq!("a".len(), 1, "tadam {}", true);
|
||||
assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
||||
assert_eq!("a".is_empty(), false, "tadam {}", true);
|
||||
assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
||||
|
||||
debug_assert_eq!("a".len(), 1, "tadam {}", 1);
|
||||
debug_assert_eq!("a".len(), 1, "tadam {}", true);
|
||||
debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
||||
debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
|
||||
debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
||||
}
|
||||
112
tests/ui/bool_assert_comparison.stderr
Normal file
112
tests/ui/bool_assert_comparison.stderr
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:16:5
|
||||
|
|
||||
LL | assert_eq!("a".is_empty(), false);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
|
||||
= note: `-D clippy::bool-assert-comparison` implied by `-D warnings`
|
||||
|
||||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:17:5
|
||||
|
|
||||
LL | assert_eq!("".is_empty(), true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:18:5
|
||||
|
|
||||
LL | assert_eq!(true, "".is_empty());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:24:5
|
||||
|
|
||||
LL | assert_ne!("a".is_empty(), false);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:25:5
|
||||
|
|
||||
LL | assert_ne!("".is_empty(), true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:26:5
|
||||
|
|
||||
LL | assert_ne!(true, "".is_empty());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:32:5
|
||||
|
|
||||
LL | debug_assert_eq!("a".is_empty(), false);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:33:5
|
||||
|
|
||||
LL | debug_assert_eq!("".is_empty(), true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:34:5
|
||||
|
|
||||
LL | debug_assert_eq!(true, "".is_empty());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:40:5
|
||||
|
|
||||
LL | debug_assert_ne!("a".is_empty(), false);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:41:5
|
||||
|
|
||||
LL | debug_assert_ne!("".is_empty(), true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_ne!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:42:5
|
||||
|
|
||||
LL | debug_assert_ne!(true, "".is_empty());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:50:5
|
||||
|
|
||||
LL | assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:51:5
|
||||
|
|
||||
LL | assert_eq!("a".is_empty(), false, "tadam {}", true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:52:5
|
||||
|
|
||||
LL | assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:56:5
|
||||
|
|
||||
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:57:5
|
||||
|
|
||||
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: used `debug_assert_eq!` with a literal bool
|
||||
--> $DIR/bool_assert_comparison.rs:58:5
|
||||
|
|
||||
LL | debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(..)`
|
||||
|
||||
error: aborting due to 18 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue