Skip to content

Commit

Permalink
Add extra check for #[doc(test(...)] attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 4, 2021
1 parent 6f32e3e commit a66bf52
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions compiler/rustc_lint_defs/src/builtin.rs
Expand Up @@ -3061,14 +3061,17 @@ declare_lint! {
}

declare_lint! {
/// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is
/// The `invalid_doc_attribute` lint detects when the `#[doc(...)]` is
/// misused.
///
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(warnings)]
/// #[doc(test(no_crate_inject))]
///
/// pub mod submodule {
/// #![doc(test(no_crate_inject))]
/// }
/// ```
///
/// {{produces}}
Expand All @@ -3083,6 +3086,6 @@ declare_lint! {
"detects invalid `#[doc(...)]` attributes",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #82730 <https://github.com/rust-lang/rust/issues/82730>",
edition: Some(Edition::Edition2021),
edition: None,
};
}
19 changes: 18 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Expand Up @@ -17,7 +17,9 @@ use rustc_hir::{
self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
};
use rustc_hir::{MethodKind, Target};
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
use rustc_session::lint::builtin::{
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTE, UNUSED_ATTRIBUTES,
};
use rustc_session::parse::feature_err;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -544,6 +546,21 @@ impl CheckAttrVisitor<'tcx> {
{
return false;
}
} else if meta.has_name(sym::test) {
if CRATE_HIR_ID != hir_id {
self.tcx.struct_span_lint_hir(
INVALID_DOC_ATTRIBUTE,
hir_id,
meta.span(),
|lint| {
lint.build(
"`#![doc(test(...)]` is only allowed as a crate level attribute"
)
.emit();
},
);
return false;
}
} else if let Some(i_meta) = meta.meta_item() {
if ![
sym::cfg,
Expand Down

0 comments on commit a66bf52

Please sign in to comment.