fix: respect rustc's lint attribute application order #21265
+60
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Reverse the order of returned lint attributes for a
SyntaxNodeto 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
After this PR
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-attrto inject crate-level lint attributes at the top of the crate root file. This means any overlapping lints passed by-Zcrate-attrshould not override the corresponding ones specified in the source file.If we're trying to mimic the behavior of
-Zcrate-attrwhen introducing a new field in therust-project.jsonformat 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.rsrust-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 }