Skip to content

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions clippy_lints/src/loops/manual_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::MANUAL_FLATTEN;
use super::utils::make_iterator_snippet;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{indent_of, reindent_multiline, snippet_with_applicability};
use clippy_utils::visitors::is_local_used;
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt};
use rustc_errors::Applicability;
Expand All @@ -28,7 +29,7 @@ pub(super) fn check<'tcx>(
&& let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind
&& path_to_local_id(let_expr, pat_hir_id)
// Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result`
&& let PatKind::TupleStruct(ref qpath, _, _) = let_pat.kind
&& let PatKind::TupleStruct(ref qpath, let_pats, _) = let_pat.kind
&& let Res::Def(DefKind::Ctor(..), ctor_id) = cx.qpath_res(qpath, let_pat.hir_id)
&& let Some(variant_id) = cx.tcx.opt_parent(ctor_id)
&& let some_ctor = cx.tcx.lang_items().option_some_variant() == Some(variant_id)
Expand All @@ -54,20 +55,35 @@ pub(super) fn check<'tcx>(
_ => "",
};

let sugg = format!("{arg_snippet}{copied}.flatten()");
let help_msg = "try `.flatten()` and remove the `if let` statement in the for loop";

// 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..."
let pat_snippet = if let_pats.is_empty() {
"_".to_string()
} else {
"...and remove the `if let` statement in the for loop"
snippet_with_applicability(
cx,
let_pats.first().unwrap().span.source_callsite(),
"_",
&mut applicability,
Copy link
Contributor

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?

)
.to_string()
};
let body_snippet =
snippet_with_applicability(cx, if_then.span.source_callsite(), "[body]", &mut applicability).to_string();
let suggestions = vec![
// flatten the iterator
(arg.span, format!("{arg_snippet}{copied}.flatten()")),
(pat.span, pat_snippet),
// remove the `if let` statement
(
body.span,
reindent_multiline(&body_snippet, true, indent_of(cx, body.span)),
),
];

span_lint_and_then(cx, MANUAL_FLATTEN, span, msg, |diag| {
diag.span_suggestion(arg.span, "try", sugg, applicability);
diag.span_help(inner_expr.span, help_msg);
diag.multipart_suggestion("try", suggestions, applicability);
});
}
}
108 changes: 67 additions & 41 deletions tests/ui/manual_flatten.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:7:5
|
LL | for n in x {
| ^ - help: try: `x.into_iter().flatten()`
| _____|
| |
LL | / for n in x {
LL | |
LL | |
LL | | if let Some(y) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:10:9
|
LL | / if let Some(y) = n {
Expand All @@ -21,14 +18,17 @@ LL | | }
| |_________^
= note: `-D clippy::manual-flatten` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_flatten)]`
help: try
|
LL ~ for y in x.into_iter().flatten() {
LL + println!("{}", y);
LL + }
|

error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:17:5
|
LL | for n in y.clone() {
| ^ --------- help: try: `y.clone().into_iter().flatten()`
| _____|
| |
LL | / for n in y.clone() {
LL | |
LL | |
LL | | if let Ok(n) = n {
Expand All @@ -37,145 +37,169 @@ LL | | };
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:20:9
|
LL | / if let Ok(n) = n {
LL | | println!("{}", n);
LL | | };
| |_________^
help: try
|
LL ~ for n in y.clone().into_iter().flatten() {
LL + println!("{}", n);
LL + }
|

error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:26:5
|
LL | for n in &y {
| ^ -- help: try: `y.iter().flatten()`
| _____|
| |
LL | / for n in &y {
LL | |
LL | |
LL | | if let Ok(n) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:29:9
|
LL | / if let Ok(n) = n {
LL | | println!("{}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in y.iter().flatten() {
LL + println!("{}", n);
LL + }
|

error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:36:5
|
LL | for n in z {
| ^ - help: try: `z.iter().flatten()`
| _____|
| |
LL | / for n in z {
LL | |
LL | |
LL | | if let Ok(n) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:39:9
|
LL | / if let Ok(n) = n {
LL | | println!("{}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in z.iter().flatten() {
LL + println!("{}", n);
LL + }
|

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:47:5
|
LL | for n in z {
| ^ - help: try: `z.flatten()`
| _____|
| |
LL | / for n in z {
LL | |
LL | |
LL | | if let Some(m) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:50:9
|
LL | / if let Some(m) = n {
LL | | println!("{}", m);
LL | | }
| |_________^
help: try
|
LL ~ for m in z.flatten() {
LL + println!("{}", m);
LL + }
Comment on lines +124 to +126
Copy link
Contributor Author

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.

|

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:82:5
|
LL | for n in &vec_of_ref {
| ^ ----------- help: try: `vec_of_ref.iter().copied().flatten()`
| _____|
| |
LL | / for n in &vec_of_ref {
LL | |
LL | |
LL | | if let Some(n) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:85:9
|
LL | / if let Some(n) = n {
LL | | println!("{:?}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in vec_of_ref.iter().copied().flatten() {
LL + println!("{:?}", n);
LL + }
|

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:91:5
|
LL | for n in vec_of_ref {
| ^ ---------- help: try: `vec_of_ref.iter().copied().flatten()`
| _____|
| |
LL | / for n in vec_of_ref {
LL | |
LL | |
LL | | if let Some(n) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:94:9
|
LL | / if let Some(n) = n {
LL | | println!("{:?}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in vec_of_ref.iter().copied().flatten() {
LL + println!("{:?}", n);
LL + }
|

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:100:5
|
LL | for n in slice_of_ref {
| ^ ------------ help: try: `slice_of_ref.iter().copied().flatten()`
| _____|
| |
LL | / for n in slice_of_ref {
LL | |
LL | |
LL | | if let Some(n) = n {
... |
LL | | }
| |_____^
|
help: ...and remove the `if let` statement in the for loop
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:103:9
|
LL | / if let Some(n) = n {
LL | | println!("{:?}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in slice_of_ref.iter().copied().flatten() {
LL + println!("{:?}", n);
LL + }
|

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:132:5
Expand All @@ -188,7 +212,7 @@ LL | | Some(1),
LL | | }
| |_____^
|
help: remove the `if let` statement in the for loop and then...
help: try `.flatten()` and remove the `if let` statement in the for loop
--> tests/ui/manual_flatten.rs:139:9
|
LL | / if let Some(n) = n {
Expand All @@ -201,6 +225,8 @@ LL | for n in vec![
...
LL | Some(3)
LL ~ ].iter().flatten() {
LL + println!("{:?}", n);
LL + }
|

error: aborting due to 9 previous errors
Expand Down