Skip to content

Commit d42c81e

Browse files
committed
Fix extra semicolon before else in let-stmt
Example --- ```rust fn main() { let x = if true { () } $0 else {}; } ``` **Before this PR**: ```rust fn main() { let x = if true { () } else if $1 { $0 }; else {}; } ``` **After this PR**: ```rust fn main() { let x = if true { () } else if $1 { $0 } else {}; } ```
1 parent db0420c commit d42c81e

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

crates/ide-completion/src/completions/keyword.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,46 @@ fn main() {
269269
"#,
270270
);
271271

272+
check_edit(
273+
"else if",
274+
r#"
275+
fn main() {
276+
let x = if true {
277+
()
278+
} $0 else {};
279+
}
280+
"#,
281+
r#"
282+
fn main() {
283+
let x = if true {
284+
()
285+
} else if $1 {
286+
$0
287+
} else {};
288+
}
289+
"#,
290+
);
291+
292+
check_edit(
293+
"else if",
294+
r#"
295+
fn main() {
296+
let x = if true {
297+
()
298+
} $0 else if true {};
299+
}
300+
"#,
301+
r#"
302+
fn main() {
303+
let x = if true {
304+
()
305+
} else if $1 {
306+
$0
307+
} else if true {};
308+
}
309+
"#,
310+
);
311+
272312
check_edit(
273313
"else",
274314
r#"

crates/ide-completion/src/context/analysis.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,16 @@ fn classify_name_ref<'db>(
970970
let after_incomplete_let = |node: SyntaxNode| {
971971
prev_expr(node).and_then(|it| it.syntax().parent()).and_then(ast::LetStmt::cast)
972972
};
973+
let before_else_kw = |node: &SyntaxNode| {
974+
node.parent()
975+
.and_then(ast::ExprStmt::cast)
976+
.filter(|stmt| stmt.semicolon_token().is_none())
977+
.and_then(|stmt| non_trivia_sibling(stmt.syntax().clone().into(), Direction::Next))
978+
.and_then(NodeOrToken::into_node)
979+
.filter(|next| next.kind() == SyntaxKind::ERROR)
980+
.and_then(|next| next.first_token())
981+
.is_some_and(|token| token.kind() == SyntaxKind::ELSE_KW)
982+
};
973983

974984
// We do not want to generate path completions when we are sandwiched between an item decl signature and its body.
975985
// ex. trait Foo $0 {}
@@ -1276,7 +1286,7 @@ fn classify_name_ref<'db>(
12761286
.parent()
12771287
.and_then(ast::LetStmt::cast)
12781288
.is_some_and(|it| it.semicolon_token().is_none())
1279-
|| after_incomplete_let && incomplete_expr_stmt.unwrap_or(true);
1289+
|| after_incomplete_let && incomplete_expr_stmt.unwrap_or(true) && !before_else_kw(it);
12801290
let in_value = it.parent().and_then(Either::<ast::LetStmt, ast::ArgList>::cast).is_some();
12811291
let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
12821292

0 commit comments

Comments
 (0)