Use TrueFor All instead of All rule + fixer (#115)#124
Open
PendingChanges wants to merge 2 commits into
Open
Conversation
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new performance analyzer/code-fix pair (GCI2334) to detect LINQ Enumerable.All calls when the receiver is a List<T> and suggest replacing them with List<T>.TrueForAll to avoid LINQ overhead.
Changes:
- Registered new rule ID
GCI2334inRule.Ids. - Added
TrueForAllInsteadOfAllanalyzer to report diagnostics forList<T>.All(...)when it binds toSystem.Linq.Enumerable.All. - Added
TrueForAllInsteadOfAllFixerplus a dedicated test suite validating diagnostics and the applied fix.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Creedengo.Tests/Tests/GCI2334.TrueForAllInsteadOfAll.Tests.cs | Adds positive/negative analyzer+fix tests for replacing All with TrueForAll on List<T>. |
| src/Creedengo.Core/Models/Rule.cs | Registers the new rule ID constant for GCI2334. |
| src/Creedengo.Core/Analyzers/GCI2334.TrueForAllInsteadOfAll.Fixer.cs | Introduces the code fix that renames .All(...) to .TrueForAll(...). |
| src/Creedengo.Core/Analyzers/GCI2334.TrueForAllInsteadOfAll.cs | Implements the analyzer identifying Enumerable.All invoked on a List<T> receiver. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+56
to
+60
| if (receiverType is null || !SymbolEqualityComparer.Default.Equals(receiverType.OriginalDefinition, listType)) | ||
| return; | ||
|
|
||
| context.ReportDiagnostic(Diagnostic.Create(Descriptor, memberAccess.Name.GetLocation())); | ||
| } |
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.
GCI2334: Use
List<T>.TrueForAllinstead ofEnumerable.AllSummary
Adds a new performance analyzer GCI2334 that detects calls to the LINQ
Enumerable.Allextension method on aList<T>receiver and suggests replacing them withList<T>.TrueForAll, which is a direct instance method that avoids LINQ overhead (no enumerator allocation, no delegate indirection through the LINQ pipeline).Changes
GCI2334_TrueForAllInsteadOfAll = "GCI2334"in theIdsclass.GCI2334.TrueForAllInsteadOfAll.cs— newDiagnosticAnalyzer. UsesRegisterCompilationStartActionto resolveSystem.Linq.EnumerableandSystem.Collections.Generic.List<T>once per compilation, thenRegisterSyntaxNodeActiononInvocationExpressionto flag calls where the method name isAll, the method is an extension method fromEnumerable, and the static type of the receiver isList<T>. Reports on the method name identifier.GCI2334.TrueForAllInsteadOfAll.Fixer.cs— newCodeFixProvider. Walks up from the reportedIdentifierNameSyntaxto the enclosingInvocationExpressionSyntaxand replaces the method nameAllwithTrueForAll, preserving trivia. Supports batch fix viaWellKnownFixAllProviders.BatchFixer.GCI2334.TrueForAllInsteadOfAll.Tests.cs— 10 tests:.All()on an array, onIEnumerable<T>, onIList<T>, already using.TrueForAll(), and a custom type with its ownAllmethod (not fromEnumerable).List<T>parameter, multiple lists in the same method; each verifies both the diagnostic location and the fixed output.Example
Why only
List<T>?TrueForAllis defined only onList<T>. Receivers typed asIEnumerable<T>,IList<T>, arrays, or any other collection are not flagged — the fix would not be valid for them.