]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_parse/parser/item.rs
parse: extract `error_on_unmatched_vis`.
[rust.git] / src / librustc_parse / parser / item.rs
index 190baa9b2d90edcd575ad8e9dbf486fc305f56c8..732bbdf1c54770679d5d5fa9f32f67bc20063264 100644 (file)
@@ -88,16 +88,9 @@ fn parse_item_implementation(
             return Ok(Some(P(self.mk_item(lo, ident, kind, vis, Defaultness::Final, attrs))));
         }
 
-        // FAILURE TO PARSE ITEM
-        if let VisibilityKind::Inherited = vis.node {
-        } else {
-            let vs = pprust::vis_to_string(&vis);
-            let vs = vs.trim_end();
-            self.struct_span_err(vis.span, &format!("unmatched visibility `{}`", vs))
-                .span_label(vis.span, "the unmatched visibility")
-                .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
-                .emit();
-        }
+        // At this point, we have failed to parse an item.
+
+        self.error_on_unmatched_vis(&vis);
 
         if !attributes_allowed {
             self.recover_attrs_no_item(&attrs)?;
@@ -105,6 +98,19 @@ fn parse_item_implementation(
         Ok(None)
     }
 
+    /// Error in-case a non-inherited visibility was parsed but no item followed.
+    fn error_on_unmatched_vis(&self, vis: &Visibility) {
+        if let VisibilityKind::Inherited = vis.node {
+            return;
+        }
+        let vs = pprust::vis_to_string(&vis);
+        let vs = vs.trim_end();
+        self.struct_span_err(vis.span, &format!("unmatched visibility `{}`", vs))
+            .span_label(vis.span, "the unmatched visibility")
+            .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
+            .emit();
+    }
+
     /// Parses one of the items allowed by the flags.
     fn parse_item_kind(
         &mut self,