Skip to content

Commit ff618e5

Browse files
authored
Rollup merge of rust-lang#72780 - GuillaumeGomez:enforce-doc-alias-check, r=ollie27
Enforce doc alias check Part of rust-lang#50146. r? @ollie27
2 parents 24c460f + 2d6267a commit ff618e5

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/librustdoc/clean/types.rs

+28
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,33 @@ impl Attributes {
486486
})
487487
}
488488

489+
/// Enforce the format of attributes inside `#[doc(...)]`.
490+
pub fn check_doc_attributes(
491+
diagnostic: &::rustc_errors::Handler,
492+
mi: &ast::MetaItem,
493+
) -> Option<(String, String)> {
494+
mi.meta_item_list().and_then(|list| {
495+
for meta in list {
496+
if meta.check_name(sym::alias) {
497+
if !meta.is_value_str()
498+
|| meta
499+
.value_str()
500+
.map(|s| s.to_string())
501+
.unwrap_or_else(String::new)
502+
.is_empty()
503+
{
504+
diagnostic.span_err(
505+
meta.span(),
506+
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
507+
);
508+
}
509+
}
510+
}
511+
512+
None
513+
})
514+
}
515+
489516
pub fn has_doc_flag(&self, flag: Symbol) -> bool {
490517
for attr in &self.other_attrs {
491518
if !attr.check_name(sym::doc) {
@@ -529,6 +556,7 @@ impl Attributes {
529556
} else {
530557
if attr.check_name(sym::doc) {
531558
if let Some(mi) = attr.meta() {
559+
Attributes::check_doc_attributes(&diagnostic, &mi);
532560
if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
533561
// Extracted #[doc(cfg(...))]
534562
match Cfg::parse(cfg_mi) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(doc_alias)]
2+
3+
#[doc(alias = "foo")] // ok!
4+
pub struct Bar;
5+
6+
#[doc(alias)] //~ ERROR
7+
#[doc(alias = 0)] //~ ERROR
8+
#[doc(alias("bar"))] //~ ERROR
9+
pub struct Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: doc alias attribute expects a string: #[doc(alias = "0")]
2+
--> $DIR/check-doc-alias-attr.rs:6:7
3+
|
4+
LL | #[doc(alias)]
5+
| ^^^^^
6+
7+
error: doc alias attribute expects a string: #[doc(alias = "0")]
8+
--> $DIR/check-doc-alias-attr.rs:7:7
9+
|
10+
LL | #[doc(alias = 0)]
11+
| ^^^^^^^^^
12+
13+
error: doc alias attribute expects a string: #[doc(alias = "0")]
14+
--> $DIR/check-doc-alias-attr.rs:8:7
15+
|
16+
LL | #[doc(alias("bar"))]
17+
| ^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)