-
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
Open
donkomura
wants to merge
5
commits into
rust-lang:master
Choose a base branch
from
donkomura:manual_flatten_snippet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac06a73
flatten and remove the if let pat
donkomura f25207a
replace for pat to if_let pat
donkomura 029bde5
remove no-rustfix
donkomura f4242ee
do not trigger macro expansions
donkomura d4b1e62
applicability: MachineApplicable if the suggestion is complete, other…
donkomura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#![warn(clippy::manual_flatten)] | ||
#![allow(clippy::useless_vec, clippy::uninlined_format_args)] | ||
|
||
fn main() { | ||
// Test for loop over implicitly adjusted `Iterator` with `if let` expression | ||
let x = vec![Some(1), Some(2), Some(3)]; | ||
for y in x.into_iter().flatten() { | ||
println!("{}", y); | ||
} | ||
|
||
// Test for loop over implicitly adjusted `Iterator` with `if let` statement | ||
let y: Vec<Result<i32, i32>> = vec![]; | ||
for n in y.clone().into_iter().flatten() { | ||
println!("{}", n); | ||
} | ||
|
||
// Test for loop over by reference | ||
for n in y.iter().flatten() { | ||
println!("{}", n); | ||
} | ||
|
||
// Test for loop over an implicit reference | ||
let z = &y; | ||
for n in z.iter().flatten() { | ||
println!("{}", n); | ||
} | ||
|
||
// Test for loop over `Iterator` with `if let` expression | ||
let z = vec![Some(1), Some(2), Some(3)]; | ||
let z = z.iter(); | ||
for m in z.flatten() { | ||
println!("{}", m); | ||
} | ||
|
||
// Using the `None` variant should not trigger the lint | ||
// Note: for an autofixable suggestion, the binding in the for loop has to take the | ||
// name of the binding in the `if let` | ||
let z = vec![Some(1), Some(2), Some(3)]; | ||
for n in z { | ||
if n.is_none() { | ||
println!("Nada."); | ||
} | ||
} | ||
|
||
// Using the `Err` variant should not trigger the lint | ||
for n in y.clone() { | ||
if let Err(e) = n { | ||
println!("Oops: {}!", e); | ||
} | ||
} | ||
|
||
// Having an else clause should not trigger the lint | ||
for n in y.clone() { | ||
if let Ok(n) = n { | ||
println!("{}", n); | ||
} else { | ||
println!("Oops!"); | ||
} | ||
} | ||
|
||
let vec_of_ref = vec![&Some(1)]; | ||
for n in vec_of_ref.iter().copied().flatten() { | ||
println!("{:?}", n); | ||
} | ||
|
||
let vec_of_ref = &vec_of_ref; | ||
for n in vec_of_ref.iter().copied().flatten() { | ||
println!("{:?}", n); | ||
} | ||
|
||
let slice_of_ref = &[&Some(1)]; | ||
for n in slice_of_ref.iter().copied().flatten() { | ||
println!("{:?}", n); | ||
} | ||
|
||
struct Test { | ||
a: usize, | ||
} | ||
|
||
let mut vec_of_struct = [Some(Test { a: 1 }), None]; | ||
|
||
// Usage of `if let` expression should not trigger lint | ||
for n in vec_of_struct.iter_mut() { | ||
if let Some(z) = n { | ||
*n = None; | ||
} | ||
} | ||
|
||
// Using manual flatten should not trigger the lint | ||
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() { | ||
println!("{}", n); | ||
} | ||
|
||
macro_rules! inner { | ||
($id:ident / $new:pat => $action:expr) => { | ||
if let Some($new) = $id { | ||
$action; | ||
} | ||
}; | ||
} | ||
|
||
// Usage of `if let` expression with macro should not trigger lint | ||
for ab in [Some((1, 2)), Some((3, 4))] { | ||
inner!(ab / (c, d) => println!("{c}-{d}")); | ||
} | ||
|
||
macro_rules! args { | ||
($($arg:expr),*) => { | ||
vec![$(Some($arg)),*] | ||
}; | ||
} | ||
|
||
// Usage of `if let` expression with macro should not trigger lint | ||
for n in args!(1, 2, 3) { | ||
if let Some(n) = n { | ||
println!("{:?}", n); | ||
} | ||
} | ||
|
||
// This should trigger the lint, but the applicability is `MaybeIncorrect` | ||
let z = vec![Some(1), Some(2), Some(3)]; | ||
for n in z.into_iter().flatten() { | ||
println!("{:?}", n); | ||
} | ||
|
||
run_unformatted_tests(); | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn run_unformatted_tests() { | ||
// Skip rustfmt here on purpose so the suggestion does not fit in one line | ||
for n in vec![ | ||
//~^ manual_flatten | ||
|
||
Some(1), | ||
Some(2), | ||
Some(3) | ||
].iter().flatten() { | ||
println!("{:?}", n); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
withMachineApplicable
if the suggestion is complete?