-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add missing_assert_message
lint
#10362
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ea2547b
Add `missing_assert_message` lint
unexge 8f3ac65
Dogfood `missing_assert_message` on Clippy
unexge 099d610
Apply suggestions from code review
unexge 4eb6ccc
Update lint description and add help section
unexge 6fac73b
Move lint to `restriction` category
unexge b4b2b12
Revert "Dogfood `missing_assert_message` on Clippy"
unexge e7065ef
Revert `tests/ui/filter_map_next_fixable.rs`
unexge 682d52c
Update assertion macro parsing logic for Rust 1.52 changes
unexge 87f58a1
Use late lint pass for `missing_assert_message` lint
unexge b554ff4
Add `Known problems` section
unexge 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 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, | ||
unexge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have a
### Known Problems
section that admits we cannot check the assert messages for quality, and perhaps a link to some docs or guidelines?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the
Known Problems
section but couldn't find a good doc to link. The best thing I can find is the answers to https://softwareengineering.stackexchange.com/questions/301652/purpose-of-assertion-messages. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, let's not link to a site where the content could change at will. In that case, just leave the quality unspecified.