Skip to content

Commit 0b59d48

Browse files
Merge pull request #21198 from A4-Tacks/nested-incomplete-let-semi
Fix nested expr missing semicolon in incomplete-let
2 parents 4503a7c + 4819100 commit 0b59d48

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,60 @@ fn main() {
342342
};
343343
bar();
344344
}
345+
"#,
346+
);
347+
348+
check_edit(
349+
"loop",
350+
r#"
351+
fn main() {
352+
let x = &$0
353+
bar();
354+
}
355+
"#,
356+
r#"
357+
fn main() {
358+
let x = &loop {
359+
$0
360+
};
361+
bar();
362+
}
363+
"#,
364+
);
365+
366+
check_edit(
367+
"loop",
368+
r#"
369+
fn main() {
370+
let x = -$0
371+
bar();
372+
}
373+
"#,
374+
r#"
375+
fn main() {
376+
let x = -loop {
377+
$0
378+
};
379+
bar();
380+
}
381+
"#,
382+
);
383+
384+
check_edit(
385+
"loop",
386+
r#"
387+
fn main() {
388+
let x = 2 + $0
389+
bar();
390+
}
391+
"#,
392+
r#"
393+
fn main() {
394+
let x = 2 + loop {
395+
$0
396+
};
397+
bar();
398+
}
345399
"#,
346400
);
347401
}

src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,8 @@ fn classify_name_ref<'db>(
13211321
let incomplete_expr_stmt =
13221322
it.parent().and_then(ast::ExprStmt::cast).map(|it| it.semicolon_token().is_none());
13231323
let before_else_kw = before_else_kw(it);
1324-
let incomplete_let = it
1325-
.parent()
1326-
.and_then(ast::LetStmt::cast)
1324+
let incomplete_let = left_ancestors(it.parent())
1325+
.find_map(ast::LetStmt::cast)
13271326
.is_some_and(|it| it.semicolon_token().is_none())
13281327
|| after_incomplete_let && incomplete_expr_stmt.unwrap_or(true) && !before_else_kw;
13291328
let in_value = is_in_value(it);
@@ -1882,6 +1881,13 @@ fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
18821881
Some((use_tree.path()?, true))
18831882
}
18841883

1884+
fn left_ancestors(node: Option<SyntaxNode>) -> impl Iterator<Item = SyntaxNode> {
1885+
node.into_iter().flat_map(|node| {
1886+
let end = node.text_range().end();
1887+
node.ancestors().take_while(move |it| it.text_range().end() == end)
1888+
})
1889+
}
1890+
18851891
fn is_in_token_of_for_loop(path: &ast::Path) -> bool {
18861892
// oh my ...
18871893
(|| {

0 commit comments

Comments
 (0)