Skip to content

C#: Extract defining accesses for local declarations - #23210

Merged
hvitved merged 1 commit into
github:mainfrom
hvitved:hvitved-all-local-defining-accesses
Nov 25, 2026
Merged

hvitved merged 1 commit into
github:mainfrom
hvitved:hvitved-all-local-defining-accesses

Conversation

@hvitved

@hvitved hvitved commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

#23183 follow-up; add defining accesses for all local declarations.

DCA has some result changes, but I think much of it is wobble.

@hvitved
hvitved requested a review from a team as a code owner November 23, 2026 14:12
Copilot AI adaptive review requested due to automatic review settings November 23, 2026 14:12
@hvitved
hvitved marked this pull request as draft November 23, 2026 14:12

Copilot AI 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.

🔵 Needs a closer look

The new C# declaration-access nodes affect interacting AST, assignable read/write, CFG, SSA/data-flow, and jump-to-definition contracts, requiring broader semantic review than automated approval supports.

No open issues 🎉

What changed in this PR

This PR makes local declaration accesses more consistent across the C# extractor and CodeQL libraries, supporting definition tracking and data-flow analysis without treating declarations as jump-to-definition uses.

Changes:

  • Extract defining accesses for uninitialized locals, deconstruction, foreach, and catch variables.
  • Connect these accesses to assignable definitions and exclude declaration accesses from navigation uses.
  • Add focused regression coverage, separate tuple path and inline tests, and update affected baselines.
File Description
csharp/​ql/​test/​library-tests/​definitions/​PrintAst.expected Updates catch and foreach declaration ASTs.
csharp/​ql/​test/​library-tests/​defining-accesses/​PrintAst.qlref Enables focused AST testing.
csharp/​ql/​test/​library-tests/​defining-accesses/​PrintAst.expected Records declaration-access AST coverage.
csharp/​ql/​test/​library-tests/​defining-accesses/​options Configures C# 8 and LINQ support.
csharp/​ql/​test/​library-tests/​defining-accesses/​Flow.ql Checks source-to-sink data flow.
csharp/​ql/​test/​library-tests/​defining-accesses/​Flow.expected Records expected declaration-related flows.
csharp/​ql/​test/​library-tests/​defining-accesses/​definitions.qlref Enables navigation-use testing.
csharp/​ql/​test/​library-tests/​defining-accesses/​definitions.expected Records uses excluding defining accesses.
csharp/​ql/​test/​library-tests/​defining-accesses/​DefiningAccesses.ql Checks access targets, classification, and definitions.
csharp/​ql/​test/​library-tests/​defining-accesses/​DefiningAccesses.expected Records expected defining-access associations.
csharp/​ql/​test/​library-tests/​defining-accesses/​DefiningAccesses.cs Adds varied declaration and flow fixtures.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​TuplesInline.ql Separately runs inline flow assertions.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​TuplesInline.expected Adds an empty inline-failure baseline.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.ql Separates path output from inline assertions.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.expected Updates paths through defining accesses.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​PrintAst.expected Adds tuple declaration access nodes.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​DataFlowStep.expected Updates tuple definition flow steps.
csharp/​ql/​test/​library-tests/​dataflow/​local/​TaintTrackingStep.expected Updates a declaration-related taint step.
csharp/​ql/​test/​library-tests/​dataflow/​local/​DataFlowStep.expected Updates a declaration-related flow step.
csharp/​ql/​test/​library-tests/​csharp9/​PrintAst.expected Updates foreach and deconstruction ASTs.
csharp/​ql/​test/​library-tests/​csharp8/​switchexprcontrolflow.expected Updates declaration control-flow nodes.
csharp/​ql/​test/​library-tests/​csharp8/​PrintAst.expected Updates foreach, tuple, and catch ASTs.
csharp/​ql/​test/​library-tests/​csharp7/​TaintReaches.expected Records taint reaching defining accesses.
csharp/​ql/​test/​library-tests/​csharp7/​PrintAst.expected Adds defining accesses across declaration forms.
csharp/​ql/​test/​library-tests/​csharp7/​LocalTaintFlow.expected Updates deconstruction taint-flow steps.
csharp/​ql/​test/​library-tests/​csharp11/​PrintAst.expected Adds an uninitialized local’s defining access.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​EntryElement.expected Updates declaration entry elements.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​EnclosingCallable.expected Records enclosing callables for new nodes.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​Condition.expected Updates condition relationships for declaration blocks.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​BasicBlock.expected Updates block boundaries and node counts.
csharp/​ql/​test/​library-tests/​assignables/​AssignableDefinition.expected Adds definition-to-access associations.
csharp/​ql/​test/​library-tests/​assignables/​AssignableAccess.expected Records new accesses as writes.
csharp/​ql/​lib/​semmle/​code/​csharp/​Assignable.qll Associates local and tuple definitions with accesses.
csharp/​ql/​lib/​definitions.qll Excludes defining accesses from navigation uses.
csharp/​ql/​lib/​change-notes/​2026-11-23-all-local-defining-accesses.md Documents expanded defining-access extraction.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​VariableDeclaration.cs Creates defining accesses for additional declaration forms.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​Query.cs Preserves separate range-variable access creation.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​Patterns/​RecursivePattern.cs Uses default defining-access creation.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​Patterns/​Pattern.cs Updates pattern callers to the revised API.

Review effort: Balanced (adaptive)

@michaelnebel

Copy link
Copy Markdown
Contributor

@hvitved : You need to re-base to avoid failing stubgentest. #23212

@hvitved
hvitved force-pushed the hvitved-all-local-defining-accesses branch 3 times, most recently from ff9c444 to d0bc5b9 Compare November 24, 2026 09:10
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved
hvitved force-pushed the hvitved-all-local-defining-accesses branch from d0bc5b9 to e57b5f7 Compare November 24, 2026 15:24
@hvitved
hvitved marked this pull request as ready for review November 24, 2026 15:24
@hvitved
hvitved requested a review from michaelnebel November 24, 2026 15:24

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

Changes looks good to me, but it is hard to say exactly what the full impact is.
A couple of questions.

  • It looks like we see more expressions with unknown types for many buildless projects? I suppose this is because we extract more (defining access) expressions, which might not have a type (if for instance of the variable doesn't have a type)?
  • There are some discrepancies in the cs/unused-collection and cs/useless-assignment-to-local that might not be due to wobble; It appears that the precision of these queries have increased for tuple assignable definitions?

@hvitved

hvitved commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author
  • It looks like we see more expressions with unknown types for many buildless projects? I suppose this is because we extract more (defining access) expressions, which might not have a type (if for instance of the variable doesn't have a type)?

Yes, that would explain the increase.

  • There are some discrepancies in the cs/unused-collection and cs/useless-assignment-to-local that might not be due to wobble; It appears that the precision of these queries have increased for tuple assignable definitions?

For cs/unused-collection we now also get results when a local variable is initialized is a tuple pattern, but is otherwise unused, because the forex in the query now actually has an access (the defining access). I think this is OK.

For cs/useless-assignment-to-local, I'm not really sure what exactly is going on, but the 7 results that have been removed all appear to be FPs, but so do the 3 added results. I think this is such a small diff that it should not be blocking.

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

LGTM!

@hvitved
hvitved merged commit fd0f903 into github:main Nov 25, 2026
27 checks passed
@hvitved
hvitved deleted the hvitved-all-local-defining-accesses branch November 25, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants