-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #10362 - unexge:missing-assert-message-lint, r=llogiq
Add `missing_assert_message` lint Fixes #6207. changelog: new lint: [`missing_assert_message`]: A new lint for checking assertions that doesn't have a custom panic message. [#10362](#10362) <!-- changelog_checked --> r? `@llogiq`
- Loading branch information
Showing
7 changed files
with
333 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,82 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, PanicExpn}; | ||
use clippy_utils::{is_in_cfg_test, is_in_test_function}; | ||
use rustc_hir::Expr; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks assertions without a custom panic message. | ||
/// | ||
/// ### Why is this bad? | ||
/// Without a good custom message, it'd be hard to understand what went wrong when the assertion fails. | ||
/// A good custom message should be more about why the failure of the assertion is problematic | ||
/// and not what is failed because the assertion already conveys that. | ||
/// | ||
/// ### Known problems | ||
/// This lint cannot check the quality of the custom panic messages. | ||
/// Hence, you can suppress this lint simply by adding placeholder messages | ||
/// like "assertion failed". However, we recommend coming up with good messages | ||
/// that provide useful information instead of placeholder messages that | ||
/// don't provide any extra information. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # struct Service { ready: bool } | ||
/// fn call(service: Service) { | ||
/// assert!(service.ready); | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # struct Service { ready: bool } | ||
/// fn call(service: Service) { | ||
/// assert!(service.ready, "`service.poll_ready()` must be called first to ensure that service is ready to receive requests"); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.69.0"] | ||
pub MISSING_ASSERT_MESSAGE, | ||
restriction, | ||
"checks assertions without a custom panic message" | ||
} | ||
|
||
declare_lint_pass!(MissingAssertMessage => [MISSING_ASSERT_MESSAGE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for MissingAssertMessage { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return }; | ||
let single_argument = match cx.tcx.get_diagnostic_name(macro_call.def_id) { | ||
Some(sym::assert_macro | sym::debug_assert_macro) => true, | ||
Some( | ||
sym::assert_eq_macro | sym::assert_ne_macro | sym::debug_assert_eq_macro | sym::debug_assert_ne_macro, | ||
) => false, | ||
_ => return, | ||
}; | ||
|
||
// This lint would be very noisy in tests, so just ignore if we're in test context | ||
if is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id) { | ||
return; | ||
} | ||
|
||
let panic_expn = if single_argument { | ||
let Some((_, panic_expn)) = find_assert_args(cx, expr, macro_call.expn) else { return }; | ||
panic_expn | ||
} else { | ||
let Some((_, _, panic_expn)) = find_assert_eq_args(cx, expr, macro_call.expn) else { return }; | ||
panic_expn | ||
}; | ||
|
||
if let PanicExpn::Empty = panic_expn { | ||
span_lint_and_help( | ||
cx, | ||
MISSING_ASSERT_MESSAGE, | ||
macro_call.span, | ||
"assert without any message", | ||
None, | ||
"consider describing why the failing assert is problematic", | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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,84 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::missing_assert_message)] | ||
|
||
macro_rules! bar { | ||
($( $x:expr ),*) => { | ||
foo() | ||
}; | ||
} | ||
|
||
fn main() {} | ||
|
||
// Should trigger warning | ||
fn asserts_without_message() { | ||
assert!(foo()); | ||
assert_eq!(foo(), foo()); | ||
assert_ne!(foo(), foo()); | ||
debug_assert!(foo()); | ||
debug_assert_eq!(foo(), foo()); | ||
debug_assert_ne!(foo(), foo()); | ||
} | ||
|
||
// Should trigger warning | ||
fn asserts_without_message_but_with_macro_calls() { | ||
assert!(bar!(true)); | ||
assert!(bar!(true, false)); | ||
assert_eq!(bar!(true), foo()); | ||
assert_ne!(bar!(true, true), bar!(true)); | ||
} | ||
|
||
// Should trigger warning | ||
fn asserts_with_trailing_commas() { | ||
assert!(foo(),); | ||
assert_eq!(foo(), foo(),); | ||
assert_ne!(foo(), foo(),); | ||
debug_assert!(foo(),); | ||
debug_assert_eq!(foo(), foo(),); | ||
debug_assert_ne!(foo(), foo(),); | ||
} | ||
|
||
// Should not trigger warning | ||
fn asserts_with_message_and_with_macro_calls() { | ||
assert!(bar!(true), "msg"); | ||
assert!(bar!(true, false), "msg"); | ||
assert_eq!(bar!(true), foo(), "msg"); | ||
assert_ne!(bar!(true, true), bar!(true), "msg"); | ||
} | ||
|
||
// Should not trigger warning | ||
fn asserts_with_message() { | ||
assert!(foo(), "msg"); | ||
assert_eq!(foo(), foo(), "msg"); | ||
assert_ne!(foo(), foo(), "msg"); | ||
debug_assert!(foo(), "msg"); | ||
debug_assert_eq!(foo(), foo(), "msg"); | ||
debug_assert_ne!(foo(), foo(), "msg"); | ||
} | ||
|
||
// Should not trigger warning | ||
#[test] | ||
fn asserts_without_message_but_inside_a_test_function() { | ||
assert!(foo()); | ||
assert_eq!(foo(), foo()); | ||
assert_ne!(foo(), foo()); | ||
debug_assert!(foo()); | ||
debug_assert_eq!(foo(), foo()); | ||
debug_assert_ne!(foo(), foo()); | ||
} | ||
|
||
// Should not trigger warning | ||
#[cfg(test)] | ||
mod tests { | ||
fn asserts_without_message_but_inside_a_test_module() { | ||
assert!(foo()); | ||
assert_eq!(foo(), foo()); | ||
assert_ne!(foo(), foo()); | ||
debug_assert!(foo()); | ||
debug_assert_eq!(foo(), foo()); | ||
debug_assert_ne!(foo(), foo()); | ||
} | ||
} | ||
|
||
fn foo() -> bool { | ||
true | ||
} |
Oops, something went wrong.