Skip to content
Merged
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
32 changes: 11 additions & 21 deletions compiler/rustc_attr_parsing/src/attributes/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,7 @@ impl DocParser {

fn accept_single_doc_attr(&mut self, cx: &mut AcceptContext<'_, '_>, args: &ArgParser) {
match args {
ArgParser::NoArgs => {
let suggestions = cx.adcx().suggestions();
let span = cx.inner_span;
cx.emit_lint(
INVALID_DOC_ATTRIBUTES,
IllFormedAttributeInput::new(&suggestions, None, None),
span,
);
}
ArgParser::List(items) => {
ArgParser::List(items) if !items.is_empty() => {

@JonathanBrouwer JonathanBrouwer Sep 1, 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.

Hmmm I'm not sure how I feel about a INVALID_DOC_ATTRIBUTES on #[doc()], could you motivate this change?

I think UNUSED_ATTRIBUTES might be nicer.
Other attributes, such as #[repr()], #![feature()] also do this.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

#[doc], #[doc(bogus)] etc emit invalid_doc_attributes. I think #[doc()] fits right in.

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.

I guess I just don't like that INVALID_DOC_ATTRIBUTES exists at all, but it does so indeed it does fit in

for i in items.mixed() {
match i {
MetaItemOrLitParser::MetaItemParser(mip) => {
Expand All @@ -739,6 +730,15 @@ impl DocParser {
);
}
}
_ => {
let suggestions = cx.adcx().suggestions();
let span = cx.inner_span;
cx.emit_lint(
INVALID_DOC_ATTRIBUTES,
IllFormedAttributeInput::new(&suggestions, None, None),
span,
);
}
}
}
}
Expand All @@ -749,7 +749,6 @@ impl AttributeParser for DocParser {
template!(
List: &[
"alias",
"attribute",
"hidden",
"html_favicon_url",
"html_logo_url",
Expand All @@ -762,19 +761,10 @@ impl AttributeParser for DocParser {
"masked",
"cfg",
"notable_trait",
"keyword",
"fake_variadic",
"search_unbox",
"rust_logo",
"auto_cfg",
"test",
"spotlight",
"include",
"no_default_passes",
"passes",
"plugins",
],
NameValueStr: "string"
NameValueStr: "doc comment"
),
AttributeStability::Stable, // Some parts of the attribute are unstable, manually checked in parser
|this, cx, args| {
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/lints/invalid-doc-attr-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ note: the lint level is defined here
LL | #![deny(invalid_doc_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
error: valid forms for the attribute are `doc = "doc comment"`, `doc(alias)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(masked)`, `doc(no_inline)`, `doc(notable_trait)`, and `doc(test)`
--> $DIR/invalid-doc-attr-2.rs:6:4
|
LL | #![doc]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/malformed-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
//~^ ERROR malformed
#[doc]
//~^ ERROR
#[doc()]
//~^ ERROR
#[rustc_macro_transparency]
//~^ ERROR malformed
//~| ERROR attribute cannot be used on
Expand Down Expand Up @@ -76,8 +78,6 @@
#[crate_name]
//~^ ERROR malformed
//~| WARN crate-level attribute should be an inner attribute
#[doc]
//~^ ERROR
#[target_feature]
//~^ ERROR malformed
#[export_stable = 1]
Expand Down
54 changes: 27 additions & 27 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ LL | #[deprecated = 5]
| expected a string literal here

error[E0539]: malformed `rustc_macro_transparency` attribute input
--> $DIR/malformed-attrs.rs:43:3
--> $DIR/malformed-attrs.rs:45:3
|
LL | #[rustc_macro_transparency]
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -206,23 +206,23 @@ LL | #[rustc_macro_transparency = "transparent"]
| +++++++++++++++

error: the `rustc_macro_transparency` attribute cannot be used on functions
--> $DIR/malformed-attrs.rs:43:3
--> $DIR/malformed-attrs.rs:45:3
|
LL | #[rustc_macro_transparency]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the `rustc_macro_transparency` attribute can only be applied to macro defs

error[E0539]: malformed `repr` attribute input
--> $DIR/malformed-attrs.rs:46:3
--> $DIR/malformed-attrs.rs:48:3
|
LL | #[repr]
| ^^^^ expected this to be a list
|
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>

error[E0565]: malformed `rustc_as_ptr` attribute input
--> $DIR/malformed-attrs.rs:48:3
--> $DIR/malformed-attrs.rs:50:3
|
LL | #[rustc_as_ptr = 5]
| ^^^^^^^^^^^^^---
Expand All @@ -236,7 +236,7 @@ LL + #[rustc_as_ptr]
|

error[E0539]: malformed `rustc_align` attribute input
--> $DIR/malformed-attrs.rs:53:3
--> $DIR/malformed-attrs.rs:55:3
|
LL | #[rustc_align]
| ^^^^^^^^^^^ expected this to be a list
Expand All @@ -247,7 +247,7 @@ LL | #[rustc_align(<alignment in bytes>)]
| ++++++++++++++++++++++

error[E0539]: malformed `optimize` attribute input
--> $DIR/malformed-attrs.rs:55:3
--> $DIR/malformed-attrs.rs:57:3
|
LL | #[optimize]
| ^^^^^^^^ expected this to be a list
Expand All @@ -262,7 +262,7 @@ LL | #[optimize(speed)]
| +++++++

error[E0805]: malformed `optimize` attribute input
--> $DIR/malformed-attrs.rs:57:3
--> $DIR/malformed-attrs.rs:59:3
|
LL | #[optimize(none, none)]
| ^^^^^^^^------------
Expand All @@ -282,7 +282,7 @@ LL + #[optimize(speed)]
|

error[E0805]: malformed `optimize` attribute input
--> $DIR/malformed-attrs.rs:59:3
--> $DIR/malformed-attrs.rs:61:3
|
LL | #[optimize(none, speed)]
| ^^^^^^^^-------------
Expand All @@ -302,7 +302,7 @@ LL + #[optimize(speed)]
|

error[E0565]: malformed `cold` attribute input
--> $DIR/malformed-attrs.rs:61:3
--> $DIR/malformed-attrs.rs:63:3
|
LL | #[cold = 1]
| ^^^^^---
Expand All @@ -316,7 +316,7 @@ LL + #[cold]
|

error[E0539]: malformed `must_use` attribute input
--> $DIR/malformed-attrs.rs:63:3
--> $DIR/malformed-attrs.rs:65:3
|
LL | #[must_use()]
| ^^^^^^^^--
Expand All @@ -334,7 +334,7 @@ LL + #[must_use = "reason"]
|

error[E0565]: malformed `no_mangle` attribute input
--> $DIR/malformed-attrs.rs:65:3
--> $DIR/malformed-attrs.rs:67:3
|
LL | #[no_mangle = 1]
| ^^^^^^^^^^---
Expand All @@ -348,7 +348,7 @@ LL + #[no_mangle]
|

error[E0565]: malformed `naked` attribute input
--> $DIR/malformed-attrs.rs:67:3
--> $DIR/malformed-attrs.rs:69:3
|
LL | #[unsafe(naked())]
| ^^^^^^^^^^^^--^
Expand All @@ -362,7 +362,7 @@ LL + #[unsafe(naked)]
|

error[E0565]: malformed `track_caller` attribute input
--> $DIR/malformed-attrs.rs:69:3
--> $DIR/malformed-attrs.rs:71:3
|
LL | #[track_caller()]
| ^^^^^^^^^^^^--
Expand All @@ -376,7 +376,7 @@ LL + #[track_caller]
|

error[E0539]: malformed `export_name` attribute input
--> $DIR/malformed-attrs.rs:71:3
--> $DIR/malformed-attrs.rs:73:3
|
LL | #[export_name()]
| ^^^^^^^^^^^^^
Expand All @@ -388,7 +388,7 @@ LL + #[export_name = "name"]
|

error[E0805]: malformed `used` attribute input
--> $DIR/malformed-attrs.rs:73:3
--> $DIR/malformed-attrs.rs:75:3
|
LL | #[used()]
| ^^^^--
Expand All @@ -406,15 +406,15 @@ LL | #[used(linker)]
| ++++++

error: the `used` attribute cannot be used on functions
--> $DIR/malformed-attrs.rs:73:3
--> $DIR/malformed-attrs.rs:75:3
|
LL | #[used()]
| ^^^^
|
= help: the `used` attribute can only be applied to statics

error[E0539]: malformed `crate_name` attribute input
--> $DIR/malformed-attrs.rs:76:3
--> $DIR/malformed-attrs.rs:78:3
|
LL | #[crate_name]
| ^^^^^^^^^^
Expand Down Expand Up @@ -852,7 +852,7 @@ LL | | #[coroutine = 63] || {}
LL | | }
| |_- not a `const fn`

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
error: valid forms for the attribute are `doc = "doc comment"`, `doc(alias)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(masked)`, `doc(no_inline)`, `doc(notable_trait)`, and `doc(test)`
--> $DIR/malformed-attrs.rs:41:3
|
LL | #[doc]
Expand All @@ -864,8 +864,14 @@ note: the lint level is defined here
LL | #![deny(invalid_doc_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^

error: valid forms for the attribute are `doc = "doc comment"`, `doc(alias)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(masked)`, `doc(no_inline)`, `doc(notable_trait)`, and `doc(test)`
--> $DIR/malformed-attrs.rs:43:3
|
LL | #[doc()]
| ^^^^^

error: valid forms for the attribute are `inline`, `inline(always)`, and `inline(never)`
--> $DIR/malformed-attrs.rs:50:3
--> $DIR/malformed-attrs.rs:52:3
|
LL | #[inline = 5]
| ^^^^^^^^^^
Expand All @@ -875,7 +881,7 @@ LL | #[inline = 5]
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![crate_name]`
--> $DIR/malformed-attrs.rs:76:1
--> $DIR/malformed-attrs.rs:78:1
|
LL | #[crate_name]
| ^^^^^^^^^^^^^
Expand All @@ -890,12 +896,6 @@ LL | | }
| |_^
= note: requested on the command line with `-W unused-attributes`

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
--> $DIR/malformed-attrs.rs:79:3
|
LL | #[doc]
| ^^^

warning: the `link` attribute cannot be used on functions
--> $DIR/malformed-attrs.rs:85:3
|
Expand Down Expand Up @@ -990,7 +990,7 @@ Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805
For more information about an error, try `rustc --explain E0308`.
Future incompatibility report: Future breakage diagnostic:
error: valid forms for the attribute are `inline`, `inline(always)`, and `inline(never)`
--> $DIR/malformed-attrs.rs:50:3
--> $DIR/malformed-attrs.rs:52:3
|
LL | #[inline = 5]
| ^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/malformed/malformed-regressions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | #[link = ""]
|
= note: for more information, visit <https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute>

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
error: valid forms for the attribute are `doc = "doc comment"`, `doc(alias)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(masked)`, `doc(no_inline)`, `doc(notable_trait)`, and `doc(test)`
--> $DIR/malformed-regressions.rs:3:3
|
LL | #[doc]
Expand Down
Loading