File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change @@ -49,22 +49,24 @@ impl From<ast::IfExpr> for ElseBranch {
49
49
50
50
impl ast:: IfExpr {
51
51
pub fn then_branch ( & self ) -> Option < ast:: BlockExpr > {
52
- self . children_after_condition ( ) . next ( )
52
+ let mut exprs = support:: children ( self . syntax ( ) ) ;
53
+ let first = exprs. next ( ) ;
54
+ let second = exprs. next ( ) ;
55
+ second. or ( first)
53
56
}
54
57
55
58
pub fn else_branch ( & self ) -> Option < ElseBranch > {
56
- let res = match self . children_after_condition ( ) . nth ( 1 ) {
57
- Some ( block) => ElseBranch :: Block ( block) ,
58
- None => {
59
- let elif = self . children_after_condition ( ) . next ( ) ?;
60
- ElseBranch :: IfExpr ( elif)
61
- }
62
- } ;
63
- Some ( res)
59
+ let mut exprs = support:: children ( self . syntax ( ) ) . nth ( 2 ) ?;
60
+
61
+ match support:: children ( self . syntax ( ) ) . nth ( 2 ) ? {
62
+ ast:: Expr :: BlockExpr ( block) => Some ( ElseBranch :: Block ( block) ) ,
63
+ ast:: Expr :: IfExpr ( elif) => Some ( ElseBranch :: IfExpr ( elif) ) ,
64
+ _ => None ,
65
+ }
64
66
}
65
67
66
68
fn children_after_condition < N : AstNode > ( & self ) -> impl Iterator < Item = N > {
67
- self . syntax ( ) . children ( ) . skip ( 1 ) . filter_map ( N :: cast)
69
+ self . syntax ( ) . children ( ) . filter_map ( N :: cast) . skip ( 1 )
68
70
}
69
71
}
70
72
Original file line number Diff line number Diff line change @@ -939,7 +939,14 @@ impl From<ast::Adt> for ast::Item {
939
939
940
940
impl ast:: IfExpr {
941
941
pub fn condition ( & self ) -> Option < ast:: Expr > {
942
- support:: child ( & self . syntax )
942
+ // If the condition is a BlockExpr, check if the then body is missing.
943
+ // If it is assume the condition is the expression that is missing instead.
944
+ let mut exprs = support:: children ( self . syntax ( ) ) ;
945
+ let first = exprs. next ( ) ;
946
+ match first {
947
+ Some ( ast:: Expr :: BlockExpr ( _) ) => exprs. next ( ) . and ( first) ,
948
+ first => first,
949
+ }
943
950
}
944
951
}
945
952
You can’t perform that action at this time.
0 commit comments