Skip to content

Rust: Remove elements superseded by attribute macro expansions - #20770

Merged
hvitved merged 2 commits into
github:mainfrom
hvitved:rust/attribute-macro-expansion-filter
Nov 12, 2025
Merged

Rust: Remove elements superseded by attribute macro expansions#20770
hvitved merged 2 commits into
github:mainfrom
hvitved:rust/attribute-macro-expansion-filter

Conversation

@hvitved

@hvitvedhvitved commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Follows up on #20454 and removes elements superseded by attribute macro expansions entirely from the Element type. We only ever care about the expanded version, which will typically contain a copy of the original definition.

DCA looks good: we reduce inconsistencies, increase call resolution rates, and eliminate rust/unused-variable false positives.

@github-actionsgithub-actionsBot added Rust Pull requests that update Rust code Swift labels Nov 6, 2025
@hvitved
hvitvedforce-pushed the rust/attribute-macro-expansion-filter branch 7 times, most recently from e305f21 to 20bb58dCompareNovember 9, 2025 18:24
@hvitved
hvitvedforce-pushed the rust/attribute-macro-expansion-filter branch from 20bb58d to c81f5f5CompareNovember 10, 2025 08:19
@hvitvedhvitved added the no-change-note-required This PR does not need a change note label Nov 10, 2025
@hvitved
hvitved marked this pull request as ready for review November 10, 2025 11:30
@hvitved
hvitved requested a review from a team as a code ownerNovember 10, 2025 11:30
CopilotAI review requested due to automatic review settings November 10, 2025 11:30

CopilotAI 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.

Pull Request Overview

This PR refactors macro expansion handling in the Rust QL library by consolidating logic into a central MacroExpansion module and introducing the isFromMacroExpansion() predicate to better distinguish between macro arguments and macro-generated code. The changes improve code organization, reduce duplication, and update test expectations to reflect the new behavior.

  • Centralized macro expansion logic from MacroCallImpl and PathResolution into ElementImpl::MacroExpansion
  • Introduced isFromMacroExpansion() to exclude macro arguments from expansion checks
  • Updated queries to use the new predicate where appropriate
  • Added custom toString() for items with attribute macro expansions

Reviewed Changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
rust/ql/lib/codeql/rust/elements/internal/ElementImpl.qllAdds centralized MacroExpansion module with logic for classifying macro-related elements
rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qllUpdates isInMacroExpansion() and isFromMacroExpansion() to delegate to MacroExpansion module
rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qllRemoves isInMacroExpansion() and getATokenTreeNode() logic, now in ElementImpl
rust/ql/lib/codeql/rust/elements/internal/ItemImpl.qllAdds custom toString() for items with attribute macro expansions
rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qllUpdates to use new getImmediatelyEnclosingMacroInvocation() helper
rust/ql/lib/codeql/rust/internal/PathResolution.qllSimplifies ItemNode constructor by removing supersededByAttributeMacroExpansion()
rust/ql/src/queries/unusedentities/UnusedValue.qlUpdates to use isFromMacroExpansion() instead of isInMacroExpansion()
rust/ql/src/queries/unusedentities/UnreachableCode.qlUpdates to use isFromMacroExpansion() instead of isInMacroExpansion()
rust/ql/src/queries/unusedentities/UnusedVariable.qllAdds TODO comment noting future migration to isFromMacroExpansion()
rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qllSimplifies itemAt() by removing unused inMacro parameter
rust/ql/test/library-tests/variables/variables.qlUpdates to use isFromMacroExpansion() instead of isInMacroExpansion()
Test expectation filesUpdates expected test outputs to reflect deduplication and new behavior
.gitattributes and .generated.listRemoves ItemImpl.qll from generated file list

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadrust/ql/lib/codeql/rust/elements/internal/ElementImpl.qll Outdated
Comment threadrust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
let a: u16;
let b: u16 = 2;
set_value!(a, 1);
set_value!(a, 1); // $ Alert[rust/unused-value]

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.

👍

I'm happy with all of the changes to test results in this PR, and I've made a comment in the DCA run about the results there. I haven't reviewed much of the actual QL (yet).

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.

Update: happy with the DCA results as well (following some investigation).

@geoffw0geoffw0 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.

Code looks good, though its complex enough I think we're leaning on test coverage for confidence here. And test results / DCA LGTM.

@hvitved
hvitved merged commit 467bd54 into github:mainNov 12, 2025
20 checks passed
@hvitved
hvitved deleted the rust/attribute-macro-expansion-filter branch November 12, 2025 18:52
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-requiredThis PR does not need a change noteRustPull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@hvitved@geoffw0