diff options
| author | Shoyu Vanilla (Flint) <modulo641@gmail.com> | 2025-09-23 09:46:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-23 09:46:37 +0000 |
| commit | aca434057fc674b0ed1fbe001541116da8b988cb (patch) | |
| tree | 9f842932e53acb1e47a983a906ac404160d25f6e /src | |
| parent | 1393e638bd29f24203c5f153a863a5c18464e304 (diff) | |
| parent | cee25947df4f0d3cb455810b64cd62c16d775109 (diff) | |
Merge pull request #20543 from sgasho/fix/19443_replace_match_with_if_let
Fix "Replace match with if let" not to trigger when invalid transformations occur
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs index dd244375dc9..3b815a467bc 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs @@ -328,7 +328,14 @@ fn pick_pattern_and_expr_order( (pat, pat2) => match (binds_name(sema, &pat), binds_name(sema, &pat2)) { (true, true) => return None, (true, false) => (pat, guard, expr, expr2), - (false, true) => (pat2, guard2, expr2, expr), + (false, true) => { + // This pattern triggers an invalid transformation. + // See issues #11373, #19443 + if let ast::Pat::IdentPat(_) = pat2 { + return None; + } + (pat2, guard2, expr2, expr) + } _ if is_sad_pat(sema, &pat) => (pat2, guard2, expr2, expr), (false, false) => (pat, guard, expr, expr2), }, @@ -1892,4 +1899,19 @@ fn main() { "#, ) } + + #[test] + fn test_replace_match_with_if_let_not_applicable_pat2_is_ident_pat() { + check_assist_not_applicable( + replace_match_with_if_let, + r" +fn test(a: i32) { + match$0 a { + 1 => code(), + other => code(other), + } +} +", + ) + } } |
