Skip to content

Commit 13cdbb9

Browse files
authored
Merge pull request #3177 from vweijsters/fix-3172
Exclude null-forgiving operator for SA1013
2 parents 2d87f14 + 6482471 commit 13cdbb9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.Testing;
11+
812
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
13+
914
using Xunit;
15+
1016
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1117
StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly,
1218
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
@@ -62,5 +68,30 @@ public void TestMethod(object value)
6268
var expected = Diagnostic().WithSpan(14, 37, 14, 38).WithArguments(string.Empty, "followed");
6369
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
6470
}
71+
72+
/// <summary>
73+
/// Validates that a closing brace followed by a null-forgiving operator does not require a space.
74+
/// </summary>
75+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
76+
[Fact]
77+
[WorkItem(3172, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3172")]
78+
public async Task TestCloseBraceWithNullForgivingOperatorAsync()
79+
{
80+
const string testCode = @"#nullable enable
81+
public class Foo
82+
{
83+
public void TestMethod()
84+
{
85+
var test = new[]
86+
{
87+
new { Value = ""a"" }!,
88+
new { Value = ""b"" } !,
89+
};
90+
}
91+
}
92+
";
93+
94+
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
95+
}
6596
}
6697
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1013ClosingBracesMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
102102
|| nextToken.IsKind(SyntaxKind.DotToken)
103103
|| (nextToken.IsKind(SyntaxKind.QuestionToken) && nextToken.GetNextToken(includeZeroWidth: true).IsKind(SyntaxKind.DotToken))
104104
|| nextToken.IsKind(SyntaxKind.CloseBracketToken)
105-
|| (nextToken.IsKind(SyntaxKind.ColonToken) && nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel));
105+
|| (nextToken.IsKind(SyntaxKind.ColonToken) && nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel))
106+
|| (nextToken.IsKind(SyntaxKind.ExclamationToken) && nextToken.Parent.IsKind(SyntaxKindEx.SuppressNullableWarningExpression));
106107
}
107108
else
108109
{

0 commit comments

Comments
 (0)