From 09186d3f811c97eb0d77c74089e6bdccfa1cae8a Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Sat, 30 Aug 2025 22:25:50 +0800 Subject: [PATCH] Fix nested if-let for merge_nested_if ```rust fn f() { $0if let Some(x) = y { if x == 4 { 1 } } } ``` -> ```rust fn f() { if let Some(x) = y && x == 4 { 1 } } ``` --- .../src/handlers/merge_nested_if.rs | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/merge_nested_if.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/merge_nested_if.rs index 73cb8204f209..b3487bb62364 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/merge_nested_if.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/merge_nested_if.rs @@ -1,4 +1,3 @@ -use ide_db::syntax_helpers::node_ext::is_pattern_cond; use syntax::{ T, ast::{self, AstNode, BinaryOp}, @@ -39,10 +38,6 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt } let cond = expr.condition()?; - //should not apply for if-let - if is_pattern_cond(cond.clone()) { - return None; - } let cond_range = cond.syntax().text_range(); @@ -62,9 +57,6 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt return None; } let nested_if_cond = nested_if_to_merge.condition()?; - if is_pattern_cond(nested_if_cond.clone()) { - return None; - } let nested_if_then_branch = nested_if_to_merge.then_branch()?; let then_branch_range = then_branch.syntax().text_range(); @@ -171,6 +163,24 @@ mod tests { ) } + #[test] + fn merge_nested_if_test8() { + check_assist( + merge_nested_if, + "fn f() { i$0f let Some(x) = y { if x == 4 { 1 } } }", + "fn f() { if let Some(x) = y && x == 4 { 1 } }", + ) + } + + #[test] + fn merge_nested_if_test9() { + check_assist( + merge_nested_if, + "fn f() { i$0f y == 0 { if let Some(x) = y { 1 } } }", + "fn f() { if y == 0 && let Some(x) = y { 1 } }", + ) + } + #[test] fn merge_nested_if_do_not_apply_to_if_with_else_branch() { check_assist_not_applicable( @@ -187,22 +197,6 @@ mod tests { ) } - #[test] - fn merge_nested_if_do_not_apply_to_if_let() { - check_assist_not_applicable( - merge_nested_if, - "fn f() { i$0f let Some(x) = y { if x == 4 { 1 } } }", - ) - } - - #[test] - fn merge_nested_if_do_not_apply_to_nested_if_let() { - check_assist_not_applicable( - merge_nested_if, - "fn f() { i$0f y == 0 { if let Some(x) = y { 1 } } }", - ) - } - #[test] fn merge_nested_if_do_not_apply_to_if_with_else_branch_and_nested_if() { check_assist_not_applicable(