@@ -1385,31 +1385,30 @@ enum FlowHandler {
1385
1385
1386
1386
impl FlowHandler {
1387
1387
fn from_ret_ty ( fun : & Function , ret_ty : & FunType ) -> FlowHandler {
1388
- match & fun. control_flow . kind {
1389
- None => FlowHandler :: None ,
1390
- Some ( flow_kind) => {
1391
- let action = flow_kind. clone ( ) ;
1392
- if let FunType :: Unit = ret_ty {
1393
- match flow_kind {
1394
- FlowKind :: Return ( None )
1395
- | FlowKind :: Break ( _, None )
1396
- | FlowKind :: Continue ( _) => FlowHandler :: If { action } ,
1397
- FlowKind :: Return ( _) | FlowKind :: Break ( _, _) => {
1398
- FlowHandler :: IfOption { action }
1399
- }
1400
- FlowKind :: Try { kind } => FlowHandler :: Try { kind : kind. clone ( ) } ,
1401
- }
1402
- } else {
1403
- match flow_kind {
1404
- FlowKind :: Return ( None )
1405
- | FlowKind :: Break ( _, None )
1406
- | FlowKind :: Continue ( _) => FlowHandler :: MatchOption { none : action } ,
1407
- FlowKind :: Return ( _) | FlowKind :: Break ( _, _) => {
1408
- FlowHandler :: MatchResult { err : action }
1409
- }
1410
- FlowKind :: Try { kind } => FlowHandler :: Try { kind : kind. clone ( ) } ,
1411
- }
1388
+ if fun. contains_tail_expr {
1389
+ return FlowHandler :: None ;
1390
+ }
1391
+ let Some ( action) = fun. control_flow . kind . clone ( ) else {
1392
+ return FlowHandler :: None ;
1393
+ } ;
1394
+
1395
+ if let FunType :: Unit = ret_ty {
1396
+ match action {
1397
+ FlowKind :: Return ( None ) | FlowKind :: Break ( _, None ) | FlowKind :: Continue ( _) => {
1398
+ FlowHandler :: If { action }
1399
+ }
1400
+ FlowKind :: Return ( _) | FlowKind :: Break ( _, _) => FlowHandler :: IfOption { action } ,
1401
+ FlowKind :: Try { kind } => FlowHandler :: Try { kind } ,
1402
+ }
1403
+ } else {
1404
+ match action {
1405
+ FlowKind :: Return ( None ) | FlowKind :: Break ( _, None ) | FlowKind :: Continue ( _) => {
1406
+ FlowHandler :: MatchOption { none : action }
1407
+ }
1408
+ FlowKind :: Return ( _) | FlowKind :: Break ( _, _) => {
1409
+ FlowHandler :: MatchResult { err : action }
1412
1410
}
1411
+ FlowKind :: Try { kind } => FlowHandler :: Try { kind } ,
1413
1412
}
1414
1413
}
1415
1414
}
@@ -1654,11 +1653,7 @@ impl Function {
1654
1653
1655
1654
fn make_ret_ty ( & self , ctx : & AssistContext < ' _ > , module : hir:: Module ) -> Option < ast:: RetType > {
1656
1655
let fun_ty = self . return_type ( ctx) ;
1657
- let handler = if self . contains_tail_expr {
1658
- FlowHandler :: None
1659
- } else {
1660
- FlowHandler :: from_ret_ty ( self , & fun_ty)
1661
- } ;
1656
+ let handler = FlowHandler :: from_ret_ty ( self , & fun_ty) ;
1662
1657
let ret_ty = match & handler {
1663
1658
FlowHandler :: None => {
1664
1659
if matches ! ( fun_ty, FunType :: Unit ) {
@@ -1728,11 +1723,7 @@ fn make_body(
1728
1723
fun : & Function ,
1729
1724
) -> ast:: BlockExpr {
1730
1725
let ret_ty = fun. return_type ( ctx) ;
1731
- let handler = if fun. contains_tail_expr {
1732
- FlowHandler :: None
1733
- } else {
1734
- FlowHandler :: from_ret_ty ( fun, & ret_ty)
1735
- } ;
1726
+ let handler = FlowHandler :: from_ret_ty ( fun, & ret_ty) ;
1736
1727
1737
1728
let block = match & fun. body {
1738
1729
FunctionBody :: Expr ( expr) => {
@@ -4471,7 +4462,7 @@ async fn foo() -> Result<(), ()> {
4471
4462
"# ,
4472
4463
r#"
4473
4464
async fn foo() -> Result<(), ()> {
4474
- fun_name().await?
4465
+ fun_name().await
4475
4466
}
4476
4467
4477
4468
async fn $0fun_name() -> Result<(), ()> {
@@ -4690,15 +4681,15 @@ fn $0fun_name() {
4690
4681
check_assist (
4691
4682
extract_function,
4692
4683
r#"
4693
- //- minicore: result
4684
+ //- minicore: result, try
4694
4685
fn foo() -> Result<(), i64> {
4695
4686
$0Result::<i32, i64>::Ok(0)?;
4696
4687
Ok(())$0
4697
4688
}
4698
4689
"# ,
4699
4690
r#"
4700
4691
fn foo() -> Result<(), i64> {
4701
- fun_name()?
4692
+ fun_name()
4702
4693
}
4703
4694
4704
4695
fn $0fun_name() -> Result<(), i64> {
@@ -5753,6 +5744,34 @@ fn $0fun_name<T, V>(t: T, v: V) -> i32 where T: Into<i32> + Copy, V: Into<i32> {
5753
5744
) ;
5754
5745
}
5755
5746
5747
+ #[ test]
5748
+ fn tail_expr_no_extra_control_flow ( ) {
5749
+ check_assist (
5750
+ extract_function,
5751
+ r#"
5752
+ //- minicore: result
5753
+ fn fallible() -> Result<(), ()> {
5754
+ $0if true {
5755
+ return Err(());
5756
+ }
5757
+ Ok(())$0
5758
+ }
5759
+ "# ,
5760
+ r#"
5761
+ fn fallible() -> Result<(), ()> {
5762
+ fun_name()
5763
+ }
5764
+
5765
+ fn $0fun_name() -> Result<(), ()> {
5766
+ if true {
5767
+ return Err(());
5768
+ }
5769
+ Ok(())
5770
+ }
5771
+ "# ,
5772
+ ) ;
5773
+ }
5774
+
5756
5775
#[ test]
5757
5776
fn non_tail_expr_of_tail_expr_loop ( ) {
5758
5777
check_assist (
@@ -5800,12 +5819,6 @@ fn $0fun_name() -> ControlFlow<()> {
5800
5819
extract_function,
5801
5820
r#"
5802
5821
//- minicore: option, try
5803
- impl<T> core::ops::Try for Option<T> {
5804
- type Output = T;
5805
- type Residual = Option<!>;
5806
- }
5807
- impl<T> core::ops::FromResidual for Option<T> {}
5808
-
5809
5822
fn f() -> Option<()> {
5810
5823
if true {
5811
5824
let a = $0if true {
@@ -5820,12 +5833,6 @@ fn f() -> Option<()> {
5820
5833
}
5821
5834
"# ,
5822
5835
r#"
5823
- impl<T> core::ops::Try for Option<T> {
5824
- type Output = T;
5825
- type Residual = Option<!>;
5826
- }
5827
- impl<T> core::ops::FromResidual for Option<T> {}
5828
-
5829
5836
fn f() -> Option<()> {
5830
5837
if true {
5831
5838
let a = fun_name()?;;
@@ -5852,12 +5859,6 @@ fn $0fun_name() -> Option<()> {
5852
5859
extract_function,
5853
5860
r#"
5854
5861
//- minicore: option, try
5855
- impl<T> core::ops::Try for Option<T> {
5856
- type Output = T;
5857
- type Residual = Option<!>;
5858
- }
5859
- impl<T> core::ops::FromResidual for Option<T> {}
5860
-
5861
5862
fn f() -> Option<()> {
5862
5863
if true {
5863
5864
$0{
@@ -5874,15 +5875,9 @@ fn f() -> Option<()> {
5874
5875
}
5875
5876
"# ,
5876
5877
r#"
5877
- impl<T> core::ops::Try for Option<T> {
5878
- type Output = T;
5879
- type Residual = Option<!>;
5880
- }
5881
- impl<T> core::ops::FromResidual for Option<T> {}
5882
-
5883
5878
fn f() -> Option<()> {
5884
5879
if true {
5885
- fun_name()?
5880
+ fun_name()
5886
5881
} else {
5887
5882
None
5888
5883
}
0 commit comments