about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-03-20 19:24:48 +0000
committerbors <bors@rust-lang.org>2025-03-20 19:24:48 +0000
commit6fb13c67989bf3690ca68a6b408ed2bcf18b57b1 (patch)
treeaa906717d793a32cd246042c0cef5d5be58a4e6e
parente85fcabd9f28e7fea60d0f87273d3b9e00029cb6 (diff)
parent6cf2d721508ab8879be65bbfc5ca74ed64c87d05 (diff)
downloadrust-6fb13c67989bf3690ca68a6b408ed2bcf18b57b1.tar.gz
rust-6fb13c67989bf3690ca68a6b408ed2bcf18b57b1.zip
Auto merge of #138515 - petrochenkov:cfgtrace, r=nnethercote
expand: Leave traces when expanding `cfg_attr` attributes

Currently `cfg_trace` just disappears during expansion, but after this PR `#[cfg_attr(some tokens)]` will leave a `#[cfg_attr_trace(some tokens)]` attribute instead of itself in AST after expansion (the new attribute is built-in and inert, its inner tokens are the same as in the original attribute).
This trace attribute can then be used by lints or other diagnostics, #133823 has some examples.

Tokens in these trace attributes are set to an empty token stream, so the traces are non-existent for proc macros and cannot affect any user-observable behavior.
This is also a weakness, because if a proc macro processes some code with the trace attributes, they will be lost, so the traces are best effort rather than precise.

The next step is to do the same thing with `cfg` attributes (`#[cfg(TRUE)]` currently remains in both AST and tokens after expanding, it should be replaced with a trace instead).

The idea belongs to `@estebank.`
-rw-r--r--clippy_lints/src/attrs/duplicated_attributes.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/clippy_lints/src/attrs/duplicated_attributes.rs b/clippy_lints/src/attrs/duplicated_attributes.rs
index 2ddbc7a6a76..5c486eb90cc 100644
--- a/clippy_lints/src/attrs/duplicated_attributes.rs
+++ b/clippy_lints/src/attrs/duplicated_attributes.rs
@@ -36,7 +36,11 @@ fn check_duplicated_attr(
     }
     let Some(ident) = attr.ident() else { return };
     let name = ident.name;
-    if name == sym::doc || name == sym::cfg_attr || name == sym::rustc_on_unimplemented || name == sym::reason {
+    if name == sym::doc
+        || name == sym::cfg_attr
+        || name == sym::cfg_attr_trace
+        || name == sym::rustc_on_unimplemented
+        || name == sym::reason {
         // FIXME: Would be nice to handle `cfg_attr` as well. Only problem is to check that cfg
         // conditions are the same.
         // `#[rustc_on_unimplemented]` contains duplicated subattributes, that's expected.