-
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 needless_bitwise_bool
lint
#7133
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. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Not a review. Just some comments.
This can't be a correction lint. It looks like there is no checking for side-effects when y is a function. I suspect this lint will have a lot of false positives since most people use |
Yeah, I would make it a pedantic lint or perhaps a style lint. But: Performance is actually tricky in that regard. Let's take an example: while To check for side effects, we could reuse some of the |
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.
So, to summarize:
- Small naming nit in the function.
- Lint only where
- the right hand side is a
Call
,MethodCall
,BinOp
orUnOp
and - the called method (or trait method) does not match``must_use::has_mutable_args(..)
nor
must_use::mutates_static(..)`. You may move the functions to `clippy_utils` for that.
- the right hand side is a
- Make it a pedantic lint (I would like to make it perf, but the checks don't capture all side effects)
- Change the docs to that effect. Write that it is likely to improve perf, but that the lint does not detect all possible side effects, so apply caution before applying the suggestion.
Thank you! This feedback is really helpful. Regarding renaming the lint: would |
☔ The latest upstream changes (presumably #7137) made this pull request unmergeable. Please resolve the merge conflicts. |
Now that I'm aware of the short-circuiting difference, I'm starting to question the value of this lint. If side-effects are possible, Clippy has no way of knowing the user's intent and can't lint. If side-effects are not possible (like OR'ing two variables), then maybe logical operators are more readable but it seems quite subjective in those cases. But my original idea with this lint was to strictly prefer logical operators for readability because the meaning is not "overloaded" with bitwise operations. For the name, I'd suggest |
@flip1995 Do you still think this should be |
I ran this lint on a bunch of crates. Here are some things I found.
|
Tangential, but this looks like another lint opportunity. |
@camsteffen no, that will likely short circuit if I'm not mistaken. The bitwise op here is by design so that the equality gets autovectorized. |
I can't remember when and where I thought this should be |
See link in OP. Or just forget it. It was over two years ago. 😄 I would think it compiles the same if LLVM can see there are no side-effects? Sounds like we can all agree on pedantic for now. |
Ah I only looked at the issues and missed that link. My views have definitely changed here. |
Okay thank you everyone for the feedback- I didn't expect this lint to garner so much discussion, but it's been illuminating to see the impacts of what at first appears to be a relatively benign change. I'll try and update the lint with the following changes:
I'll then go ahead and run the lints on the same crates that @mikerite did, and report back. Does this seem like a reasonable plan to everyone? |
I think you can just use |
092bd9d
to
d468dca
Compare
Hello everyone! Sorry for the delay- I believe I've implemented all the changes I outlined above. I took @camsteffen's suggestion and made the lint use For now the lint is very conservative; I've added some tests that I hope we could one day lint in the future, depending on how |
Thank you for seeing this through! And yes, the side effect check is overly pessimistic. As I wrote above, I had written my own one for the @bors r+ |
📌 Commit 5ba236f has been approved by |
Add `needless_bitwise_bool` lint fixes #6827 fixes #1594 changelog: Add ``[`needless_bitwise_bool`]`` lint Creates a new `bitwise_bool` lint to convert `x & y` to `x && y` when both `x` and `y` are booleans. I also had to adjust thh `needless_bool` lint slightly, and fix a couple failing dogfood tests. I made it a correctness lint as per flip1995's comment [here](#3385 (comment)), from a previous WIP attempt at this lint.
💔 Test failed - checks-action_test |
@bors r=llogiq |
📌 Commit 0dcde71 has been approved by |
Add `needless_bitwise_bool` lint fixes #6827 fixes #1594 changelog: Add ``[`needless_bitwise_bool`]`` lint Creates a new `bitwise_bool` lint to convert `x & y` to `x && y` when both `x` and `y` are booleans. I also had to adjust thh `needless_bool` lint slightly, and fix a couple failing dogfood tests. I made it a correctness lint as per flip1995's comment [here](#3385 (comment)), from a previous WIP attempt at this lint.
💔 Test failed - checks-action_test |
@bors retry |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Is the example in #1594 not spotted by this lint? fn foo2(x1: i32, x2: i32) {
if (x1 < x2) & (x1 != 0) {}
} |
fixes #6827
fixes #1594
changelog: Add
[`needless_bitwise_bool`]
lintCreates a new
bitwise_bool
lint to convertx & y
tox && y
when bothx
andy
are booleans. I also had to adjust thhneedless_bool
lint slightly, and fix a couple failing dogfood tests. I made it a correctness lint as per flip1995's comment here, from a previous WIP attempt at this lint.