Skip to content

Commit 333386c

Browse files
committed
Update SA1003 for null-forgiving operator at end of line when followed by delegate invocation
1 parent e620812 commit 333386c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,38 @@ public void TestMethod(bool condition, string? canBeNullExpression)
385385
}
386386
}
387387
}
388+
";
389+
390+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
391+
}
392+
393+
[Fact]
394+
public async Task TestNullForgivingOperatorWithDelegateInvocationAsync()
395+
{
396+
var testCode = @"
397+
namespace TestNamespace
398+
{
399+
using System;
400+
401+
public class TestClass
402+
{
403+
public void TestMethod()
404+
{
405+
Func<string>? delegateThatMightReturnNull = null;
406+
407+
// Inline invocation
408+
var result1 = delegateThatMightReturnNull!();
409+
410+
// Invocation with arguments
411+
Func<int, string>? delegateWithArgs = null;
412+
var result2 = delegateWithArgs!(42);
413+
414+
// Multi-line invocation
415+
var result3 = delegateThatMightReturnNull!
416+
();
417+
}
418+
}
419+
}
388420
";
389421

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ private static void HandlePostfixUnaryExpression(SyntaxNodeAnalysisContext conte
373373
case SyntaxKind.CommaToken:
374374
case SyntaxKind.DotToken:
375375
case SyntaxKind.MinusGreaterThanToken:
376+
case SyntaxKind.OpenParenToken:
376377
mustHaveTrailingWhitespace = false;
377378
break;
378379

@@ -413,6 +414,11 @@ followingToken.Parent is ConditionalExpressionSyntax &&
413414
// Allow null forgiving operator at end of line when followed by colon in conditional expression on the next line
414415
allowEndOfLine = true;
415416
}
417+
else if (followingToken.IsKind(SyntaxKind.OpenParenToken) && followingToken.IsFirstInLine())
418+
{
419+
// Allow null forgiving operator at end of line when followed by delegate invocation on the next line
420+
allowEndOfLine = true;
421+
}
416422
else
417423
{
418424
allowEndOfLine = false;

0 commit comments

Comments
 (0)