Skip to content

Fix MA0075 false positive on conditional and other compound expressions - #1295

Merged
meziantou merged 1 commit into
mainfrom
feature/meziantou-analyzer-1294-913de8
Aug 18, 2026
Merged

Fix MA0075 false positive on conditional and other compound expressions#1295
meziantou merged 1 commit into
mainfrom
feature/meziantou-analyzer-1294-913de8

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Fixes #1294

Problem

using System.Globalization;

DateTime? date = null;
_ = "test" + (date.HasValue ? date.Value.ToString(CultureInfo.InvariantCulture) : string.Empty);

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 through CultureSensitiveFormattingContext.GetCultureSensitivity, whose fallback for an unrecognized operation kind was:

// Unknown operation
return CultureSensitivity.CultureSensitive;

IConditionalOperation has 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 and await expressions.

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:

// Unknown operation (conditional expression, coalesce expression, switch expression, await expression, ...).
// The formatting depends on the type of the value, so use it to determine the culture sensitivity.
return GetCultureSensitivity(operation.Type, format: null, instance: operation, options);

"test" + (cond ? s : "") is now clean, while "test" + (cond ? a : b) with double operands is still reported — same for ??, switch and await.

Notes for reviewers

  • An operation with a null type (invalid code, method group) now maps to MaybeCultureSensitiveOpaqueRuntimeType instead of CultureSensitive, so it is no longer reported by default. That matches how object-typed values are already treated.
  • IInvocationOperation is 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).

  • Full suite on the default Roslyn version: 3650 passed
  • roslyn4.8 (3520), roslyn4.14 (3554), roslyn5.0 (3593), roslyn5.6 (3604): all passed
  • dotnet run --project src/DocumentationGenerator: exit 0, no markdown changes

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
meziantou enabled auto-merge (squash) August 18, 2026 12:59
@meziantou
meziantou merged commit b301eec into main Aug 18, 2026
12 checks passed
@meziantou
meziantou deleted the feature/meziantou-analyzer-1294-913de8 branch August 18, 2026 13:00
This was referenced Aug 18, 2026
This was referenced Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MA0075 question

1 participant