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

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

CoCo-Japan-pan
Copy link
Contributor

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
Contributor 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
Contributor 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.

@Centri3
Copy link
Member

Centri3 commented Oct 24, 2024

Well, this code is incorrect when the suggestion is applied:

fn a(a: bool) -> bool {
    (!(4 > 3)).b()
}

trait B {
    fn b(&self) -> bool { true }
}

impl B for bool {}

Really, is there any reason not to use Sugg directly? It's easier (and much less error prone in the future) than getting the parent expression as well.

@CoCo-Japan-pan
Copy link
Contributor Author

Using Sugg::maybe_par would be like this?
Or should we get the modified Expr (not just the String) from simplify_not, and then apply maybe_par?
Currently it doesn't see the parent expression, and adds an unnecessary parenthesis to some expressions like below.

if !(12 == a) {}
   ^^^^^^^^^^ help: try: `(12 != a)`

@CoCo-Japan-pan
Copy link
Contributor Author

CoCo-Japan-pan commented Oct 24, 2024

Excluded the case when the parent expression is the ExprKind::DropTemps(_) to avoid adding unnecessary parenthesis.

@CoCo-Japan-pan CoCo-Japan-pan marked this pull request as ready for review October 24, 2024 17:43
@Centri3
Copy link
Member

Centri3 commented Oct 24, 2024

and adds an unnecessary parenthesis to some expressions like below.

Yes, I know, this is a non-issue as unused_parens will almost always catch it and if it doesn't that's fine too.

Why do we need the parent expression? Is there any case where it misses required parentheses in that case? This feels overly complicated for 2 characters that will likely be removed on the next clippy run.

@CoCo-Japan-pan
Copy link
Contributor Author

Deleted the parent expression stuff.

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.

Thanks 👍

@Centri3
Copy link
Member

Centri3 commented Oct 28, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 28, 2024

📌 Commit d30a026 has been approved by Centri3

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Oct 28, 2024

⌛ Testing commit d30a026 with merge 2e4a11e...

@bors
Copy link
Collaborator

bors commented Oct 28, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Centri3
Pushing 2e4a11e to master...

@bors bors merged commit 2e4a11e into rust-lang:master Oct 28, 2024
8 checks passed
@CoCo-Japan-pan CoCo-Japan-pan deleted the nonminimal_bool_casted branch October 28, 2024 14:14
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