-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Conversation
Lintcheck run shows one fixed diagnostic which would have caused a bug if applied as-is (in cargo sources). |
It's pretty common for closure/function paths to be used and needing to be handled by clippy. We should maybe provide a |
cfb57d8
to
eb12c32
Compare
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. |
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 fn make_func() -> impl Fn(u32) -> u32 {
|x| x+1
}
fn main() {
let a = Some(41);
println!("{:?}", a.map(make_func()));
} |
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. |
Not planning to do it right away. I plan to move your current enum, so I need this PR to be merge first. :3 |
eb12c32
to
8eb2320
Compare
error: called `.map() != Ok()` | ||
--> tests/ui/manual_is_variant_and.rs:232:18 | ||
| | ||
LL | let a1 = b.map(iad) != Ok(false); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use: `!b.is_ok_and(|x| !iad(x))` |
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.
I have to say this case stretches the "readability" point of the lint's suggestion a little bit IMO 😄 This took me a bit to mentally parse and prove that it's equivalent. The error message of the lint is also kinda terse and doesn't add any extra information.
But, those are pre-existing issues I guess, so not something we have to figure out here
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.
8eb2320
to
d8ba94b
Compare
When comparing
x.map(func) == Some(bool_lit)
, the value ofbool_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 semanticsFixes #15202
Summary Notes
Managed by
@rustbot
—see help for details