]> git.lizzy.rs Git - rust.git/commitdiff
Add check for doc alias attribute format
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 30 May 2020 13:36:42 +0000 (15:36 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 30 May 2020 13:36:42 +0000 (15:36 +0200)
src/librustdoc/clean/types.rs

index 381238165274de42d61683ab44d30eebd2a8cf83..4f99476ebfa556d557ab1d77d38ab3f3e4ef537d 100644 (file)
@@ -481,6 +481,33 @@ pub fn extract_include(mi: &ast::MetaItem) -> Option<(String, String)> {
         })
     }
 
+    /// Enforce the format of attributes inside `#[doc(...)]`.
+    pub fn check_doc_attributes(
+        diagnostic: &::rustc_errors::Handler,
+        mi: &ast::MetaItem,
+    ) -> Option<(String, String)> {
+        mi.meta_item_list().and_then(|list| {
+            for meta in list {
+                if meta.check_name(sym::alias) {
+                    if !meta.is_value_str()
+                        || meta
+                            .value_str()
+                            .map(|s| s.to_string())
+                            .unwrap_or_else(String::new)
+                            .is_empty()
+                    {
+                        diagnostic.span_err(
+                            meta.span(),
+                            "doc alias attribute expects a string: #[doc(alias = \"0\")]",
+                        );
+                    }
+                }
+            }
+
+            None
+        })
+    }
+
     pub fn has_doc_flag(&self, flag: Symbol) -> bool {
         for attr in &self.other_attrs {
             if !attr.check_name(sym::doc) {
@@ -524,6 +551,7 @@ pub fn from_ast(diagnostic: &::rustc_errors::Handler, attrs: &[ast::Attribute])
                 } else {
                     if attr.check_name(sym::doc) {
                         if let Some(mi) = attr.meta() {
+                            Attributes::check_doc_attributes(&diagnostic, &mi);
                             if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
                                 // Extracted #[doc(cfg(...))]
                                 match Cfg::parse(cfg_mi) {