Skip to content

[manual_flatten]: Fix with nested Some or Ok pattern #14846

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

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions clippy_lints/src/loops/manual_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::utils::make_iterator_snippet;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::visitors::is_local_used;
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt};
use clippy_utils::{higher, is_refutable, path_to_local_id, peel_blocks_with_stmt};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, Pat, PatKind};
Expand All @@ -28,7 +28,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, [inner_pat], _) = 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 @@ -37,6 +37,7 @@ pub(super) fn check<'tcx>(
// Ensure expr in `if let` is not used afterwards
&& !is_local_used(cx, if_then, pat_hir_id)
&& msrv.meets(cx, msrvs::ITER_FLATTEN)
&& !is_refutable(cx, inner_pat)
{
let if_let_type = if some_ctor { "Some" } else { "Ok" };
// Prepare the error message
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/manual_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ fn main() {
println!("{}", n);
}

// Using nested `Some` pattern should not trigger the lint
for n in vec![Some((1, Some(2)))] {
if let Some((_, Some(n))) = n {
println!("{}", n);
}
}

run_unformatted_tests();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/manual_flatten.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ LL | | }
| |_________^

error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> tests/ui/manual_flatten.rs:132:5
--> tests/ui/manual_flatten.rs:139:5
|
LL | / for n in vec![
LL | |
Expand All @@ -189,7 +189,7 @@ LL | | }
| |_____^
|
help: remove the `if let` statement in the for loop and then...
--> tests/ui/manual_flatten.rs:139:9
--> tests/ui/manual_flatten.rs:146:9
|
LL | / if let Some(n) = n {
LL | | println!("{:?}", n);
Expand Down