Skip to content

improve diagnostic for literals in lint attributes - #164486

Open
annieversary wants to merge 5 commits into
rust-lang:mainfrom
annieversary:bad-attribute-lit
Open

annieversary wants to merge 5 commits into
rust-lang:mainfrom
annieversary:bad-attribute-lit

Conversation

@annieversary

Copy link
Copy Markdown

Improves the diagnostic for literals in lint attributes, like #[allow("meow")].

Before:

error[E0452]: malformed lint attribute input
 --> main.rs:5:9
  |
5 | #[allow("meow")]
  |         ^^^^^^ bad attribute argument

error[E0452]: malformed lint attribute input
 --> main.rs:8:8
  |
8 | #[deny(1.0)]
  |        ^^^ bad attribute argument

After:

error: expected path to a lint, found literal
 --> main.rs:5:9
  |
5 | #[allow("meow")]
  |         ^^^^^^ not a lint
  |
  = help: try using `#[allow(meow)]`

error: expected path to a lint, found literal
 --> main.rs:8:8
  |
8 | #[deny(1.0)]
  |        ^^^ not a lint
  |
  = help: for example, write `#[deny(dead_code)]` for `dead_code`

Probably easier to review with whitespace changes turned off.

r? JonathanBrouwer

@rustbot

rustbot commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 17, 2026
@rustbot

rustbot commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

JonathanBrouwer is currently at their maximum review capacity.
They may take a while to respond.

@rustbot

rustbot commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @JonathanBrouwer (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

@jdonszelmann jdonszelmann left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add a test for this change?

View changes since this review

Comment thread compiler/rustc_attr_parsing/src/attributes/lint.rs Outdated
@jdonszelmann

Copy link
Copy Markdown
Contributor

Also grats on your first contrib ^.^

@rust-log-analyzer

This comment has been minimized.

@annieversary

Copy link
Copy Markdown
Author

I added a test (as a commit before the one with my change, as suggested by @N1ark), changed the original commit to update this test and another that failed, and made another commit to rename sub to kind. :3

Comment thread tests/ui/diagnostic-flags/deduplicate-diagnostics.deduplicate.stderr Outdated
Comment thread tests/ui/diagnostic-flags/deduplicate-diagnostics.rs
Comment thread tests/ui/lint/lint-attribute-literals.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@JonathanBrouwer JonathanBrouwer left a comment •

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.

}

#[derive(Diagnostic)]
#[diag("expected path to a lint, found literal")]

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.

"expected a lint, found literal"

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.

lint names are indeed paths, but I think emphasizing this is not needed

pub(crate) enum BadAttributeLitHelp {
#[help("try using `#[{$kind}({$sym})]`")]
StrLit { sym: Symbol, kind: Symbol },
#[help("for example, write `#[{$kind}(dead_code)]` for `dead_code`")]

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.

Could you also add a case that covers this variant to tests/ui/lint/lint-attribute-literals.rs?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2026
@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants