Skip to content

C#: Extract defining accesses for pattern variables - #23183

Merged
hvitved merged 1 commit into
github:mainfrom
hvitved:hvitved-pattern-variable-accesses
Nov 23, 2026
Merged

hvitved merged 1 commit into
github:mainfrom
hvitved:hvitved-pattern-variable-accesses

Conversation

@hvitved

@hvitved hvitved commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The C# extractor recorded local variables declared in patterns such as o is MyStruct ms1, but omitted their defining access. As a result, LocalVariableDeclExpr.getAccess() returned no result for those declarations.

Needed for #23170, where the CFG for patterns will be changed to include the defining accesses.

DCA is uneventful

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.

🟡 Changes recommended

Stale test baselines are expected to fail with the rebuilt extractor, and new pattern accesses are misclassified as variable uses, producing self-referential jump-to-definition links.

2 open issues
What changed in this PR

Adds defining accesses for C# pattern variables so the extractor and QL libraries expose them consistently as write targets.

Changes:

  • Reuse defining-access extraction for typed, var, recursive, and tuple patterns.
  • Associate pattern definitions with write-only target accesses and clarify API documentation.
  • Add regression coverage, update affected test expectations, and document the change.
File Description
csharp/​ql/​test/​library-tests/​dataflow/​patterns/​PatternFlow.expected Adds pattern-definition steps to expected data-flow paths.
csharp/​ql/​test/​library-tests/​csharp9/​PrintAst.expected Includes defining accesses in C# 9 pattern ASTs.
csharp/​ql/​test/​library-tests/​csharp8/​switchstmtctrlflow.expected Updates switch-statement control-flow expectations.
csharp/​ql/​test/​library-tests/​csharp8/​switchexprcontrolflow.expected Updates switch-expression control-flow expectations.
csharp/​ql/​test/​library-tests/​csharp8/​PrintAst.expected Includes defining accesses in C# 8 pattern ASTs.
csharp/​ql/​test/​library-tests/​csharp8/​patterns.ql Checks access targets and write-only classification.
csharp/​ql/​test/​library-tests/​csharp8/​patterns.expected Records expected defining-access regression results.
csharp/​ql/​test/​library-tests/​csharp8/​ispatternflow.expected Updates is-pattern control-flow expectations.
csharp/​ql/​test/​library-tests/​csharp7/​PrintAst.expected Includes defining accesses in C# 7 pattern ASTs.
csharp/​ql/​test/​library-tests/​csharp7/​LocalTaintFlow.expected Uses defining accesses in expected taint-flow steps.
csharp/​ql/​test/​library-tests/​csharp7/​IsFlow.expected Includes defining accesses in pattern control flow.
csharp/​ql/​test/​library-tests/​csharp11/​PrintAst.expected Includes defining accesses within list patterns.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​NodeGraph.expected Adds access nodes and their control-flow edges.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​EntryElement.expected Updates pattern entry elements.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​EnclosingCallable.expected Associates new nodes with their enclosing callables.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​Dominance.expected Updates dominance and post-dominance expectations.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​Condition.expected Updates conditional-flow endpoints.
csharp/​ql/​test/​library-tests/​controlflow/​graph/​BasicBlock.expected Updates basic-block node counts.
csharp/​ql/​test/​library-tests/​assignables/​AssignableDefinition.expected Records target accesses for pattern definitions.
csharp/​ql/​test/​library-tests/​assignables/​AssignableAccess.expected Classifies pattern defining accesses as writes.
csharp/​ql/​lib/​semmle/​code/​csharp/​exprs/​Expr.qll Clarifies the getAccess() docstring.
csharp/​ql/​lib/​semmle/​code/​csharp/​Assignable.qll Maps pattern definitions to target accesses and updates documentation.
csharp/​ql/​lib/​change-notes/​2026-11-20-pattern-defining-access.md Documents pattern defining-access support.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​VariableDeclaration.cs Shares defining-access creation and enables it for patterns.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​Patterns/​RecursivePattern.cs Enables defining-access extraction for recursive patterns.
csharp/​extractor/​Semmle.Extraction.CSharp/​Entities/​Expressions/​Patterns/​Pattern.cs Enables defining-access extraction for typed and var patterns.

Review effort: Max

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.

🟡 Changes recommended

Two regression baselines omit newly extracted variable accesses, causing the affected tests to return unexpected rows.

1 open issue
2 resolved since last review

Review effort: Max

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved
hvitved force-pushed the hvitved-pattern-variable-accesses branch from bd0ebb6 to 548c27b Compare November 23, 2026 08:25
@hvitved
hvitved marked this pull request as ready for review November 23, 2026 08:32
@hvitved
hvitved requested a review from a team as a code owner November 23, 2026 08:32
@hvitved
hvitved requested a review from michaelnebel November 23, 2026 08:32

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

Nice!

@hvitved
hvitved merged commit ae570b2 into github:main Nov 23, 2026
26 checks passed
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.

3 participants