Skip to content

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

Draft
hvitved wants to merge 3 commits into
github:mainfrom
hvitved:hvitved-pattern-variable-accesses
Draft

hvitved wants to merge 3 commits 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.

Reuse the defining-access extraction from ordinary initialized declarations for typed, var, recursive, and tuple patterns. The QL library treats the access as the pattern definition's write target, not as a read. Ordinary declarations and discards are unchanged.

The regression test verifies that each pattern access targets the declared variable and is classified as a write. Rebuilt the extractor and ran 124 related C# tests, including the affected AST, control-flow, and data-flow tests.

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

This branch has not been deployed

No deployments
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.

2 participants