-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Suggest A && B
for if A { B } else { false }
#14865
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
Comments
Complicated conditions tend to be longer, so at least they would get formatted across lines. I don't think I would ever see the Do you have a suggestion for the name. I'm at either |
Hm, brand new lint seems challenging - but would love to give it a go. |
The lint itself is fine. There is a question as to what category to put it in, but that can wait till a PR is made. |
Personally I would propose category (It's not correctness or performance, at least.) |
Uh oh!
There was an error while loading. Please reload this page.
What it does
Inspired by this code in the standard library: https://doc.rust-lang.org/src/std/collections/hash/set.rs.html#864-866
which could instead be
When the "then" block is a single simple expression (rather than a complex thing with a bunch of
let
s, say), seeingelse { false }
is a hint that the code can be simplified without changing behaviour or performance.Playground repro that this is not linted today: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=90c86a06c5fce6c0f816f5f4ec170cc5
Similarly, suggesting
A || B
instead ofif A { true } else { B }
would also be good.Advantage
&&
s be&&
s it helps emphasize the places that do need theif
-else
for their less-common operation.Drawbacks
If either condition is particularly complicated, splitting it up into the two pieces might help a reader grok the intent.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: