-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add CA1876 analyzer to detect misuse of AsParallel() in foreach loops #51287
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
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…ethods Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot, it looks like you checked in edits to hundreds of unrelated files. Did you accidentally run Also, there's a build failure to address: |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Reverted the 479 unintended formatting changes from dotnet format and regenerated the documentation files using msbuild /t:pack. Commit: 7ba18ec |
...rs.UnitTests/Microsoft.NetCore.Analyzers/Performance/DoNotUseAsParallelInForEachLoopTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements analyzer CA1876 to detect incorrect usage of AsParallel() in foreach loops, where it has no effect on parallelization. The analyzer identifies when AsParallel() is called directly in a foreach statement's collection expression, as the foreach statement itself always iterates serially regardless of the collection type.
Key Changes:
- Added new performance analyzer CA1876 with comprehensive test coverage
- Extended diagnostic ID range and added necessary localization resources
- Regenerated auto-generated documentation files
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| DoNotUseAsParallelInForEachLoop.cs | Core analyzer implementation detecting AsParallel() misuse in foreach loops |
| DoNotUseAsParallelInForEachLoopTests.cs | 11 test cases covering various scenarios (basic usage, LINQ chains, valid patterns) |
| WellKnownTypeNames.cs | Added SystemLinqParallelEnumerable type constant |
| DiagnosticCategoryAndIdRanges.txt | Extended Performance category range to include CA1876 |
| MicrosoftNetCoreAnalyzersResources.resx | Added English resource strings for title, message, and description |
| MicrosoftNetCoreAnalyzersResources.*.xlf | Added localization placeholders for 13 languages |
| AnalyzerReleases.Unshipped.md | Documented new CA1876 analyzer |
| Microsoft.CodeAnalysis.NetAnalyzers.sarif | Auto-generated SARIF metadata for CA1876 |
| Microsoft.CodeAnalysis.NetAnalyzers.md | Auto-generated documentation for CA1876 |
…maintainability Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
tarekgh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Plan for AsParallel() Misuse Analyzer
Creating a new analyzer to detect incorrect usage of
AsParallel()in LINQ queries.Problem
AsParallel()at the end of a LINQ query directly in aforeachis a no-opforeach (var item in src.Select(...).Where(...).AsParallel()) { }does NOT parallelize the foreachforeach (var item in src.AsParallel()) { }also does NOT parallelize the foreachImplementation Complete ✅
foreachoverAsParallel()resultChanges Made
New Analyzer:
DoNotUseAsParallelInForEachLoop.csAsParallel()is called directly in a foreach loopSymbolEqualityComparer.Defaultfor proper symbol comparisonResource Strings: Added to
MicrosoftNetCoreAnalyzersResources.resxConfiguration Files Updated:
WellKnownTypeNames.cs: Added SystemLinqParallelEnumerableDiagnosticCategoryAndIdRanges.txt: Extended Performance range to CA1876AnalyzerReleases.Unshipped.md: Added CA1876 entryAuto-generated Documentation:
.mdand.sariffiles usingmsbuild /t:packTest Coverage: 11 comprehensive tests covering:
All tests passing ✓
Build succeeds without warnings ✓
Original prompt
Fixes dotnet/runtime#33769
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.