Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ pub(crate) struct RustcDocPrimitiveParser;

impl SingleAttributeParser for RustcDocPrimitiveParser {
const PATH: &[Symbol] = &[sym::rustc_doc_primitive];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Mod)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Const)]);
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "primitive name");
const STABILITY: AttributeStability = unstable!(
rustc_attrs,
Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use rustc_session::lint::builtin::{
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, MISPLACED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
};
use rustc_span::edition::Edition;
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
use rustc_trait_selection::traits::ObligationCtxt;
Expand Down Expand Up @@ -1024,18 +1024,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
hir::Node::Item(item) => Some(&item.kind),
_ => None,
};
match item_kind {
Some(ItemKind::Mod(_, module)) => {
if !module.item_ids.is_empty() {
self.dcx()
.emit_err(diagnostics::DocKeywordAttributeEmptyMod { span, attr_name });
return;
}
}
_ => {
self.dcx().emit_err(diagnostics::DocKeywordAttributeNotMod { span, attr_name });
return;
}
if let Some(ItemKind::Const(ident, _gen, _ty, _rhs)) = item_kind
&& ident.name == kw::Underscore
{
} else {
Comment on lines +1027 to +1030

@GuillaumeGomez GuillaumeGomez Aug 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the power of matches! =D

Suggested change
if let Some(ItemKind::Const(ident, _gen, _ty, _rhs)) = item_kind
&& ident.name == kw::Underscore
{
} else {
if !matches!(item_kind, ItemKind::(ident, ..) if ident.name == kw::Underscore) {

View changes since the review

self.dcx().emit_err(diagnostics::DocKeywordAttributeNotAnonConst { span, attr_name });
}
}

Expand Down
12 changes: 2 additions & 10 deletions compiler/rustc_passes/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,8 @@ pub(crate) struct DocAliasNotAnAlias {
}

#[derive(Diagnostic)]
#[diag("`#[doc({$attr_name} = \"...\")]` should be used on empty modules")]
pub(crate) struct DocKeywordAttributeEmptyMod {
#[primary_span]
pub span: Span,
pub attr_name: &'static str,
}

#[derive(Diagnostic)]
#[diag("`#[doc({$attr_name} = \"...\")]` should be used on modules")]
pub(crate) struct DocKeywordAttributeNotMod {
#[diag("`#[doc({$attr_name} = \"...\")]` should be used on anonymous constants")]
pub(crate) struct DocKeywordAttributeNotAnonConst {
#[primary_span]
pub span: Span,
pub attr_name: &'static str,
Expand Down
28 changes: 14 additions & 14 deletions library/core/src/attribute_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
///
/// [`unused_must_use`]: ../rustc/lints/listing/warn-by-default.html#unused-must-use
/// [the `must_use` attribute]: ../reference/attributes/diagnostics.html#the-must_use-attribute
mod must_use_attribute {}
const _: () = ();

#[doc(attribute = "allow")]
Comment thread
camelid marked this conversation as resolved.
//
Expand Down Expand Up @@ -144,7 +144,7 @@ mod must_use_attribute {}
/// [`forbid`]: ./attribute.forbid.html
/// [`warn`]: ./attribute.warn.html
/// [`deny`]: ./attribute.deny.html
mod allow_attribute {}
const _: () = ();

#[doc(attribute = "cfg")]
//
Expand Down Expand Up @@ -192,7 +192,7 @@ mod allow_attribute {}
/// [`cfg_attr`]: ../reference/conditional-compilation.html#the-cfg_attr-attribute
/// [the `cfg` attribute]: ../reference/conditional-compilation.html#the-cfg-attribute
/// [`if`]: ./keyword.if.html
mod cfg_attribute {}
const _: () = ();

#[doc(attribute = "deny")]
//
Expand Down Expand Up @@ -240,7 +240,7 @@ mod cfg_attribute {}
/// [`allow`]: ./attribute.allow.html
/// [`warn`]: ./attribute.warn.html
/// [`deny`]: ./attribute.deny.html
mod deny_attribute {}
const _: () = ();

#[doc(attribute = "forbid")]
//
Expand Down Expand Up @@ -276,7 +276,7 @@ mod deny_attribute {}
/// [the `forbid` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
/// [`allow`]: ./attribute.allow.html
/// [`warn`]: ./attribute.warn.html
mod forbid_attribute {}
const _: () = ();

#[doc(attribute = "deprecated")]
//
Expand All @@ -302,7 +302,7 @@ mod forbid_attribute {}
/// For more information, see the Reference on [the `deprecated` attribute].
///
/// [the `deprecated` attribute]: ../reference/attributes/diagnostics.html#the-deprecated-attribute
mod deprecated_attribute {}
const _: () = ();

#[doc(attribute = "warn")]
//
Expand Down Expand Up @@ -348,7 +348,7 @@ mod deprecated_attribute {}
/// [`allow`]: ./attribute.allow.html
/// [`deny`]: ./attribute.deny.html
/// [`forbid`]: ./attribute.forbid.html
mod warn_attribute {}
const _: () = ();

#[doc(attribute = "no_std")]
//
Expand Down Expand Up @@ -404,7 +404,7 @@ mod warn_attribute {}
/// [`Option`]: option::Option
/// [`Result`]: result::Result
/// [the `no_std` attribute]: ../reference/names/preludes.html#the-no_std-attribute
mod no_std_attribute {}
const _: () = ();

#[doc(attribute = "inline")]
//
Expand Down Expand Up @@ -444,7 +444,7 @@ mod no_std_attribute {}
/// For more information, see the Reference on [the `inline` attribute].
///
/// [the `inline` attribute]: ../reference/attributes/codegen.html#the-inline-attribute
mod inline_attribute {}
const _: () = ();

#[doc(attribute = "cold")]
//
Expand Down Expand Up @@ -474,7 +474,7 @@ mod inline_attribute {}
/// For more information, see the Reference on [the `cold` attribute].
///
/// [the `cold` attribute]: ../reference/attributes/codegen.html#the-cold-attribute
mod cold_attribute {}
const _: () = ();

#[doc(attribute = "track_caller")]
//
Expand Down Expand Up @@ -505,7 +505,7 @@ mod cold_attribute {}
/// [`Location::caller`]: panic::Location::caller
/// [`Option::unwrap`]: Option::unwrap
/// [the `track_caller` attribute]: ../reference/attributes/codegen.html#the-track_caller-attribute
mod track_caller_attribute {}
const _: () = ();

#[doc(attribute = "proc_macro")]
//
Expand Down Expand Up @@ -554,7 +554,7 @@ mod track_caller_attribute {}
/// [`TokenStream`]: ../proc_macro/struct.TokenStream.html
/// [function-like procedural macros]: ../reference/procedural-macros.html#the-proc_macro-attribute
/// [`proc_macro`]: ../proc_macro/index.html
mod proc_macro_attribute {}
const _: () = ();

#[doc(attribute = "link_section")]
//
Expand All @@ -580,7 +580,7 @@ mod proc_macro_attribute {}
/// For more information, see the Reference on [the `link_section` attribute].
///
/// [the `link_section` attribute]: ../reference/abi.html#the-link_section-attribute
mod link_section_attribute {}
const _: () = ();

#[doc(attribute = "non_exhaustive")]
//
Expand Down Expand Up @@ -632,4 +632,4 @@ mod link_section_attribute {}
/// For more information, see the Reference on [the `non_exhaustive` attribute].
///
/// [the `non_exhaustive` attribute]: ../reference/attributes/type_system.html#the-non_exhaustive-attribute
mod non_exhaustive_attribute {}
const _: () = ();
Loading
Loading