-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Simplify negative Option::{is_some_and,is_none_or}
#13443
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @xFrednet (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
660c59f
to
457d8a3
Compare
_ = !opt.is_some_and(|x| x > 1000); //~ ERROR: this boolean expression can be simplified | ||
_ = !opt.is_some_and(|x| x >= 1000); //~ ERROR: this boolean expression can be simplified | ||
_ = !opt.is_some_and(|x| x == 1000); //~ ERROR: this boolean expression can be simplified | ||
_ = !opt.is_some_and(|x| x != 1000); //~ ERROR: this boolean expression can be simplified |
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.
Since simplify_not
is called recursively on closures, could you add a recursive test? Something like this:
let opt_opt = Some(opt);
!opt_opt.is_some_and(|x| !x.is_some_and(|y| y != 1000));
I just want to make sure that the output is correct
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.
Nice catch! The test triggers an error cannot replace slice of data that was already replaced
. I'm trying to fix it.
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.
Ahh, that happens if two suggestions overlap. Don't spend too much time on it. Overlapping suggestions are okay if they are still correct. You can also just create a second test file that doesn't run rustfix.
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.
Learned it! It looks like for users, rustfix will apply the first fix and ignore the subsequent. So !opt_opt.is_some_and(|x| !x.is_some_and(|y| y != 1000))
will be fixed to opt_opt.is_none_or(|x| x.is_some_and(|y| y != 1000))
.
I have added the test case to the PR.
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.
Hey and welcome to Clippy. Excellent start! I only have one tiny comment, for an additional test and then this will be good to go! :D
457d8a3
to
f17bc11
Compare
@xFrednet Thanks for the review! I'm trying to start regularly contributing to Rust, as the docs said clippy is a good place to start, hopefully I can stick with it. 😄 |
f17bc11
to
762a91b
Compare
This version looks good to me, thank you for the changes! :D
That's what got me into Clippy as well. It felt like rustc was way too complex and Clippy is more approachable with focussed lint passes and reasonable compile times. And now I stick around due to the people and history :D Good luck! I hope you find interesting issues to work on =^.^= Roses are red, With a special thanks to @SpriteOvO |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Closes #13436.
Improved based on the existing lint
nonminimal_bool
, since there is already handling of similar methodsOption::{is_some,is_none}
andResult::{is_ok,is_err}
, and there is a lot of reusable code.When
is_some_and
oris_none_or
have a negation, we invert it into another method by removing the Not sign and inverting the expression in the closure.For the case where the closure block has statements, currently no simplification is implemented. (Should we do it?)
changelog: [
nonminimal_bool
]: Simplify negativeOption::{is_some_and,is_none_or}