You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SA1513/SA1500 triggered on switch expressions followed by a null-coalescing operator #4332
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:
vars0=nswitch{0=>GetNonNullString(),1=>GetNullableString(),
_ =>thrownewNotSupportedException(),}??thrownewInvalidOperationException("Could not resolve valid string.");// Triggers SA1513 due to the null-coalescing operator.vars1=nswitch{0=>GetNonNullString(),1=>GetNullableString(),
_ =>thrownewNotSupportedException(),}??thrownewInvalidOperationException("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.stringGetNonNullString()=>thrownewNotImplementedException();string?GetNullableString()=>thrownewNotImplementedException();
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.
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.
Activity
AArnott commentedon Oct 17, 2026
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.