Skip to content

Commit c19056d

Browse files
authored
Merge pull request #22759 from Wilfred/fix/syntax-factory-mapping-unwraps
Fix crashes in assists due to .unwrap() calls in SyntaxFactory
2 parents 7ae7c10 + 5e6ac17 commit c19056d

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_closure_to_fn.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,25 @@ fn main() {
738738
);
739739
}
740740

741+
#[test]
742+
fn handles_closures_with_unannotated_rest_patterns() {
743+
check_assist(
744+
convert_closure_to_fn,
745+
r#"
746+
fn main() {
747+
let closure = |$0..| ();
748+
}
749+
"#,
750+
r#"
751+
fn main() {
752+
fn closure(..: _) {
753+
()
754+
}
755+
}
756+
"#,
757+
);
758+
}
759+
741760
#[test]
742761
fn multiple_capture_usages() {
743762
check_assist(

src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_branch.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,25 @@ fn main() {
831831
);
832832
}
833833

834+
#[test]
835+
fn regression_22759() {
836+
check_assist(
837+
unwrap_branch,
838+
r#"
839+
fn main() {
840+
match () {
841+
() $0=> let x = (),
842+
}
843+
}
844+
"#,
845+
r#"
846+
fn main() {
847+
let x = ()
848+
}
849+
"#,
850+
);
851+
}
852+
834853
#[test]
835854
fn simple_if_in_while_bad_cursor_position() {
836855
check_assist_not_applicable(

src/tools/rust-analyzer/crates/syntax/src/ast/make.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,17 @@ pub fn async_move_block_expr(
565565
}
566566

567567
pub fn tail_only_block_expr(tail_expr: ast::Expr) -> ast::BlockExpr {
568-
ast_from_text(&format!("fn f() {{ {tail_expr} }}"))
568+
quote! {
569+
BlockExpr {
570+
StmtList {
571+
['{']
572+
" "
573+
#tail_expr
574+
" "
575+
['}']
576+
}
577+
}
578+
}
569579
}
570580

571581
/// Ideally this function wouldn't exist since it involves manual indenting.
@@ -1043,7 +1053,14 @@ pub fn untyped_param(pat: ast::Pat) -> ast::Param {
10431053
}
10441054

10451055
pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
1046-
ast_from_text(&format!("fn f({pat}: {ty}) {{ }}"))
1056+
quote! {
1057+
Param {
1058+
#pat
1059+
[:]
1060+
" "
1061+
#ty
1062+
}
1063+
}
10471064
}
10481065

10491066
pub fn self_param() -> ast::SelfParam {

0 commit comments

Comments
 (0)