Skip to content

Commit a66bf52

Browse files
Add extra check for #[doc(test(...)] attribute
1 parent 6f32e3e commit a66bf52

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3061,14 +3061,17 @@ declare_lint! {
30613061
}
30623062

30633063
declare_lint! {
3064-
/// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is
3064+
/// The `invalid_doc_attribute` lint detects when the `#[doc(...)]` is
30653065
/// misused.
30663066
///
30673067
/// ### Example
30683068
///
30693069
/// ```rust,compile_fail
30703070
/// #![deny(warnings)]
3071-
/// #[doc(test(no_crate_inject))]
3071+
///
3072+
/// pub mod submodule {
3073+
/// #![doc(test(no_crate_inject))]
3074+
/// }
30723075
/// ```
30733076
///
30743077
/// {{produces}}
@@ -3083,6 +3086,6 @@ declare_lint! {
30833086
"detects invalid `#[doc(...)]` attributes",
30843087
@future_incompatible = FutureIncompatibleInfo {
30853088
reference: "issue #82730 <https://github.com/rust-lang/rust/issues/82730>",
3086-
edition: Some(Edition::Edition2021),
3089+
edition: None,
30873090
};
30883091
}

compiler/rustc_passes/src/check_attr.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use rustc_hir::{
1717
self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
1818
};
1919
use rustc_hir::{MethodKind, Target};
20-
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
20+
use rustc_session::lint::builtin::{
21+
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTE, UNUSED_ATTRIBUTES,
22+
};
2123
use rustc_session::parse::feature_err;
2224
use rustc_span::symbol::{sym, Symbol};
2325
use rustc_span::{Span, DUMMY_SP};
@@ -544,6 +546,21 @@ impl CheckAttrVisitor<'tcx> {
544546
{
545547
return false;
546548
}
549+
} else if meta.has_name(sym::test) {
550+
if CRATE_HIR_ID != hir_id {
551+
self.tcx.struct_span_lint_hir(
552+
INVALID_DOC_ATTRIBUTE,
553+
hir_id,
554+
meta.span(),
555+
|lint| {
556+
lint.build(
557+
"`#![doc(test(...)]` is only allowed as a crate level attribute"
558+
)
559+
.emit();
560+
},
561+
);
562+
return false;
563+
}
547564
} else if let Some(i_meta) = meta.meta_item() {
548565
if ![
549566
sym::cfg,

0 commit comments

Comments
 (0)