]> git.lizzy.rs Git - rust.git/commitdiff
extract recover_pat_ident_mut_first
authorMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 05:45:55 +0000 (07:45 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 10:50:44 +0000 (12:50 +0200)
src/libsyntax/parse/parser/pat.rs

index b7e40969d3eb5ed2a32037c1ccbd829e62a4533e..1b6baf09d816b4c2bec33967bfff787a9840b031 100644 (file)
@@ -140,22 +140,7 @@ fn parse_pat_with_range_pat(
                 // Parse _
                 PatKind::Wild
             } else if self.eat_keyword(kw::Mut) {
-                // Parse mut ident @ pat / mut ref ident @ pat
-                let mutref_span = self.prev_span.to(self.token.span);
-                let binding_mode = if self.eat_keyword(kw::Ref) {
-                    self.diagnostic()
-                        .struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
-                        .span_suggestion(
-                            mutref_span,
-                            "try switching the order",
-                            "ref mut".into(),
-                            Applicability::MachineApplicable
-                        ).emit();
-                    BindingMode::ByRef(Mutability::Mutable)
-                } else {
-                    BindingMode::ByValue(Mutability::Mutable)
-                };
-                self.parse_pat_ident(binding_mode)?
+                self.recover_pat_ident_mut_first()?
             } else if self.eat_keyword(kw::Ref) {
                 // Parse ref ident @ pat / ref mut ident @ pat
                 let mutbl = self.parse_mutability();
@@ -338,6 +323,25 @@ fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
         })
     }
 
+    // Recover on `mut ref? ident @ pat` and suggest that the order of `mut` and `ref` is incorrect.
+    fn recover_pat_ident_mut_first(&mut self) -> PResult<'a, PatKind> {
+        let mutref_span = self.prev_span.to(self.token.span);
+        let binding_mode = if self.eat_keyword(kw::Ref) {
+            self.struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
+                .span_suggestion(
+                    mutref_span,
+                    "try switching the order",
+                    "ref mut".into(),
+                    Applicability::MachineApplicable
+                )
+                .emit();
+            BindingMode::ByRef(Mutability::Mutable)
+        } else {
+            BindingMode::ByValue(Mutability::Mutable)
+        };
+        self.parse_pat_ident(binding_mode)
+    }
+
     // Helper function to decide whether to parse as ident binding
     // or to try to do something more complex like range patterns.
     fn parse_as_ident(&mut self) -> bool {