-
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 lint to check for boolean comparison in assert macro calls #7083
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
3375908
to
3f2c607
Compare
I'm pretty sure we are moving away from pre-expansion passes. Sorry that that means you need to re-work this to inspect expanded code. We have a util function Tagging @rust-lang/clippy for input on this. |
Makes sense. I'll rewrite it then. |
Rewrite done. I'm very unhappy with the result though: if any of the macro gets updated, the lint will break. Also, the suggestion now is much worse since I can't simply show the macro arguments... Anyway, at least now it's fully running on early pass and doesn't require macro expansion anymore! |
Yeah, that's fine, we have a fair number of lints that do that kind of thing, which is why we have all these desugaring functions in |
6b5312b
to
1a43e15
Compare
CI finally passed! \o/ |
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.
Not sure what the file /bool_assert_eq
is. Probably need to remove.
for mac in macros.iter().chain(inverted_macros.iter()) { | ||
if is_direct_expn_of(e.span, mac).is_some() { |
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.
You can use the Span
from on is_direct_expn_of
to lint the entire macro invocation, not just the bool arg. This makes more sense because you are suggesting to use a different macro.
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.
Right, now that I don't show the full result anymore, it doesn't make much sense anymore...
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.
What do you mean by "the full result"?
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 suggestion doesn't show anymore what the assert!
call should look like since that the snippet
span returns the macro "definition" and not the arguments anymore.
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.
When you use snippet
, you have to use spans that only include the arg passed to the macro. For example, assert_eq!(a, true)
will expand to match (&a, &true) { .. }
. From that, you need to get the span of just a
.
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.
It's still not good in case you have more than two arguments. Getting the arguments starting from the third is a nightmare... It was easy to do in the pre_expansion pass, but now it's just trying to go through the macro source code from a token tree.
66f5ecb
to
7403737
Compare
7403737
to
cbb0039
Compare
for mac in macros.iter().chain(inverted_macros.iter()) { | ||
if is_direct_expn_of(e.span, mac).is_some() { |
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.
When you use snippet
, you have to use spans that only include the arg passed to the macro. For example, assert_eq!(a, true)
will expand to match (&a, &true) { .. }
. From that, you need to get the span of just a
.
4f5e5bc
to
6c8e925
Compare
Updated! |
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.
A couple minor changes and then LGTM! Please squash your commits.
6c8e925
to
c050a4d
Compare
I'd rather keep the first commit if you don't mind. It was more complete (in its suggestion) and it can be used for potential future migrations if anyone needs to do the same (I might haha). |
aefee1a
to
5a54c3f
Compare
No, I don't see how it would be useful. Pre-expansion passes are going away. Migrating to an early pass will always require re-thinking the implementation. I added a comment to hopefully stop people short of wring pre-exapnsion passes in the future. |
Makes me sad but ok. Just waiting for our last discussion to be resolved and then I'll squash. |
5a54c3f
to
eb1d71f
Compare
Removed the support of |
Sorry just thought of one more case that I think is missing: |
|
Ah nevermind! It actually throws an error. Interesting... |
eb1d71f
to
1dd3210
Compare
1dd3210
to
e2e104b
Compare
Updated! |
Is there anything else for me to do in here? |
Nope! Thanks! @bors r+ |
📌 Commit e2e104b has been approved by |
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!
💔 Test failed - checks-action_test |
There is a failure because there is a missing "changelog" in the PR body but can't see anything about it in https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md. If someone has a link or a tip on what I need to update? :) |
@bors retry added a changelog line |
Thank you! |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Hey, was there a reason why the lint suggestion doesn't snip the non literal expression into the suggested code change? 🙃 |
If you have an improvement in mind, don't hesitate to send it or at least open an issue with your suggestion. |
This PR adds a lint to check if an assert macro is using a boolean as "comparison value". For example:
Could be rewritten as:
PS: The dev guidelines are amazing. Thanks a lot for writing them!
changelog: Add
bool_assert_comparison
lint