@@ -35,39 +35,33 @@ impl<'a> Parser<'a> {
3535 let attrs = self . parse_outer_attributes ( ) ?;
3636 let lo = self . token . span ;
3737
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 ) {
4543 self . bump ( ) ; // `auto`
4644 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) {
5047 self . bump ( ) ; // `var`
5148 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 ( )
6053 {
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 ( ) ) ? {
6560 // 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) ) )
6762 }
68-
6963 // 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 {
7165 self . error_outer_attrs ( & attrs) ;
7266 self . bump ( ) ;
7367 let mut last_semi = lo;
@@ -82,17 +76,16 @@ impl<'a> Parser<'a> {
8276 ExprKind :: Tup ( Vec :: new ( ) ) ,
8377 AttrVec :: new ( ) ,
8478 ) ) ;
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 {
8985 self . error_outer_attrs ( & attrs) ;
9086 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) )
9689 }
9790
9891 fn parse_stmt_item ( & mut self , attrs : Vec < Attribute > ) -> PResult < ' a , Option < ast:: Item > > {
@@ -168,12 +161,12 @@ impl<'a> Parser<'a> {
168161 attrs : AttrVec ,
169162 msg : & str ,
170163 sugg : & str ,
171- ) -> PResult < ' a , Option < Stmt > > {
164+ ) -> PResult < ' a , Stmt > {
172165 let stmt = self . parse_local_mk ( lo, attrs) ?;
173166 self . struct_span_err ( lo, "invalid variable declaration" )
174167 . span_suggestion ( lo, msg, sugg. to_string ( ) , Applicability :: MachineApplicable )
175168 . emit ( ) ;
176- Ok ( Some ( stmt) )
169+ Ok ( stmt)
177170 }
178171
179172 fn parse_local_mk ( & mut self , lo : Span , attrs : AttrVec ) -> PResult < ' a , Stmt > {
0 commit comments