]> git.lizzy.rs Git - rust.git/blobdiff - crates/syntax/src/ast/node_ext.rs
Wrap remaining self/super/crate in Name{Ref}
[rust.git] / crates / syntax / src / ast / node_ext.rs
index b8ce71d270fc42a4f30909d131b9e80f2b53c683..738c92a5b5b596f453eae5b847d3c8292f8b62cb 100644 (file)
@@ -198,6 +198,13 @@ impl ast::Path {
     pub fn parent_path(&self) -> Option<ast::Path> {
         self.syntax().parent().and_then(ast::Path::cast)
     }
+
+    pub fn as_single_segment(&self) -> Option<ast::PathSegment> {
+        match self.qualifier() {
+            Some(_) => None,
+            None => self.segment(),
+        }
+    }
 }
 
 impl ast::UseTreeList {
@@ -448,16 +455,22 @@ pub enum VisibilityKind {
 
 impl ast::Visibility {
     pub fn kind(&self) -> VisibilityKind {
-        if let Some(path) = support::children(self.syntax()).next() {
-            VisibilityKind::In(path)
-        } else if self.crate_token().is_some() {
-            VisibilityKind::PubCrate
-        } else if self.super_token().is_some() {
-            VisibilityKind::PubSuper
-        } else if self.self_token().is_some() {
-            VisibilityKind::PubSelf
-        } else {
-            VisibilityKind::Pub
+        match self.path() {
+            Some(path) => {
+                if let Some(segment) =
+                    path.as_single_segment().filter(|it| it.coloncolon_token().is_none())
+                {
+                    if segment.crate_token().is_some() {
+                        return VisibilityKind::PubCrate;
+                    } else if segment.super_token().is_some() {
+                        return VisibilityKind::PubSuper;
+                    } else if segment.self_token().is_some() {
+                        return VisibilityKind::PubSelf;
+                    }
+                }
+                VisibilityKind::In(path)
+            }
+            None => VisibilityKind::Pub,
         }
     }
 }