Merge pull request #20543 from sgasho/fix/19443_replace_match_with_if_let
Some checks are pending
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run

Fix "Replace match with if let" not to trigger when invalid transformations occur
This commit is contained in:
Shoyu Vanilla (Flint) 2025-09-23 09:46:37 +00:00 committed by GitHub
commit 905641f352
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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),
}
}
",
)
}
}