Skip to content

SA1513/SA1500 triggered on switch expressions followed by a null-coalescing operator #4332

Closed

Description

@smdn

In StyleCop.Analyzers (reproduced in version 1.3.98-beta), SA1513 (and SA1500) is incorrectly triggered when a switch expression resulting in a nullable value is followed by a null-coalescing operator (??).

Placing the null-coalescing operator on a new line with an empty line before it avoids the warning, but this formatting is undesirable from a readability standpoint. Conversely, removing the empty line triggers SA1513.

A similar issue was addressed in #1613 ("SA1513 and SA1500 for Auto-Implemented Properties"). It is requested that this pattern involving switch expressions and null-coalescing operators be similarly allowed.

Reproduction

Code example:

var s0 = n switch
{
  0 => GetNonNullString(),
  1 => GetNullableString(),
  _ => throw new NotSupportedException(),
} ?? throw new InvalidOperationException("Could not resolve valid string.");
// Triggers SA1513 due to the null-coalescing operator.

var s1 = n switch
{
  0 => GetNonNullString(),
  1 => GetNullableString(),
  _ => throw new NotSupportedException(),
}

?? throw new InvalidOperationException("Could not resolve valid string.");
// Does not trigger SA1513 because of the blank line, but this formatting is hard to accept for readability.
// Removing the blank line causes SA1513 to be triggered.

string GetNonNullString() => throw new NotImplementedException();
string? GetNullableString() => throw new NotImplementedException();

Desired Behavior

It is requested that StyleCop.Analyzers allow switch expressions followed by a null-coalescing operator on the same line or next line without triggering SA1513 or SA1500, similar to how property declarations handle trailing syntax.

Alternatively, consideration is requested for adding a configuration option (such as a setting in layoutRules similar to allowDoWhileOnClosingBrace) to explicitly allow or disallow this behavior.

Activity

  1. AArnott commented on Oct 17, 2026

    @AArnott
    Contributor

    IMO a switch expression's braces should probably not be held to any of the same rules as curly braces used for block statements.

    In this particular case a null-coalescing operator isn't the most compelling use case IMO because it could have been placed in the 1 specific case that needed it instead of applying equally across all 3. But I imagine there are plenty of variants of this. And I totally agree that a blank line requirement or even the 1 new line requirement after the closing brace doesn't make sense.

  2. self-assigned this
    on Oct 17, 2026
  3. moved this from Backlog to In progress in StyleCopAnalyzerson Oct 17, 2026
  4. moved this from In progress to Done in StyleCopAnalyzerson Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions