Skip to content

Commit 75a15bf

Browse files
authored
Rollup merge of #83098 - camelid:more-doc-attr-check, r=davidtwco
Find more invalid doc attributes - Lint on `#[doc(123)]`, `#[doc("hello")]`, etc. - Lint every attribute; e.g., will now report two warnings for `#[doc(foo, bar)]` - Add hyphen to "crate level" - Display paths like `#[doc(foo::bar)]` correctly instead of as an empty string
2 parents b8622f2 + 8f40e11 commit 75a15bf

12 files changed

+195
-55
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4247,6 +4247,7 @@ name = "rustc_passes"
42474247
version = "0.0.0"
42484248
dependencies = [
42494249
"rustc_ast",
4250+
"rustc_ast_pretty",
42504251
"rustc_attr",
42514252
"rustc_data_structures",
42524253
"rustc_errors",

compiler/rustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ rustc_serialize = { path = "../rustc_serialize" }
1919
rustc_span = { path = "../rustc_span" }
2020
rustc_trait_selection = { path = "../rustc_trait_selection" }
2121
rustc_lexer = { path = "../rustc_lexer" }
22+
rustc_ast_pretty = { path = "../rustc_ast_pretty" }

compiler/rustc_passes/src/check_attr.rs

+64-46
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl CheckAttrVisitor<'tcx> {
524524
.struct_span_err(
525525
meta.span(),
526526
&format!(
527-
"`#![doc({} = \"...\")]` isn't allowed as a crate level attribute",
527+
"`#![doc({} = \"...\")]` isn't allowed as a crate-level attribute",
528528
attr_name,
529529
),
530530
)
@@ -535,79 +535,97 @@ impl CheckAttrVisitor<'tcx> {
535535
}
536536

537537
fn check_doc_attrs(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
538-
if let Some(mi) = attr.meta() {
539-
if let Some(list) = mi.meta_item_list() {
540-
for meta in list {
541-
if meta.has_name(sym::alias) {
542-
if !self.check_attr_crate_level(meta, hir_id, "alias")
543-
|| !self.check_doc_alias(meta, hir_id, target)
538+
let mut is_valid = true;
539+
540+
if let Some(list) = attr.meta().and_then(|mi| mi.meta_item_list().map(|l| l.to_vec())) {
541+
for meta in list {
542+
if let Some(i_meta) = meta.meta_item() {
543+
match i_meta.name_or_empty() {
544+
sym::alias
545+
if !self.check_attr_crate_level(&meta, hir_id, "alias")
546+
|| !self.check_doc_alias(&meta, hir_id, target) =>
544547
{
545-
return false;
548+
is_valid = false
546549
}
547-
} else if meta.has_name(sym::keyword) {
548-
if !self.check_attr_crate_level(meta, hir_id, "keyword")
549-
|| !self.check_doc_keyword(meta, hir_id)
550+
551+
sym::keyword
552+
if !self.check_attr_crate_level(&meta, hir_id, "keyword")
553+
|| !self.check_doc_keyword(&meta, hir_id) =>
550554
{
551-
return false;
555+
is_valid = false
552556
}
553-
} else if meta.has_name(sym::test) {
554-
if CRATE_HIR_ID != hir_id {
557+
558+
sym::test if CRATE_HIR_ID != hir_id => {
555559
self.tcx.struct_span_lint_hir(
556560
INVALID_DOC_ATTRIBUTES,
557561
hir_id,
558562
meta.span(),
559563
|lint| {
560564
lint.build(
561-
"`#![doc(test(...)]` is only allowed as a crate level attribute"
565+
"`#![doc(test(...)]` is only allowed \
566+
as a crate-level attribute",
562567
)
563568
.emit();
564569
},
565570
);
566-
return false;
571+
is_valid = false;
567572
}
568-
} else if let Some(i_meta) = meta.meta_item() {
569-
if ![
570-
sym::cfg,
571-
sym::hidden,
572-
sym::html_favicon_url,
573-
sym::html_logo_url,
574-
sym::html_no_source,
575-
sym::html_playground_url,
576-
sym::html_root_url,
577-
sym::include,
578-
sym::inline,
579-
sym::issue_tracker_base_url,
580-
sym::masked,
581-
sym::no_default_passes, // deprecated
582-
sym::no_inline,
583-
sym::passes, // deprecated
584-
sym::plugins, // removed, but rustdoc warns about it itself
585-
sym::primitive,
586-
sym::spotlight,
587-
sym::test,
588-
]
589-
.iter()
590-
.any(|m| i_meta.has_name(*m))
591-
{
573+
574+
// no_default_passes: deprecated
575+
// passes: deprecated
576+
// plugins: removed, but rustdoc warns about it itself
577+
sym::alias
578+
| sym::cfg
579+
| sym::hidden
580+
| sym::html_favicon_url
581+
| sym::html_logo_url
582+
| sym::html_no_source
583+
| sym::html_playground_url
584+
| sym::html_root_url
585+
| sym::include
586+
| sym::inline
587+
| sym::issue_tracker_base_url
588+
| sym::keyword
589+
| sym::masked
590+
| sym::no_default_passes
591+
| sym::no_inline
592+
| sym::passes
593+
| sym::plugins
594+
| sym::primitive
595+
| sym::spotlight
596+
| sym::test => {}
597+
598+
_ => {
592599
self.tcx.struct_span_lint_hir(
593600
INVALID_DOC_ATTRIBUTES,
594601
hir_id,
595602
i_meta.span,
596603
|lint| {
597-
lint.build(&format!(
604+
let msg = format!(
598605
"unknown `doc` attribute `{}`",
599-
i_meta.name_or_empty()
600-
))
601-
.emit();
606+
rustc_ast_pretty::pprust::path_to_string(&i_meta.path),
607+
);
608+
lint.build(&msg).emit();
602609
},
603610
);
604-
return false;
611+
is_valid = false;
605612
}
606613
}
614+
} else {
615+
self.tcx.struct_span_lint_hir(
616+
INVALID_DOC_ATTRIBUTES,
617+
hir_id,
618+
meta.span(),
619+
|lint| {
620+
lint.build(&format!("invalid `doc` attribute")).emit();
621+
},
622+
);
623+
is_valid = false;
607624
}
608625
}
609626
}
610-
true
627+
628+
is_valid
611629
}
612630

613631
/// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid.

src/test/rustdoc-ui/doc-alias-crate-level.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: '\'' character isn't allowed in `#[doc(alias = "...")]`
44
LL | #[doc(alias = "shouldn't work!")]
55
| ^^^^^^^^^^^^^^^^^
66

7-
error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute
7+
error: `#![doc(alias = "...")]` isn't allowed as a crate-level attribute
88
--> $DIR/doc-alias-crate-level.rs:1:8
99
|
1010
LL | #![doc(alias = "crate-level-not-working")]

src/test/rustdoc-ui/doc-attr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@
88
//~^ ERROR unknown `doc` attribute
99
//~^^ WARN
1010
pub fn foo() {}
11+
12+
#[doc(123)]
13+
//~^ ERROR invalid `doc` attribute
14+
//~| WARN
15+
#[doc("hello", "bar")]
16+
//~^ ERROR invalid `doc` attribute
17+
//~| WARN
18+
//~| ERROR invalid `doc` attribute
19+
//~| WARN
20+
#[doc(foo::bar, crate::bar::baz = "bye")]
21+
//~^ ERROR unknown `doc` attribute
22+
//~| WARN
23+
//~| ERROR unknown `doc` attribute
24+
//~| WARN
25+
fn bar() {}

src/test/rustdoc-ui/doc-attr.stderr

+46-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ LL | #![deny(warnings)]
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
1515

16+
error: invalid `doc` attribute
17+
--> $DIR/doc-attr.rs:12:7
18+
|
19+
LL | #[doc(123)]
20+
| ^^^
21+
|
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
23+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
24+
25+
error: invalid `doc` attribute
26+
--> $DIR/doc-attr.rs:15:7
27+
|
28+
LL | #[doc("hello", "bar")]
29+
| ^^^^^^^
30+
|
31+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
32+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
33+
34+
error: invalid `doc` attribute
35+
--> $DIR/doc-attr.rs:15:16
36+
|
37+
LL | #[doc("hello", "bar")]
38+
| ^^^^^
39+
|
40+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
41+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
42+
43+
error: unknown `doc` attribute `foo::bar`
44+
--> $DIR/doc-attr.rs:20:7
45+
|
46+
LL | #[doc(foo::bar, crate::bar::baz = "bye")]
47+
| ^^^^^^^^
48+
|
49+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
50+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
51+
52+
error: unknown `doc` attribute `crate::bar::baz`
53+
--> $DIR/doc-attr.rs:20:17
54+
|
55+
LL | #[doc(foo::bar, crate::bar::baz = "bye")]
56+
| ^^^^^^^^^^^^^^^^^^^^^^^
57+
|
58+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
59+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
60+
1661
error: unknown `doc` attribute `as_ptr`
1762
--> $DIR/doc-attr.rs:3:8
1863
|
@@ -22,5 +67,5 @@ LL | #![doc(as_ptr)]
2267
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2368
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
2469

25-
error: aborting due to 2 previous errors
70+
error: aborting due to 7 previous errors
2671

src/test/rustdoc-ui/doc-attr2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `#![doc(test(...)]` is only allowed as a crate level attribute
1+
error: `#![doc(test(...)]` is only allowed as a crate-level attribute
22
--> $DIR/doc-attr2.rs:4:7
33
|
44
LL | #[doc(test(no_crate_inject))]
@@ -13,7 +13,7 @@ LL | #![deny(warnings)]
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
1515

16-
error: `#![doc(test(...)]` is only allowed as a crate level attribute
16+
error: `#![doc(test(...)]` is only allowed as a crate-level attribute
1717
--> $DIR/doc-attr2.rs:9:12
1818
|
1919
LL | #![doc(test(no_crate_inject))]

src/test/ui/attributes/doc-attr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@
88
//~^ ERROR unknown `doc` attribute
99
//~^^ WARN
1010
pub fn foo() {}
11+
12+
#[doc(123)]
13+
//~^ ERROR invalid `doc` attribute
14+
//~| WARN
15+
#[doc("hello", "bar")]
16+
//~^ ERROR invalid `doc` attribute
17+
//~| WARN
18+
//~| ERROR invalid `doc` attribute
19+
//~| WARN
20+
#[doc(foo::bar, crate::bar::baz = "bye")]
21+
//~^ ERROR unknown `doc` attribute
22+
//~| WARN
23+
//~| ERROR unknown `doc` attribute
24+
//~| WARN
25+
fn bar() {}

src/test/ui/attributes/doc-attr.stderr

+46-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ LL | #![deny(warnings)]
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
1515

16+
error: invalid `doc` attribute
17+
--> $DIR/doc-attr.rs:12:7
18+
|
19+
LL | #[doc(123)]
20+
| ^^^
21+
|
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
23+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
24+
25+
error: invalid `doc` attribute
26+
--> $DIR/doc-attr.rs:15:7
27+
|
28+
LL | #[doc("hello", "bar")]
29+
| ^^^^^^^
30+
|
31+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
32+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
33+
34+
error: invalid `doc` attribute
35+
--> $DIR/doc-attr.rs:15:16
36+
|
37+
LL | #[doc("hello", "bar")]
38+
| ^^^^^
39+
|
40+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
41+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
42+
43+
error: unknown `doc` attribute `foo::bar`
44+
--> $DIR/doc-attr.rs:20:7
45+
|
46+
LL | #[doc(foo::bar, crate::bar::baz = "bye")]
47+
| ^^^^^^^^
48+
|
49+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
50+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
51+
52+
error: unknown `doc` attribute `crate::bar::baz`
53+
--> $DIR/doc-attr.rs:20:17
54+
|
55+
LL | #[doc(foo::bar, crate::bar::baz = "bye")]
56+
| ^^^^^^^^^^^^^^^^^^^^^^^
57+
|
58+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
59+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
60+
1661
error: unknown `doc` attribute `as_ptr`
1762
--> $DIR/doc-attr.rs:3:8
1863
|
@@ -22,5 +67,5 @@ LL | #![doc(as_ptr)]
2267
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2368
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
2469

25-
error: aborting due to 2 previous errors
70+
error: aborting due to 7 previous errors
2671

src/test/ui/attributes/doc-attr2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `#![doc(test(...)]` is only allowed as a crate level attribute
1+
error: `#![doc(test(...)]` is only allowed as a crate-level attribute
22
--> $DIR/doc-attr2.rs:4:7
33
|
44
LL | #[doc(test(no_crate_inject))]
@@ -13,7 +13,7 @@ LL | #![deny(warnings)]
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
1515

16-
error: `#![doc(test(...)]` is only allowed as a crate level attribute
16+
error: `#![doc(test(...)]` is only allowed as a crate-level attribute
1717
--> $DIR/doc-attr2.rs:9:12
1818
|
1919
LL | #![doc(test(no_crate_inject))]

src/test/ui/rustdoc/doc-alias-crate-level.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: '\'' character isn't allowed in `#[doc(alias = "...")]`
44
LL | #[doc(alias = "shouldn't work!")]
55
| ^^^^^^^^^^^^^^^^^
66

7-
error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute
7+
error: `#![doc(alias = "...")]` isn't allowed as a crate-level attribute
88
--> $DIR/doc-alias-crate-level.rs:5:8
99
|
1010
LL | #![doc(alias = "not working!")]

src/test/ui/rustdoc/doc_keyword.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: `#[doc(keyword = "...")]` can only be used on modules
1010
LL | #[doc(keyword = "hall")]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: `#![doc(keyword = "...")]` isn't allowed as a crate level attribute
13+
error: `#![doc(keyword = "...")]` isn't allowed as a crate-level attribute
1414
--> $DIR/doc_keyword.rs:4:8
1515
|
1616
LL | #![doc(keyword = "hello")]

0 commit comments

Comments
 (0)