-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix false positive in MissedSelectOpportunity when foreach body uses await
#21708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| class MissedSelectOpportunity | ||
| { | ||
| public void M1(List<int> lst) | ||
| { | ||
| // BAD: Can be replaced with lst.Select(i => i * i) | ||
| foreach (int i in lst) | ||
| { | ||
| int j = i * i; | ||
| Console.WriteLine(j); | ||
| } | ||
| } | ||
|
|
||
| public async Task M2(IEnumerable<ICounter> counters) | ||
| { | ||
| // GOOD: Cannot use Select because the initializer contains an await expression | ||
| foreach (var counter in counters) | ||
| { | ||
| var count = await counter.CountAsync(); | ||
| Console.WriteLine(count); | ||
| } | ||
| } | ||
|
|
||
| public interface ICounter | ||
| { | ||
| Task<int> CountAsync(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | MissedSelectOpportunity.cs:11:9:15:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider mapping the sequence explicitly using '.Select(...)'. | MissedSelectOpportunity.cs:13:13:13:26 | ... ...; | maps its iteration variable to another variable | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Linq/MissedSelectOpportunity.ql | ||
Check warningCode scanning / CodeQL Query test without inline test expectations Warning test
Query test does not use inline test expectations.
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use inline test expectations, i.e. This also means adding |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj |
Uh oh!
There was an error while loading. Please reload this page.