Fix false positive in MissedSelectOpportunity when foreach body uses await#21708
Open
Fix false positive in MissedSelectOpportunity when foreach body uses await#21708
MissedSelectOpportunity when foreach body uses await#21708Conversation
Agent-Logs-Url: https://github.com/github/codeql/sessions/3e8f4320-2bf4-45f5-b9ea-dad41d522d84 Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix false positive for missed opportunity to use Select with C#
Fix false positive in Apr 14, 2026
MissedSelectOpportunity when foreach body uses await
csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.qlref
Fixed
Show fixed
Hide fixed
Copilot stopped work on behalf of
hvitved due to an error
April 14, 2026 09:30
hvitved
requested changes
Apr 14, 2026
| @@ -0,0 +1 @@ | |||
| Linq/MissedSelectOpportunity.ql | |||
Contributor
There was a problem hiding this comment.
Should use inline test expectations, i.e.
postprocess:
- utils/test/InlineExpectationsTestQuery.ql
This also means adding // $ Alert marker comment in the test file.
hvitved
approved these changes
Apr 14, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adjusts the cs/linq/missed-select query helper logic to avoid flagging foreach loops whose first statement’s initializer contains an await, since these loops can’t be straightforwardly refactored into a synchronous .Select(...) mapping.
Changes:
- Updated
missedSelectOpportunityto exclude cases where the first statement’s initializer contains anAwaitExpr. - Added a new query-test folder for
Linq/MissedSelectOpportunity.qlcovering one flagged (“BAD”) and one non-flagged (“GOOD”) scenario. - Added test harness files (
options,.qlref,.expected) for the new test.
Show a summary per file
| File | Description |
|---|---|
| csharp/ql/lib/Linq/Helpers.qll | Refines missedSelectOpportunity to ignore await-based initializers; updates the predicate’s doc comment. |
| csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/options | Adds extractor options/stub project loading for the new test. |
| csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.qlref | Adds a .qlref pointing at Linq/MissedSelectOpportunity.ql with inline-expectations postprocessing. |
| csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.expected | Captures the expected single result (only the “BAD” case). |
| csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.cs | Adds “BAD” and “GOOD” C# examples to validate the updated behavior. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 2
csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.cs
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
cs/linq/missed-selectquery incorrectly flaggedforeachloops whose first statement usedawait— loops that cannot be refactored to.Select(...)since LINQ'sSelectoperates onIEnumerable<T>and does not support async lambdas.Changes
csharp/ql/lib/Linq/Helpers.qll: Added exclusion tomissedSelectOpportunity— the predicate no longer holds when the first statement's initializer contains anAwaitExpr. Updated doc comment accordingly.csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/: New test covering:await(should not be flagged)