Skip to content

C#: Data flow for positional patterns. - #22689

Draft
michaelnebel wants to merge 8 commits into
github:mainfrom
michaelnebel:csharp/positionalpatternflow
Draft

michaelnebel wants to merge 8 commits into
github:mainfrom
michaelnebel:csharp/positionalpatternflow

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

In this PR we add data flow support via positional patterns. That is,

var x = (1, (2, 3), 4);
switch (x)
{
    case (var a1, (var b1, var c1), _):
        Sink(a1);        // has value flow 1
        Sink(c1);        // has value flow 3
        Sink(b1);       // has value flow 2.
        break;
}

Note, that positional patterns are similar to tuple patterns (var (x, y)), but they haven't been handled in the data flow library before now.

@michaelnebel michaelnebel changed the title Csharp/positionalpatternflow C#: Data flow for positional patterns. Sep 29, 2026
@michaelnebel
michaelnebel force-pushed the csharp/positionalpatternflow branch from 84d670d to f059beb Compare September 30, 2026 12:22
@michaelnebel
michaelnebel force-pushed the csharp/positionalpatternflow branch from f059beb to f3eaab2 Compare October 2, 2026 08:17

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.

Copilot review overview

🟡 Changes recommended

Nested positional patterns that capture the whole inner tuple still lose flow to the capture variable.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds tuple-backed positional-pattern support to C# data-flow analysis, including nested patterns.

Changes:

  • Models flow through positional patterns in is expressions and switch cases.
  • Adds nested-pattern tests and updates generated expectations.
  • Documents the analysis improvement.
File Description
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.expected Updates expected flow results.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.cs Adds nested positional-pattern tests.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​PrintAst.expected Updates expected test syntax trees.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​DataFlowStep.expected Updates expected flow steps.
csharp/​ql/​lib/​semmle/​code/​csharp/​dataflow/​internal/​DataFlowPrivate.qll Adds positional-pattern flow modeling.
csharp/​ql/​lib/​change-notes/​2026-10-02-positional-pattern-flow.md Documents the new support.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll Outdated

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