Skip to content

Commit 7f1df1f

Browse files
committed
Allow null forgiving operator at end of line when followed by colon in conditional expression
1 parent 0be0669 commit 7f1df1f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,32 @@ public void TestOnMethod()
359359
}
360360
}
361361
}
362+
";
363+
364+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
365+
}
366+
367+
[Fact]
368+
public async Task TestNullForgivingOperatorAtEndOfLineBeforeConditionalExpressionColonAsync()
369+
{
370+
var testCode = @"
371+
namespace TestNamespace
372+
{
373+
public class TestClass
374+
{
375+
public void TestMethod(bool condition, string? canBeNullExpression)
376+
{
377+
var result1 = condition ? canBeNullExpression!
378+
: ""default"";
379+
380+
var result2 = condition
381+
? canBeNullExpression!
382+
: ""default"";
383+
384+
var result3 = condition ? canBeNullExpression! : ""default"";
385+
}
386+
}
387+
}
362388
";
363389

364390
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ private static void HandlePostfixUnaryExpression(SyntaxNodeAnalysisContext conte
393393
break;
394394
}
395395

396+
// Determine if the operator is allowed at the end of a line
396397
bool allowEndOfLine;
397398
if (followingToken.IsKind(SyntaxKind.CloseBraceToken))
398399
{
@@ -405,6 +406,13 @@ private static void HandlePostfixUnaryExpression(SyntaxNodeAnalysisContext conte
405406
// Allow null forgiving operator at end of line when followed by member access on the next line
406407
allowEndOfLine = true;
407408
}
409+
else if (followingToken.IsKind(SyntaxKind.ColonToken) &&
410+
followingToken.Parent is ConditionalExpressionSyntax &&
411+
followingToken.IsFirstInLine())
412+
{
413+
// Allow null forgiving operator at end of line when followed by colon in conditional expression on the next line
414+
allowEndOfLine = true;
415+
}
408416
else
409417
{
410418
allowEndOfLine = false;

0 commit comments

Comments
 (0)