Skip to content

Commit cbe3afe

Browse files
committed
Auto merge of #87728 - GuillaumeGomez:doc-test-attr-checks, r=jyn514
Add "doc(test(...))" attribute checks Fixes #82672. r? `@camelid`
2 parents 02b27f1 + d4293ff commit cbe3afe

File tree

7 files changed

+156
-2
lines changed

7 files changed

+156
-2
lines changed

compiler/rustc_passes/src/check_attr.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,42 @@ impl CheckAttrVisitor<'tcx> {
717717
true
718718
}
719719

720+
/// Checks that `doc(test(...))` attribute contains only valid attributes. Returns `true` if
721+
/// valid.
722+
fn check_test_attr(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
723+
let mut is_valid = true;
724+
if let Some(metas) = meta.meta_item_list() {
725+
for i_meta in metas {
726+
match i_meta.name_or_empty() {
727+
sym::attr | sym::no_crate_inject => {}
728+
_ => {
729+
self.tcx.struct_span_lint_hir(
730+
INVALID_DOC_ATTRIBUTES,
731+
hir_id,
732+
i_meta.span(),
733+
|lint| {
734+
lint.build(&format!(
735+
"unknown `doc(test)` attribute `{}`",
736+
rustc_ast_pretty::pprust::path_to_string(
737+
&i_meta.meta_item().unwrap().path
738+
),
739+
))
740+
.emit();
741+
},
742+
);
743+
is_valid = false;
744+
}
745+
}
746+
}
747+
} else {
748+
self.tcx.struct_span_lint_hir(INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| {
749+
lint.build("`#[doc(test(...)]` takes a list of attributes").emit();
750+
});
751+
is_valid = false;
752+
}
753+
is_valid
754+
}
755+
720756
/// Runs various checks on `#[doc]` attributes. Returns `true` if valid.
721757
///
722758
/// `specified_inline` should be initialized to `None` and kept for the scope
@@ -793,8 +829,13 @@ impl CheckAttrVisitor<'tcx> {
793829
| sym::no_inline
794830
| sym::notable_trait
795831
| sym::passes
796-
| sym::plugins
797-
| sym::test => {}
832+
| sym::plugins => {}
833+
834+
sym::test => {
835+
if !self.check_test_attr(&meta, hir_id) {
836+
is_valid = false;
837+
}
838+
}
798839

799840
sym::primitive => {
800841
if !self.tcx.features().doc_primitive {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
3+
#![crate_type = "lib"]
4+
#![deny(invalid_doc_attributes)]
5+
#![doc(test(no_crate_inject))]
6+
#![doc(test(attr(deny(warnings))))]
7+
8+
pub fn foo() {}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_type = "lib"]
2+
#![deny(invalid_doc_attributes)]
3+
4+
#![doc(test)]
5+
//~^ ERROR `#[doc(test(...)]` takes a list of attributes
6+
//~^^ WARN this was previously accepted by the compiler
7+
#![doc(test = "hello")]
8+
//~^ ERROR `#[doc(test(...)]` takes a list of attributes
9+
//~^^ WARN this was previously accepted by the compiler
10+
#![doc(test(a))]
11+
//~^ ERROR unknown `doc(test)` attribute `a`
12+
//~^^ WARN this was previously accepted by the compiler
13+
14+
pub fn foo() {}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: `#[doc(test(...)]` takes a list of attributes
2+
--> $DIR/doc-test-attr.rs:4:8
3+
|
4+
LL | #![doc(test)]
5+
| ^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/doc-test-attr.rs:2:9
9+
|
10+
LL | #![deny(invalid_doc_attributes)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
14+
15+
error: `#[doc(test(...)]` takes a list of attributes
16+
--> $DIR/doc-test-attr.rs:7:8
17+
|
18+
LL | #![doc(test = "hello")]
19+
| ^^^^^^^^^^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
23+
24+
error: unknown `doc(test)` attribute `a`
25+
--> $DIR/doc-test-attr.rs:10:13
26+
|
27+
LL | #![doc(test(a))]
28+
| ^
29+
|
30+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
32+
33+
error: aborting due to 3 previous errors
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
3+
#![crate_type = "lib"]
4+
#![deny(invalid_doc_attributes)]
5+
#![doc(test(no_crate_inject))]
6+
#![doc(test(attr(deny(warnings))))]
7+
#![doc(test())]
8+
9+
pub fn foo() {}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_type = "lib"]
2+
#![deny(invalid_doc_attributes)]
3+
4+
#![doc(test)]
5+
//~^ ERROR `#[doc(test(...)]` takes a list of attributes
6+
//~^^ WARN this was previously accepted by the compiler
7+
#![doc(test = "hello")]
8+
//~^ ERROR `#[doc(test(...)]` takes a list of attributes
9+
//~^^ WARN this was previously accepted by the compiler
10+
#![doc(test(a))]
11+
//~^ ERROR unknown `doc(test)` attribute `a`
12+
//~^^ WARN this was previously accepted by the compiler
13+
14+
pub fn foo() {}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: `#[doc(test(...)]` takes a list of attributes
2+
--> $DIR/doc-test-attr.rs:4:8
3+
|
4+
LL | #![doc(test)]
5+
| ^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/doc-test-attr.rs:2:9
9+
|
10+
LL | #![deny(invalid_doc_attributes)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
14+
15+
error: `#[doc(test(...)]` takes a list of attributes
16+
--> $DIR/doc-test-attr.rs:7:8
17+
|
18+
LL | #![doc(test = "hello")]
19+
| ^^^^^^^^^^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
23+
24+
error: unknown `doc(test)` attribute `a`
25+
--> $DIR/doc-test-attr.rs:10:13
26+
|
27+
LL | #![doc(test(a))]
28+
| ^
29+
|
30+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
32+
33+
error: aborting due to 3 previous errors
34+

0 commit comments

Comments
 (0)