Skip to content

Commit cbfe874

Browse files
Add check for doc(test(...)) attribute
1 parent 0035d9d commit cbfe874

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-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 {

0 commit comments

Comments
 (0)