Add CA1881 analyzer: prefer MemoryExtensions.Split over counted string.Split - #1
Open
Mrnikbobjeff wants to merge 1 commit into
Open
Mrnikbobjeff wants to merge 1 commit into
Mrnikbobjeff wants to merge 1 commit into
Conversation
…g.Split Implements dotnet#85487 as approved in the 2023-06-01 API review: report string.Split calls that pass a count, and suggest the span-based MemoryExtensions.Split/SplitAny overloads (.NET 8) with a Span<Range> destination. Category Performance, severity Info, no fixer. CA rules now live in dotnet/sdk (src/Microsoft.CodeAnalysis.NetAnalyzers), so the analyzer and its tests are added at their upstream paths, and upstream/0001-Add-CA1881.patch carries the full change (resx, xlf, release tracking, ID range, generated md/sarif) for `git am` on dotnet/sdk 3b1d59f. docs/analyzers/CA1881 has the review notes, design, verification details and open items; the video transcript is still pending because YouTube was not reachable from the build environment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AdmaanBw8ruzpHQFgrisSa
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.
Implements the analyzer from dotnet/runtime#85487, following what the 2023-06-01 API review approved.
What it does
CA1881 reports
string.Splitcalls that pass a count and suggests the .NET 8 span-basedMemoryExtensions.Split/SplitAnyoverloads with aSpan<Range>destination. The destination's length stands in for the count, so the result is the same (the last range holds the remainder). Category Performance, severity Info, no code fix, as the review decided.Split(char, int[, options])Split(Span<Range>, char, options)Split(string, int[, options])Split(Span<Range>, ReadOnlySpan<char>, options)Split(char[], int[, options])SplitAny(Span<Range>, ReadOnlySpan<char>, options)Split(string[], int, options)SplitAny(Span<Range>, ReadOnlySpan<string>, options)The rule:
Where the code lives
CA rules now live in dotnet/sdk (
src/Microsoft.CodeAnalysis.NetAnalyzers), so this is written against that tree:src/Microsoft.CodeAnalysis.NetAnalyzers/...contains the new analyzer and test files at their dotnet/sdk paths. They aren't part of this repo's build.upstream/0001-Add-CA1881.patchis the complete dotnet/sdk change: resx, 13 xlf files,AnalyzerReleases.Unshipped.md, ID range,WellKnownTypeNames, and the generated md/sarif. It applies withgit amon dotnet/sdk3b1d59f.docs/analyzers/CA1881/has the design notes, review notes, verification details, open items, and a placeholder for the review video transcript. YouTube wasn't reachable from the build environment.Testing
The real dotnet/sdk build couldn't run because the Arcade/dnceng feeds were blocked. I used a throwaway harness instead, built from upstream's own
Analyzer.Utilitiessources andCSharpCodeFixVerifier, against Roslyn 4.14.0 with warnings as errors and release-tracking analyzers on.string.Split.git amof the patch on a clean dotnet/sdk3b1d59fgives a tree identical to the one tested.Before upstreaming
NextDiagnosticId.cs Performance.ca1881.mdpage in dotnet/docs is required within a week of merge.🤖 Generated with Claude Code
https://claude.ai/code/session_01AdmaanBw8ruzpHQFgrisSa
Generated by Claude Code