Auto merge of #80686 - GuillaumeGomez:error-doc-alias-same-name, r=jyn514

Error when #[doc(alias)] has same name as the item

Something I came across when reviewing some doc alias PRs.

r? `@jyn514`
This commit is contained in:
bors 2021-01-05 08:47:46 +00:00
commit f4b9d32ef5
5 changed files with 36 additions and 1 deletions

View File

@ -310,7 +310,7 @@ fn check_doc_alias(&self, meta: &NestedMetaItem, hir_id: HirId, target: Target)
.sess .sess
.struct_span_err( .struct_span_err(
meta.name_value_literal_span().unwrap_or_else(|| meta.span()), meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
&format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c,), &format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c),
) )
.emit(); .emit();
return false; return false;
@ -358,6 +358,17 @@ fn check_doc_alias(&self, meta: &NestedMetaItem, hir_id: HirId, target: Target)
.emit(); .emit();
return false; return false;
} }
let item_name = self.tcx.hir().name(hir_id);
if item_name.to_string() == doc_alias {
self.tcx
.sess
.struct_span_err(
meta.span(),
&format!("`#[doc(alias = \"...\")]` is the same as the item's name"),
)
.emit();
return false;
}
true true
} }

View File

@ -0,0 +1,4 @@
#![crate_type = "lib"]
#[doc(alias = "Foo")] //~ ERROR
pub struct Foo;

View File

@ -0,0 +1,8 @@
error: `#[doc(alias = "...")]` is the same as the item's name
--> $DIR/doc-alias-same-name.rs:3:7
|
LL | #[doc(alias = "Foo")]
| ^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -0,0 +1,4 @@
#![crate_type = "lib"]
#[doc(alias = "Foo")] //~ ERROR
pub struct Foo;

View File

@ -0,0 +1,8 @@
error: `#[doc(alias = "...")]` is the same as the item's name
--> $DIR/doc-alias-same-name.rs:3:7
|
LL | #[doc(alias = "Foo")]
| ^^^^^^^^^^^^^
error: aborting due to previous error