Fix MA0183 false positive and missed diagnostic when named arguments are reordered - #1408
Merged
Merged
Conversation
IInvocationOperation.Arguments is in evaluation order, so reordered named
arguments made the analyzer pick the wrong operand as the format string.
string.Format(arg0: "hello", format: "{0}") was reported as having no
placeholder, and string.Format(arg0: x, provider: c, format: "abc") was
not reported at all.
Locate the format argument through IArgumentOperation.Parameter.Ordinal
instead of the position in the argument list, and detect the IFormatProvider
overload from IMethodSymbol.Parameters, which is always in declaration order.
HasFormattingArguments now compares parameter ordinals as well.
meziantou
enabled auto-merge (squash)
September 6, 2026 04:21
This was referenced Sep 6, 2026
Closed
This was referenced Sep 17, 2026
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 changed
StringFormatShouldBeConstantAnalyzer(MA0183) located the format string by indexingIInvocationOperation.Argumentspositionally. That collection is in evaluation/source order, so when named arguments are written out of parameter order the analyzer picked the wrong operand:The same applied to
StringBuilder.AppendFormatandConsole.Write/Console.WriteLine.The analyzer now works with parameter ordinals instead of argument positions:
GetFormatArgIndex(operation, …)becameGetFormatParameterOrdinal(method, …): theIFormatProvideroverload is detected fromIMethodSymbol.Parameters, which is always in declaration order, and the method returns a parameter ordinal rather than an argument index.GetArgumentForParameterhelper resolves the format argument by matchingIArgumentOperation.Parameter.Ordinal, replacingoperation.Arguments[formatArgumentIndex]and its bounds check.HasFormattingArgumentsiterates every argument and keeps the ones whoseParameter.Ordinalis greater than the format parameter's ordinal, instead of slicing the array fromformatArgumentIndex + 1.Tests
7 cases added to
StringFormatShouldBeConstantAnalyzerTests, covering reordered named arguments forstring.Format(with and withoutprovider:),StringBuilder.AppendFormatandConsole.WriteLine, in both the false-positive and the missed-diagnostic direction. 4 of them failed before the fix (3 false positives, 1 missed diagnostic).Verification
StringFormatShouldBeConstantAnalyzerTests: 62 tests, green on roslyn4.8, roslyn4.14, roslyn5.0, roslyn5.6 and roslyn5.9.dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes — this is a behavior fix that does not alter the documented rule.Note for the reviewer
The report that prompted this also flagged the same positional assumption in
UseDateTimeUnixEpochAnalyzer.ArgumentsEquals. That no longer applies: it already matches onargument.Parameter.Ordinaland its doc comment states it is order-independent, so it is left untouched here.