Merge pull request #20912 from A4-Tacks/left-side-in-cond

Fix not complete `let` before expr in condition
This commit is contained in:
Lukas Wirth
2025-10-26 07:19:04 +00:00
committed by GitHub
2 changed files with 8 additions and 0 deletions

View File

@@ -460,6 +460,8 @@ pub(crate) fn is_in_condition(it: &ast::Expr) -> bool {
ast::MatchGuard(guard) => guard.condition()? == *it,
ast::BinExpr(bin_expr) => (bin_expr.op_token()?.kind() == T![&&])
.then(|| is_in_condition(&bin_expr.into()))?,
ast::Expr(expr) => (expr.syntax().text_range().start() == it.syntax().text_range().start())
.then(|| is_in_condition(&expr))?,
_ => return None,
} })
})

View File

@@ -3268,6 +3268,12 @@ fn foo() -> (i32, i32) {
#[test]
fn let_in_condition() {
check_edit("let", r#"fn f() { if $0 {} }"#, r#"fn f() { if let $1 = $0 {} }"#);
check_edit("let", r#"fn f() { if $0x {} }"#, r#"fn f() { if let $1 = $0x {} }"#);
check_edit(
"let",
r#"fn f() { if $0foo.bar() {} }"#,
r#"fn f() { if let $1 = $0foo.bar() {} }"#,
);
}
#[test]