@@ -35,39 +35,33 @@ impl<'a> Parser<'a> {
35
35
let attrs = self . parse_outer_attributes ( ) ?;
36
36
let lo = self . token . span ;
37
37
38
- if self . eat_keyword ( kw:: Let ) {
39
- return self . parse_local_mk ( lo, attrs. into ( ) ) . map ( Some ) ;
40
- }
41
- if self . is_kw_followed_by_ident ( kw:: Mut ) {
42
- return self . recover_stmt_local ( lo, attrs. into ( ) , "missing keyword" , "let mut" ) ;
43
- }
44
- if self . is_kw_followed_by_ident ( kw:: Auto ) {
38
+ let stmt = if self . eat_keyword ( kw:: Let ) {
39
+ self . parse_local_mk ( lo, attrs. into ( ) ) ?
40
+ } else if self . is_kw_followed_by_ident ( kw:: Mut ) {
41
+ self . recover_stmt_local ( lo, attrs. into ( ) , "missing keyword" , "let mut" ) ?
42
+ } else if self . is_kw_followed_by_ident ( kw:: Auto ) {
45
43
self . bump ( ) ; // `auto`
46
44
let msg = "write `let` instead of `auto` to introduce a new variable" ;
47
- return self . recover_stmt_local ( lo, attrs. into ( ) , msg, "let" ) ;
48
- }
49
- if self . is_kw_followed_by_ident ( sym:: var) {
45
+ self . recover_stmt_local ( lo, attrs. into ( ) , msg, "let" ) ?
46
+ } else if self . is_kw_followed_by_ident ( sym:: var) {
50
47
self . bump ( ) ; // `var`
51
48
let msg = "write `let` instead of `var` to introduce a new variable" ;
52
- return self . recover_stmt_local ( lo, attrs. into ( ) , msg, "let" ) ;
53
- }
54
-
55
- // Starts like a simple path, being careful to avoid contextual keywords,
56
- // e.g., `union`, items with `crate` visibility, or `auto trait` items.
57
- // We aim to parse an arbitrary path `a::b` but not something that starts like a path
58
- // (1 token), but it fact not a path. Also, we avoid stealing syntax from `parse_item_`.
59
- if self . token . is_path_start ( ) && !self . token . is_qpath_start ( ) && !self . is_path_start_item ( )
49
+ self . recover_stmt_local ( lo, attrs. into ( ) , msg, "let" ) ?
50
+ } else if self . token . is_path_start ( )
51
+ && !self . token . is_qpath_start ( )
52
+ && !self . is_path_start_item ( )
60
53
{
61
- return self . parse_stmt_path_start ( lo, attrs) . map ( Some ) ;
62
- }
63
-
64
- if let Some ( item) = self . parse_stmt_item ( attrs. clone ( ) ) ? {
54
+ // We have avoided contextual keywords like `union`, items with `crate` visibility,
55
+ // or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
56
+ // that starts like a path (1 token), but it fact not a path.
57
+ // Also, we avoid stealing syntax from `parse_item_`.
58
+ self . parse_stmt_path_start ( lo, attrs) ?
59
+ } else if let Some ( item) = self . parse_stmt_item ( attrs. clone ( ) ) ? {
65
60
// FIXME: Bad copy of attrs
66
- return Ok ( Some ( self . mk_stmt ( lo. to ( item. span ) , StmtKind :: Item ( P ( item) ) ) ) ) ;
61
+ self . mk_stmt ( lo. to ( item. span ) , StmtKind :: Item ( P ( item) ) )
67
62
}
68
-
69
63
// Do not attempt to parse an expression if we're done here.
70
- if self . token == token:: Semi {
64
+ else if self . token == token:: Semi {
71
65
self . error_outer_attrs ( & attrs) ;
72
66
self . bump ( ) ;
73
67
let mut last_semi = lo;
@@ -82,17 +76,16 @@ impl<'a> Parser<'a> {
82
76
ExprKind :: Tup ( Vec :: new ( ) ) ,
83
77
AttrVec :: new ( ) ,
84
78
) ) ;
85
- return Ok ( Some ( self . mk_stmt ( lo. to ( last_semi) , kind) ) ) ;
86
- }
87
-
88
- if self . token == token:: CloseDelim ( token:: Brace ) {
79
+ self . mk_stmt ( lo. to ( last_semi) , kind)
80
+ } else if self . token != token:: CloseDelim ( token:: Brace ) {
81
+ // Remainder are line-expr stmts.
82
+ let e = self . parse_expr_res ( Restrictions :: STMT_EXPR , Some ( attrs. into ( ) ) ) ?;
83
+ self . mk_stmt ( lo. to ( e. span ) , StmtKind :: Expr ( e) )
84
+ } else {
89
85
self . error_outer_attrs ( & attrs) ;
90
86
return Ok ( None ) ;
91
- }
92
-
93
- // Remainder are line-expr stmts.
94
- let e = self . parse_expr_res ( Restrictions :: STMT_EXPR , Some ( attrs. into ( ) ) ) ?;
95
- Ok ( Some ( self . mk_stmt ( lo. to ( e. span ) , StmtKind :: Expr ( e) ) ) )
87
+ } ;
88
+ Ok ( Some ( stmt) )
96
89
}
97
90
98
91
fn parse_stmt_item ( & mut self , attrs : Vec < Attribute > ) -> PResult < ' a , Option < ast:: Item > > {
@@ -168,12 +161,12 @@ impl<'a> Parser<'a> {
168
161
attrs : AttrVec ,
169
162
msg : & str ,
170
163
sugg : & str ,
171
- ) -> PResult < ' a , Option < Stmt > > {
164
+ ) -> PResult < ' a , Stmt > {
172
165
let stmt = self . parse_local_mk ( lo, attrs) ?;
173
166
self . struct_span_err ( lo, "invalid variable declaration" )
174
167
. span_suggestion ( lo, msg, sugg. to_string ( ) , Applicability :: MachineApplicable )
175
168
. emit ( ) ;
176
- Ok ( Some ( stmt) )
169
+ Ok ( stmt)
177
170
}
178
171
179
172
fn parse_local_mk ( & mut self , lo : Span , attrs : AttrVec ) -> PResult < ' a , Stmt > {
0 commit comments