Fix MA0075 false positive on FormattableString.Invariant - #1322
Merged
Conversation
GetCultureSensitivity classifies any invocation that takes a FormattableString argument with the culture sensitivity of that argument, before checking anything else. FormattableString.Invariant formats its argument with CultureInfo.InvariantCulture, so the result is culture insensitive, but the invocation was reported as culture sensitive when used as an operand of a string concatenation. Add FormattableString.Invariant to the excluded methods, which are checked before the interpolated string argument. FormattableString.CurrentCulture is on the same type but is genuinely culture sensitive, so it keeps being reported. Fixes #1321
meziantou
enabled auto-merge (squash)
August 26, 2026 14:33
This was referenced Aug 26, 2026
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.103 to 3.0.182
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#98
Closed
Closed
Closed
Closed
This was referenced Aug 31, 2026
Open
Open
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.
Fixes #1321
Problem
Since 3.0.150 (#1273), MA0075 reports
FormattableString.Invariant($"...")when the result is used directly as an operand of a string concatenation:Storing the result in a local first was already accepted, so the inline form should be too.
Cause
CultureSensitiveFormattingContext.GetCultureSensitivitycallsGetInterpolatedStringArgumentCultureSensitivityfor invocations, which treats any invocation with aFormattableString(or interpolated string handler) argument as producing the culture sensitivity of that argument, and returns before anything else is considered. ForInvariant($"abc{1:N0}")the argument is culture sensitive, so the whole invocation was classified as culture sensitive — even thoughInvariantexists precisely to format that argument withCultureInfo.InvariantCulture.Fix
Add
System.FormattableString.Invariant(System.FormattableString)toCreateExcludedMethods, which is consulted before the interpolated string argument path.I used the excluded-methods list rather than skipping every method whose containing type is
System.FormattableString, becauseFormattableString.CurrentCulturelives on the same type and is genuinely culture sensitive — it must keep being reported.FormattableString.ToString(IFormatProvider)andstring.Create(IFormatProvider, ...), also mentioned in the issue, were already covered by the existingHasArgumentOfType(FormatProviderSymbol)check, so they needed no change.Tests
FormattableString_Invariant_StringConcatcovers the four concatenation forms from the issue (left operand, right operand, both operands, parenthesized).FormattableString_CurrentCulture_StringConcatlocks in thatFormattableString.CurrentCultureis still reported.Verification
dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.