Fix MA0075 false positive on conditional and other compound expressions - #1295
Merged
Conversation
GetCultureSensitivity returned CultureSensitive for any operation kind it
did not explicitly handle. Since MA0075 started analyzing every string
concatenation operand, conditional expressions, coalesce expressions,
switch expressions and await expressions were all reported even when
their type is culture-insensitive:
_ = "test" + (date.HasValue ? date.Value.ToString(CultureInfo.InvariantCulture) : string.Empty);
The fallback now uses the static type of the value, which is what
determines the formatting behavior and is already how interpolated string
parts are analyzed.
Fixes #1294
meziantou
enabled auto-merge (squash)
August 18, 2026 12:59
This was referenced Aug 18, 2026
Closed
Closed
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.103 to 3.0.165
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#93
Closed
Closed
Closed
This was referenced Aug 27, 2026
Bump Meziantou.Analyzer from 3.0.139 to 3.0.189
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#556
Open
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.189
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#542
Open
Open
Open
Open
Open
Open
Open
Open
Open
Bump Meziantou.Analyzer from 3.0.103 to 3.0.190
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#99
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 #1294
Problem
This was reported as MA0075 even though the operand is a
string.Cause
Before #1273, MA0075 only inspected concatenation operands that were an implicit conversion to
object, and ignored everything else. #1273 changed it to run every operand throughCultureSensitiveFormattingContext.GetCultureSensitivity, whose fallback for an unrecognized operation kind was:IConditionalOperationhas no case in that method, so the ternary fell through to "culture-sensitive" regardless of its type. The same false positive applied to coalesce expressions (??), switch expressions andawaitexpressions.Fix
The fallback now uses the static type of the value, which is what determines the formatting behavior and is already how interpolated string parts are analyzed:
"test" + (cond ? s : "")is now clean, while"test" + (cond ? a : b)withdoubleoperands is still reported — same for??,switchandawait.Notes for reviewers
nulltype (invalid code, method group) now maps toMaybeCultureSensitiveOpaqueRuntimeTypeinstead ofCultureSensitive, so it is no longer reported by default. That matches howobject-typed values are already treated.IInvocationOperationis handled earlier in the method and is unaffected, so MA0011 (UseIFormatProviderAnalyzer) keeps its current behavior for invocations.Tests
8 tests added to
DoNotUseImplicitCultureSensitiveToStringAnalyzerTests(a positive and a negative case for each expression kind, including the exact snippet from the issue).roslyn4.8(3520),roslyn4.14(3554),roslyn5.0(3593),roslyn5.6(3604): all passeddotnet run --project src/DocumentationGenerator: exit 0, no markdown changes