Skip to content

Conversation

@J3m3
Copy link

@J3m3 J3m3 commented Dec 14, 2025

Description

Reverse the order of returned lint attributes for a SyntaxNode to match rustc's behavior.

When multiple lint attributes are present, rustc overrides earlier ones with the last defined attribute. The previous iteration order was incorrect, causing earlier attributes to override the later ones.

Before this PR

image

After this PR

image

Is it worthwhile?

Generally, no one wants to specify same lints with different levels in a row. However, it is technically possible for users to use -Zcrate-attr to inject crate-level lint attributes at the top of the crate root file. This means any overlapping lints passed by -Zcrate-attr should not override the corresponding ones specified in the source file.

If we're trying to mimic the behavior of -Zcrate-attr when introducing a new field in the rust-project.json format that can inject crate-level attributes, it would need to handle lint attributes in a similar fashion.

Overall, I'm not sure this is truly worthwhile, since neither scenario seems to be a common use case (even the decision to support lint attributes hasn't been made yet). That said, I don't see a good reason to leave it as-is, especially since the fix isn't difficult to implement.

Reference setup for reproduction

main.rs

#![allow(unused)]
#![deny(unused)]

fn main() {
    let should_be_denied = 0;

    #[warn(unused)]
    #[allow(unused)]
    let should_be_allowed = 0;
}

rust-project.json

{
    "sysroot": "/usr/local/rustup/toolchains/1.91.1-aarch64-unknown-linux-gnu",
    "sysroot_src": "/usr/local/rustup/toolchains/1.91.1-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library",
    "crates": [
        {
            "cfg": [],
            "deps": [],
            "edition": "2024",
            "is_proc_macro": false,
            "root_module": "/root/workspace/lint-attr-order/main.rs"
        }
    ]
}

.vscode/settings.json

{
    "rust-analyzer.diagnostics.experimental.enable": true
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 14, 2025
@J3m3
Copy link
Author

J3m3 commented Dec 14, 2025

Hmm... not sure why the test failed. IIUC, syntax_highlighting::tests::syntax_highlighting_not_quadratic doesn't even call hir::semantics::Semantics::lint_attrs.

Edit: I think it occasionally fails since it's a time-based test.

@J3m3 J3m3 force-pushed the lint-attr-order branch 2 times, most recently from 26fcd04 to a0647d7 Compare December 14, 2025 06:20
Reverse the order of returned lint attributes for a `SyntaxNode` to
match rustc's behavior.

When multiple lint attributes are present, rustc overrides earlier ones
with the last defined attribute. The previous iteration order was
incorrect, causing earlier attributes to override the later ones.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants