Skip to content

C#: Fix missed All opportunities with early returns - #23145

Open
baywet wants to merge 2 commits into
github:mainfrom
baywet:fix/csharp-missed-all-early-return
Open

baywet wants to merge 2 commits into
github:mainfrom
baywet:fix/csharp-missed-all-early-return

Conversation

@baywet

@baywet baywet commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Recognize foreach loops that return false when an element fails a condition and are immediately followed by return true as opportunities to use LINQ's All.
  • Preserve the existing assignment-and-break detection and avoid suggestions involving additional statements, else branches, captured reference parameters, or awaited predicates.
  • Add regression coverage based on the missed pattern in fix: improved external ref file loading microsoft/OpenAPI.NET#3164, including braced and unbraced loops, and add query/library change notes.

Validation

  • Confirmed the new positive regression cases failed before the query fix.
  • All 5 C# LINQ query tests passed in Docker using CodeQL CLI 2.27.4.
  • QL formatting check and git diff --check passed.
  • Updated .expected results using codeql test accept; no manual expected-file edits.

Recognize foreach loops that return false on a failed condition and return true immediately after the loop. Add regression coverage and change notes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b48b187b-dcc1-4a8b-a428-b9ee51f638f4
@baywet
baywet requested a review from a team as a code owner November 16, 2026 18:39
Copilot AI adaptive review requested due to automatic review settings November 16, 2026 18:39

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

The new return-based path can recommend LINQ lambdas that cannot compile because they capture ref or ref-like values; these accesses need exclusion and negative regression coverage.

1 open issue
What changed in this PR

Extends the C# cs/linq/missed-all query to recognize early-return loops that could use LINQ’s All, while preserving existing assignment-and-break detection.

Changes:

  • Detects loops that return false on a failed condition and are immediately followed by return true.
  • Adds positive and negative regression cases, with updated expected results.
  • Adds query and library change notes.
File Description
csharp/​ql/​test/​query-tests/​Linq/​MissedAllOpportunity/​MissedAllOpportunity.expected Records the two new expected alerts.
csharp/​ql/​test/​query-tests/​Linq/​MissedAllOpportunity/​MissedAllOpportunity.cs Tests braced and unbraced early returns and excluded patterns.
csharp/​ql/​src/​change-notes/​2026-11-16-missed-all-early-return.md Documents expanded query detection.
csharp/​ql/​lib/​Linq/​Helpers.qll Adds early-return matching and eligibility checks.
csharp/​ql/​lib/​change-notes/​2026-11-16-missed-all-early-return.md Documents the library predicate enhancement.

Review effort: Balanced (adaptive)

Comment thread csharp/ql/lib/Linq/Helpers.qll Resolved
Exclude ref locals and ref-like locals or parameters from the new All candidate path. Add negative regressions for ref and ref readonly locals and generic ref-struct locals and parameters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b48b187b-dcc1-4a8b-a428-b9ee51f638f4

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 LINQ path can suggest code that fails to compile by capturing a struct’s this, and its production-query impact still needs DCA validation.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Reject struct instance field access in return-based All rewrites

csharp/​ql/​lib/​Linq/​Helpers.qll:180

Inside a struct instance method, foreach (var i in values) if (i > limit) return false; return true; is reported when limit is an instance field, but values.All(i => i <= limit) fails with CS1673 because the lambda captures the struct's this. The local-variable guard does not exclude this receiver. Reject value-type ThisAccess expressions, including implicit qualifiers, in the new return-based branch and add a negative regression case.

Medium severity Validate final changes against DCA production queries

csharp/​ql/​lib/​Linq/​Helpers.qll:204

This adds a new path to missedAllOpportunity, which changes alerts from the production cs/linq/missed-all query (and its interaction with cs/linq/missed-where). The validation summary reports only focused query tests; please include DCA results for the final change and assess any changed results or unexpected regressions before merging.

1 resolved since last review

Review effort: Balanced (adaptive)

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