Skip to content

Commit d99b446

Browse files
committed
Update SA1013 for nullable reference types
1 parent 241452a commit d99b446

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,54 @@ public void TestMethod()
8888
};
8989
}
9090
}
91+
";
92+
93+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
94+
}
95+
96+
/// <summary>
97+
/// Validates that a closing brace followed by a null-forgiving operator is allowed after an object initializer.
98+
/// </summary>
99+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
100+
[Fact]
101+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
102+
public async Task TestObjectInitializerWithNullForgivingOperatorAsync()
103+
{
104+
const string testCode = @"#nullable enable
105+
public class Foo
106+
{
107+
public Foo? Value { get; set; }
108+
109+
public void TestMethod()
110+
{
111+
var item = new Foo { Value = null }!;
112+
var other = new Foo { }!;
113+
}
114+
}
115+
";
116+
117+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
118+
}
119+
120+
/// <summary>
121+
/// Validates that a closing brace followed by a null-forgiving operator is allowed after a collection initializer.
122+
/// </summary>
123+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
124+
[Fact]
125+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
126+
public async Task TestCollectionInitializerWithNullForgivingOperatorAsync()
127+
{
128+
const string testCode = @"#nullable enable
129+
using System.Collections.Generic;
130+
131+
public class Foo
132+
{
133+
public void TestMethod()
134+
{
135+
var numbers = new List<int> { 1, 2, 3 }!;
136+
var nested = new List<List<int>> { new List<int> { 1, 2 }!, new List<int> { 3 }! };
137+
}
138+
}
91139
";
92140

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

documentation/SA1013.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ A closing brace within a C# element is not spaced correctly.
2323

2424
A violation of this rule occurs when the spacing around a closing brace is not correct.
2525

26-
A closing brace should always be followed by a single space, unless it is the last character on the line, or unless it is followed by a closing parenthesis, a comma, or a semicolon.
26+
A closing brace should always be followed by a single space, unless it is the last character on the line, or unless it is followed by a closing parenthesis, a comma, or a semicolon. The null-forgiving operator (`!`) may also appear immediately after a closing brace without a space (for example, `new Foo { Value = null }!`).
2727

2828
A closing brace should always be preceded by a single space, unless it is the first character on the line.
2929

0 commit comments

Comments
 (0)