-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fixes manual_flatten
removes the useless if let
#14861
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
e9606eb
to
ac06a73
Compare
manual_flatten
removes the useless if let
// If suggestion is not a one-liner, it won't be shown inline within the error message. In that | ||
// case, it will be shown in the extra `help` message at the end, which is why the first | ||
// `help_msg` needs to refer to the correct relative position of the suggestion. | ||
let help_msg = if sugg.contains('\n') { | ||
"remove the `if let` statement in the for loop and then..." | ||
} else { | ||
"...and remove the `if let` statement in the for loop" | ||
}; |
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.
The suggestion always have multiple lines, so we can become the help message simplify.
(or should we suggest the one-liner and separate the suggestion that includes removing if let
?)
rustbot has assigned @samueltardieu. Use |
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.
If this is supposed to now be auto-fixable, then //@no-rustfix
should be removed from the test file.
However, the current suggestion don't appear to be valid unless the binding from the Some()
or Ok()
is a single identifier with the same name as the identifier from the for
loop.
For example, given this code:
for x in [Some(3)] {
if let Some(y) = x {
println!("{y}");
}
}
the suggestion will not compile:
for x in [Some(3)].into_iter().flatten() {
println!("{y}");
}
LL ~ for m in z.flatten() { | ||
LL + println!("{}", m); | ||
LL + } |
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'd fixed it to out valid code.
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.
This is getting good.
//@no-rustfix
is still in place and should be removed, so that autofixes can be tested as well.
Also, the following code makes the lint fail to give a good error message and fix the problem:
macro_rules! inner {
($id:ident / $new:pat => $action:expr) => { if let Some($new) = $id { $action; } }
}
for ab in [Some((1, 2)), Some((3, 4))] {
inner!(ab / (c, d) => println!("{c}-{d}"));
}
because it gets confused by the macro. The lint should probably not trigger if the for
loop, the if let
, or its test, don't all come from the same context.
cx, | ||
let_pats.first().unwrap().span.source_callsite(), | ||
"_", | ||
&mut applicability, |
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.
Shouldn't you initialize applicability
with MachineApplicable
if the suggestion is complete?
3a54cb7
to
d4b1e62
Compare
Thank you for pointing that out. I made corrections to prevent the macro from triggering a lint. I also adjusted the Applicability. |
☔ The latest upstream changes (possibly 3927a61) made this pull request unmergeable. Please resolve the merge conflicts. |
Closes #14692
The suggestion of
manual_flatten
does not includes the replacement ofif let
so far despite of.flatten()
suggestion.This PR eliminates a redundant
if let
.changelog: [
manual_flatten
] the suggestion removesif let