Do not report MA0028 on StringBuilder.Insert with a Substring argument - #1429
Merged
Merged
Conversation
The Substring branch of IsOptimizable was the only one of the four IInvocationOperation branches missing the `methodName != "Insert"` guard that ToString, string.Format and string.Join all have. As a result, `new StringBuilder().Insert(0, s.Substring(1))` reported "Use Insert(string, int, int) or Insert(ReadOnlySpan<char>) instead of Substring". StringBuilder.Insert(string, int, int) does not exist, and the closest real overload, Insert(int index, string value, int count), repeats the value instead of slicing it, so there is no Insert counterpart to suggest here. The code fixer already declined to fix these: it reads argument 0 as an invocation, but argument 0 of Insert is the int index, so it returned before registering the fix. The user-visible defect was the diagnostic itself naming an API that does not exist.
This was referenced Sep 7, 2026
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.224
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#564
Closed
This was referenced Sep 24, 2026
Open
Open
Open
Open
Open
Open
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.
What
OptimizeStringBuilderUsageAnalyzerreported MA0028 onStringBuilder.Insertwhen the inserted string came fromSubstring:StringBuilder.Insert(string, int, int)does not exist. The closest real overload isInsert(int index, string value, int count), which repeats the value rather than slicing it — so there is noInsertcounterpart to suggest here at all.Why
The
Substringbranch ofIsOptimizablewas the only one of the fourIInvocationOperationbranches missing themethodName != "Insert"guard. The three siblings —ToString,string.Formatandstring.Join— all begin with it, and the comment above them explains exactly whyInserthas to be excluded: those overloads areAppend's, andInserthas no counterpart for some of them.Change
One line, matching the neighbouring branches:
Plus two
Insert_NoDiagnostictheory cases ("abc".Substring(2)and"abc".Substring(0, 1)) with a comment recording whyInsertis excluded.Notes for the reviewer
The code fixer was not throwing on these. Its pattern
Arguments: [{ Value: IInvocationOperation ... }, ..]fails to match, because argument 0 ofInsertis theintindex, so it returned before callingRegisterCodeFixand no lightbulb was ever offered. The user-visible defect was the diagnostic message naming a non-existent API, so the fixer needed no change and is untouched.docs/Rules/MA0028.mdnever described theSubstring/Insertbehaviour, so there was nothing to update there.Verification
dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.