From 213dc41fe7e9e4ab56a58d7048deaa658015bc44 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 6 Jul 2020 16:11:12 +0200 Subject: [PATCH] Add checks for doc alias on which item it's used --- src/librustdoc/visit_ast.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index cf57ffd0b4b..e1a84e99836 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -31,6 +31,27 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec { std::iter::once(crate_name).chain(relative).collect() } +fn check_doc_alias_attrs( + attrs: &[ast::Attribute], + item_kind: &str, + diagnostic: &::rustc_errors::Handler, +) { + for attr in attrs { + if let Some(attr) = attr.meta() { + if let Some(list) = attr.meta_item_list() { + for meta in list { + if meta.check_name(sym::alias) { + diagnostic.span_err( + meta.span(), + &format!("`#[doc(alias = \"...\")]` isn't allowed on {}", item_kind), + ); + } + } + } + } + } +} + // Also, is there some reason that this doesn't use the 'visit' // framework from syntax?. @@ -387,6 +408,7 @@ fn visit_item( match item.kind { hir::ItemKind::ForeignMod(ref fm) => { + check_doc_alias_attrs(&item.attrs, "extern block", self.cx.sess().diagnostic()); for item in fm.items { self.visit_foreign_item(item, None, om); } @@ -561,6 +583,11 @@ fn visit_item( self_ty, ref items, } => { + check_doc_alias_attrs( + &item.attrs, + "implementation block", + self.cx.sess().diagnostic(), + ); // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick // them up regardless of where they're located. if !self.inlining && of_trait.is_none() {