Skip to content

Fix false positive in MissedSelectOpportunity when foreach body uses await#21708

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-missed-opportunity-to-use-select
Draft

Fix false positive in MissedSelectOpportunity when foreach body uses await#21708
Copilot wants to merge 2 commits intomainfrom
copilot/fix-missed-opportunity-to-use-select

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

The cs/linq/missed-select query incorrectly flagged foreach loops whose first statement used await — loops that cannot be refactored to .Select(...) since LINQ's Select operates on IEnumerable<T> and does not support async lambdas.

// Previously flagged — cannot use Select because of await
foreach (var locator in contactSelectors.Select(s => Page.Locator(s)))
{
    var elements = await locator.CountAsync(); // ← await here blocks Select refactor
    if (elements > 0) { ... }
}

Changes

  • csharp/ql/lib/Linq/Helpers.qll: Added exclusion to missedSelectOpportunity — the predicate no longer holds when the first statement's initializer contains an AwaitExpr. Updated doc comment accordingly.

  • csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/: New test covering:

    • BAD: plain variable mapping (should be flagged)
    • GOOD: variable mapping whose initializer uses await (should not be flagged)

Copilot AI changed the title [WIP] Fix false positive for missed opportunity to use Select with C# Fix false positive in MissedSelectOpportunity when foreach body uses await Apr 14, 2026
Copilot AI requested a review from hvitved April 14, 2026 08:19
@github-actions github-actions bot added the C# label Apr 14, 2026
@@ -0,0 +1 @@
Linq/MissedSelectOpportunity.ql
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 : Please address this.

@@ -0,0 +1 @@
Linq/MissedSelectOpportunity.ql
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.

Should use inline test expectations, i.e.

postprocess:
 - utils/test/InlineExpectationsTestQuery.ql

This also means adding // $ Alert marker comment in the test file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: Missed opportunity to use Select with C#

3 participants