Skip to content

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

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

SpriteOvO
Copy link
Contributor

@SpriteOvO SpriteOvO commented Sep 23, 2024

Closes #13436.

Improved based on the existing lint nonminimal_bool, since there is already handling of similar methods Option::{is_some,is_none} and Result::{is_ok,is_err}, and there is a lot of reusable code.

When is_some_and or is_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?)

// Currently will not simplify this
_ = !opt.is_some_and(|x| {
    let complex_block = 100;
    x == complex_block
});

changelog: [nonminimal_bool]: Simplify negative Option::{is_some_and,is_none_or}

@rustbot
Copy link
Collaborator

rustbot commented Sep 23, 2024

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 23, 2024
@SpriteOvO SpriteOvO force-pushed the simplify-option-neg-methods branch 3 times, most recently from 660c59f to 457d8a3 Compare September 23, 2024 18:27
_ = !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
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@SpriteOvO SpriteOvO Sep 29, 2024

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.

Copy link
Member

@xFrednet xFrednet left a 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

@SpriteOvO SpriteOvO force-pushed the simplify-option-neg-methods branch from 457d8a3 to f17bc11 Compare September 29, 2024 19:19
@SpriteOvO
Copy link
Contributor Author

@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. 😄

@SpriteOvO SpriteOvO force-pushed the simplify-option-neg-methods branch from f17bc11 to 762a91b Compare September 29, 2024 19:36
@xFrednet
Copy link
Member

xFrednet commented Oct 2, 2024

This version looks good to me, thank you for the changes! :D

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.

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,
Violets are blue,
Let's get this merged,
here and now!

With a special thanks to @SpriteOvO

@bors
Copy link
Contributor

bors commented Oct 2, 2024

📌 Commit 762a91b has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Oct 2, 2024

⌛ Testing commit 762a91b with merge 398be8c...

@bors
Copy link
Contributor

bors commented Oct 2, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 398be8c to master...

@bors bors merged commit 398be8c into rust-lang:master Oct 2, 2024
8 checks passed
@SpriteOvO SpriteOvO deleted the simplify-option-neg-methods branch October 2, 2024 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest using opt.is_none_or instead of !opt.is_some_and
4 participants