-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lint to check for boolean comparison in assert macro calls #7083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::{ast_utils, is_direct_expn_of}; | ||
use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** This lint warns about boolean comparisons in assert-like macros. | ||
/// | ||
/// **Why is this bad?** It is shorter to use the equivalent. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// // Bad | ||
/// assert_eq!("a".is_empty(), false); | ||
/// assert_ne!("a".is_empty(), true); | ||
/// | ||
/// // Good | ||
/// assert!(!"a".is_empty()); | ||
/// ``` | ||
pub BOOL_ASSERT_COMPARISON, | ||
style, | ||
"Using a boolean as comparison value in an assert_* macro when there is no need" | ||
} | ||
|
||
declare_lint_pass!(BoolAssertComparison => [BOOL_ASSERT_COMPARISON]); | ||
|
||
fn is_bool_lit(e: &Expr) -> bool { | ||
matches!( | ||
e.kind, | ||
ExprKind::Lit(Lit { | ||
kind: LitKind::Bool(_), | ||
.. | ||
}) | ||
) && !e.span.from_expansion() | ||
} | ||
|
||
impl EarlyLintPass for BoolAssertComparison { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) { | ||
let macros = ["assert_eq", "debug_assert_eq"]; | ||
let inverted_macros = ["assert_ne", "debug_assert_ne"]; | ||
|
||
for mac in macros.iter().chain(inverted_macros.iter()) { | ||
if let Some(span) = is_direct_expn_of(e.span, mac) { | ||
if let Some([a, b]) = ast_utils::extract_assert_macro_args(e) { | ||
let nb_bool_args = is_bool_lit(a) as usize + is_bool_lit(b) as usize; | ||
|
||
if nb_bool_args != 1 { | ||
// If there are two boolean arguments, we definitely don't understand | ||
// what's going on, so better leave things as is... | ||
// | ||
// Or there is simply no boolean and then we can leave things as is! | ||
return; | ||
GuillaumeGomez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
let non_eq_mac = &mac[..mac.len() - 3]; | ||
span_lint_and_sugg( | ||
cx, | ||
BOOL_ASSERT_COMPARISON, | ||
span, | ||
&format!("used `{}!` with a literal bool", mac), | ||
"replace it with", | ||
format!("{}!(..)", non_eq_mac), | ||
Applicability::MaybeIncorrect, | ||
); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.