@@ -1799,7 +1799,6 @@ fn make_body(
1799
1799
} )
1800
1800
. collect :: < Vec < SyntaxElement > > ( ) ;
1801
1801
let tail_expr = tail_expr. map ( |expr| expr. dedent ( old_indent) . indent ( body_indent) ) ;
1802
-
1803
1802
make:: hacky_block_expr_with_comments ( elements, tail_expr)
1804
1803
}
1805
1804
} ;
@@ -1860,9 +1859,29 @@ fn with_default_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::B
1860
1859
}
1861
1860
1862
1861
fn with_tail_expr ( block : ast:: BlockExpr , tail_expr : ast:: Expr ) -> ast:: BlockExpr {
1863
- let stmt_tail = block. tail_expr ( ) . map ( |expr| make:: expr_stmt ( expr) . into ( ) ) ;
1864
- let stmts = block. statements ( ) . chain ( stmt_tail) ;
1865
- make:: block_expr ( stmts, Some ( tail_expr) )
1862
+ let stmt_tail_opt: Option < ast:: Stmt > =
1863
+ block. tail_expr ( ) . map ( |expr| make:: expr_stmt ( expr) . into ( ) ) ;
1864
+
1865
+ let mut elements: Vec < SyntaxElement > = vec ! [ ] ;
1866
+
1867
+ block. statements ( ) . for_each ( |stmt| {
1868
+ elements. push ( syntax:: NodeOrToken :: Node ( stmt. syntax ( ) . clone ( ) ) ) ;
1869
+ } ) ;
1870
+
1871
+ if let Some ( stmt_list) = block. stmt_list ( ) {
1872
+ stmt_list. syntax ( ) . children_with_tokens ( ) . for_each ( |node_or_token| {
1873
+ match & node_or_token {
1874
+ syntax:: NodeOrToken :: Token ( _) => elements. push ( node_or_token) ,
1875
+ _ => ( ) ,
1876
+ } ;
1877
+ } ) ;
1878
+ }
1879
+
1880
+ if let Some ( stmt_tail) = stmt_tail_opt {
1881
+ elements. push ( syntax:: NodeOrToken :: Node ( stmt_tail. syntax ( ) . clone ( ) ) ) ;
1882
+ }
1883
+
1884
+ make:: hacky_block_expr_with_comments ( elements, Some ( tail_expr) )
1866
1885
}
1867
1886
1868
1887
fn format_type ( ty : & hir:: Type , ctx : & AssistContext < ' _ > , module : hir:: Module ) -> String {
@@ -5744,6 +5763,46 @@ fn $0fun_name() -> Option<()> {
5744
5763
};
5745
5764
Some(a)
5746
5765
}
5766
+ "# ,
5767
+ ) ;
5768
+ }
5769
+
5770
+ #[ test]
5771
+ fn non_tail_expr_with_comment_of_tail_expr_loop ( ) {
5772
+ check_assist (
5773
+ extract_function,
5774
+ r#"
5775
+ pub fn f() {
5776
+ loop {
5777
+ $0// A comment
5778
+ if true {
5779
+ continue;
5780
+ }$0
5781
+ if false {
5782
+ break;
5783
+ }
5784
+ }
5785
+ }
5786
+ "# ,
5787
+ r#"
5788
+ pub fn f() {
5789
+ loop {
5790
+ if let ControlFlow::Break(_) = fun_name() {
5791
+ continue;
5792
+ }
5793
+ if false {
5794
+ break;
5795
+ }
5796
+ }
5797
+ }
5798
+
5799
+ fn $0fun_name() -> ControlFlow<()> {
5800
+ // A comment
5801
+ if true {
5802
+ return ControlFlow::Break(());
5803
+ }
5804
+ ControlFlow::Continue(())
5805
+ }
5747
5806
"# ,
5748
5807
) ;
5749
5808
}
0 commit comments