-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint: if let Some iter element #6564
Comments
Nice suggestion Cam 😄 Can I take this issue? |
@nahuakang yes! |
It can also cover |
@flip1995 @camsteffen I've started looking into the code base. Seems like this lint should be included in Cam, can I cover the original case first before looking into |
Sounds like a good start!
Absolutely. You can always leave some cases to be added later as an enhancement. You also can open a PR to get feedback before you are done. Here are some examples (each can be changed to use for n in vec![Some(1)] {
// if let Some
if let Some(n) = n {
println!("sup");
}
}
let vec: Vec<Result<i32, i32>> = vec![];
for n in vec.clone() {
// if let Ok
if let Ok(n) = n {
println!("sup");
}
}
for n in vec.clone() {
// Option::map or Result::map
n.map(|n| println!("sup"));
}
// for_each and match
Some(Some(1)).into_iter().for_each(|n| match n {
Some(_) => println!("sup"),
None => {}
}); One note that may help is that Good luck! |
You have to be careful that the |
…flip1995 New Lint: Manual Flatten This is a draft PR for [Issue 6564](#6564). r? `@camsteffen` - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` --- *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Add new lint `manual_flatten` to check for loops over a single `if let` expression with `Result` or `Option`.
…flip1995 New Lint: Manual Flatten This is a draft PR for [Issue 6564](#6564). r? `@camsteffen` - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` --- *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Add new lint [`manual_flatten`] to check for loops over a single `if let` expression with `Result` or `Option`.
Implemented in #6646 |
What it does
Checks for iteration of
Option
s withif let Some
inside. Can useflatten()
instead. The lint should also work initer.for_each()
.Categories (optional)
What is the advantage of the recommended code over the original code
It is simpler.
Drawbacks
None.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: