-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) soon. Please see the contribution instructions for more information. |
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.
Great work! I think the macro stuff is really tricky.
I have but a few small doc suggestions and I'll have to look up if we need an msrv.
clippy_utils/src/numeric_literal.rs
Outdated
debug_assert!(lit_kind.is_numeric()); | ||
debug_assert!(lit_kind.is_numeric(), "`lit_kind` should be numeric"); | ||
lit_suffix_length(lit_kind) |
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.
My two cents: these changes display the very problem with this lint.
It encourages people to just add some kind of assert-message to silent the lint, but the message provides zero information.
If foo.is_numeric()
returns false
and blows up an assert, I already know that foo
is not numeric and that this is problematic.
Why would would I want to waste time and add the same exact information "foo should be numeric" again, in a textual form (which just adds additional maintenance burden because now I have to additionally update the message every time I change the variable name, etc..) ?
IMO it would be good to add an additional message to the lint, something along the lines of
help: try to describe why the failing assert is problematic
. The inserted assert-message should be more about the why is it important to know that this failed
and not what exactly failed here
, because the code inside the assert already conveys that.
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 agree that the messages provided here are a bit meh. However, I would not say that this is a problem with the lint; we simply have no way of knowing whether an assert message will satisfy. However, having them at all will at least put them under the eyes of a potential reviewer. Also I think your point should be added to the lint's documentation.
Perhaps tne lint should be put in restriction
instead of pedantic
.
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.
Yes you're right about the messages, since we are not very familiar with the codebase we tried to come up with messages by quickly looking at the assertion but it was hard to come up with good messages.
I've added a help message and updated the lint's description.
Didn't know about restriction
, it makes more sense for this lint. I've reverted the "dogfood" commit, I believe someone with more familiarity with the codebase can enable the lint and come up with good assert messages in a follow-up PR.
I just looked and |
clippy_lints/src/lib.rs
Outdated
@@ -911,6 +912,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: | |||
store.register_late_pass(|_| Box::new(size_of_ref::SizeOfRef)); | |||
store.register_late_pass(|_| Box::new(multiple_unsafe_ops_per_block::MultipleUnsafeOpsPerBlock)); | |||
store.register_late_pass(|_| Box::new(extra_unused_type_parameters::ExtraUnusedTypeParameters)); | |||
store.register_pre_expansion_pass(|| Box::<missing_assert_message::MissingAssertMessage>::default()); |
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.
Unfortunately we don't want to be adding more pre expansion passes because it comes with a good number of bugs - #6610, it would want to be a late pass
We do have some utilities that would help out there: find_assert_args
and find_assert_eq_args
. Usage would be similar to
rust-clippy/clippy_lints/src/assertions_on_constants.rs
Lines 34 to 40 in 6444621
let Some(macro_call) = root_macro_call_first_node(cx, e) else { return }; | |
let is_debug = match cx.tcx.get_diagnostic_name(macro_call.def_id) { | |
Some(sym::debug_assert_macro) => true, | |
Some(sym::assert_macro) => false, | |
_ => return, | |
}; | |
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else { return }; |
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.
We started with a late pass and find_assert_args
functions, but I think there was a recent change in the compiler and assert macros expand to something else than find_assert_args
expects. So those functions don't return the correct PanicExpn
, we tried to fix it but couldn't do it :) I'll give it another go!
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.
☔ The latest upstream changes (presumably #10392) made this pull request unmergeable. Please resolve the merge conflicts. |
cc616a4
to
569fa96
Compare
/ping 😄 I believe all the comments are addressed and it's ready for another round of review |
@Alexendoo since you commented on the implementation, do you have an idea on how to get this to work as a LateLintPass? |
It's working as a late pass from the latest commit, it looks good to me at a glance |
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.
This looks good to me, I only have a last small doc suggestion.
/// 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. | ||
/// |
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.
☔ The latest upstream changes (presumably #10415) made this pull request unmergeable. Please resolve the merge conflicts. |
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: llogiq <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
This reverts commit ec65357.
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
Co-authored-by: Weihang Lo <[email protected]>
9c5ec69
to
b554ff4
Compare
Thank you! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Would it be possible to not trigger this lint in testing code? In tests the assert expression usually suffices but outside of tests the additional message is much more likely to be exposed to a non-developer. This is done for other lints such as |
Hey @mickvangelderen, that should already be the case. There might be some cases we fail to identify as a test function though, could you please share the code if possible? |
@unexge ah, I only read the lint documentation, not the code. Perhaps it should be added because I misjudged the false positive rate of this lint based on the current description. |
@mickvangelderen Yes, that's definitely a good point! I'll submit a PR. Thanks! |
Mention that `missing_assert_message` lint ignores test functions Updates `missing_assert_message`'s docs to reflect that it ignores test functions as pointed out by `@mickvangelderen` in #10362 (comment) --- changelog: [`missing_assert_message`]: Update docs to reflect this lint ignores test functions
Fixes #6207.
changelog: new lint: [
missing_assert_message
]: A new lint for checking assertions that doesn't have a custom panic message.#10362
r? @llogiq