Skip to content
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

fix incorrect suggestion for !(a >= b) as i32 == c #13338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CoCo-Japan-pan
Copy link

fixes #12761

The expression !(a >= b) as i32 == c got simplified to a < b as i32 == c, but this is a syntax error.
The result we want is (a < b) as i32 == c.
This is fixed by adding a parenthesis to the suggestion given in check_simplify_not when the boolean expression is casted.

changelog: [nonminimal_bool]: fix incorrect suggestion for !(a >= b) as i32 == c

@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (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 3, 2024
Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we should use Sugg::maybe_par in clippy_utils for this. The code is correct for this case, but it's missing other cases (mostly binary ops) where parentheses are required. For example, !(1 >= 2) | true == a also causes the same issue, which the suggested function handles.

@CoCo-Japan-pan
Copy link
Author

I wasn't familiar with the clippy_utils::sugg, and I think my code is redundant, but I'll push this for now.
I thought we have to keep the parent expression, so I added a stack, but maybe just applying Sugg::maybe_par to the suggestion in let Some(suggestion) = simplify_not(cx, inner) was sufficient?

@CoCo-Japan-pan
Copy link
Author

CoCo-Japan-pan commented Sep 12, 2024

Given the fact that simplify_not only deals with ExprKind::Binary and ExprKind::MethodCall, I believe the problem occurs only when we simplify ExprKind::Binary, and the expression belongs to ExprKind::Cast or ExprKind::Binary (please let me know if there are more), so I changed the need_parens logic.

@bors
Copy link
Collaborator

bors commented Oct 2, 2024

☔ The latest upstream changes (presumably #13443) made this pull request unmergeable. Please resolve the merge conflicts.

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.

suggestion from nonminimal_bool with integer cast creates a syntax error due to parentheses removal
4 participants