Skip to content

Fix manual_is_variant_and condition generation #15206

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

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

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented Jul 4, 2025

When comparing x.map(func) == Some(bool_lit), the value of bool_lit was ignored, despite the fact that its value should determine the value of the proposed expression.

func can be either a closure or a path. For the latter, η-expansion will be used if needed to invert the result of the function call.

changelog: [manual_is_variant_and]: fix inverted suggestions that could lead to code with different semantics

Fixes #15202

Summary Notes

Managed by @rustbot—see help for details

@rustbot
Copy link
Collaborator

rustbot commented Jul 4, 2025

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 4, 2025
@samueltardieu
Copy link
Contributor Author

@rustbot note Beta-nomination
@rustbot label +beta-nominated +I-suggestion-causes-bug

Those extensions to manual_is_variant_and were introduced in Rust 1.89, now in beta. They cause compilable suggestions with bogus semantics. It would be preferable that they don't reach the stable version.

@rustbot rustbot added beta-nominated Nominated for backporting to the compiler in the beta channel. I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way labels Jul 4, 2025
@samueltardieu
Copy link
Contributor Author

Lintcheck run shows one fixed diagnostic which would have caused a bug if applied as-is (in cargo sources).

@GuillaumeGomez
Copy link
Member

It's pretty common for closure/function paths to be used and needing to be handled by clippy. We should maybe provide a utils function to handle it, no?

@samueltardieu
Copy link
Contributor Author

It's pretty common for closure/function paths to be used and needing to be handled by clippy. We should maybe provide a utils function to handle it, no?

Yes, probably, but usages may be different, for example here we need to invert the boolean result of the closure/function.

Btw, my latest push is only some cleanup to ease later maintenance, no change in functionality.

@GuillaumeGomez
Copy link
Member

It's pretty common for closure/function paths to be used and needing to be handled by clippy. We should maybe provide a utils function to handle it, no?

Yes, probably, but usages may be different, for example here we need to invert the boolean result of the closure/function.

Having a function to generate an enum closure/path would likely be a good first step and also likely to be useable everywhere. For the more specific parts, we can see afterwards.

@samueltardieu
Copy link
Contributor Author

Having a function to generate an enum closure/path would likely be a good first step and also likely to be useable everywhere. For the more specific parts, we can see afterwards.

An Expr variant for arbitrary expressions would also have to be added in addition to closure + path, as in:

fn make_func() -> impl Fn(u32) -> u32 {
    |x| x+1
}

fn main() {
    let a = Some(41);
    println!("{:?}", a.map(make_func()));
}

@samueltardieu
Copy link
Contributor Author

Anyway, I agree with your proposal. However, I would like to see it implemented after this is merged, so that this can be backported to beta, rather than before, because the backport would be much more involved.

@GuillaumeGomez
Copy link
Member

Not planning to do it right away. I plan to move your current enum, so I need this PR to be merge first. :3

When comparing `x.map(func) == Some(bool_lit)`, the value of `bool_lit` was
ignored, despite the fact that its value should determine the value of
the proposed expression.

`func` can be either a closure or a path. For the latter, η-expansion
will be used if needed to invert the result of the function call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-nominated Nominated for backporting to the compiler in the beta channel. I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way 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.

Incorrect suggestion by manual_is_variant_and
4 participants