Skip to content

Commit 241452a

Browse files
committed
Update SA1011 for nullable reference types
1 parent d5dbb1f commit 241452a

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
75
{
86
using System.Threading;
@@ -16,6 +14,65 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
1614

1715
public partial class SA1011CSharp8UnitTests : SA1011CSharp7UnitTests
1816
{
17+
[Fact]
18+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
19+
public async Task VerifyNullableArrayAnnotationsAsync()
20+
{
21+
var testCode = @"#nullable enable
22+
namespace TestNamespace
23+
{
24+
public class TestClass
25+
{
26+
private string[]? field1;
27+
private string[]?[]? field2;
28+
private string?[][]? field3;
29+
30+
public string[]? Property1 => field1;
31+
32+
public string[]?[]? Property2
33+
{
34+
get
35+
{
36+
return new string[]?[] { field1, field2?[0] };
37+
}
38+
}
39+
40+
public string?[] Method(string?[]? values, string[]?[]? other)
41+
{
42+
return values ?? new string?[] { null, other?[0]?[0] };
43+
}
44+
}
45+
}
46+
";
47+
48+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
49+
}
50+
51+
[Fact]
52+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
53+
public async Task VerifyNullForgivingAfterElementAccessAndArrayCreationAsync()
54+
{
55+
var testCode = @"#nullable enable
56+
namespace TestNamespace
57+
{
58+
public class TestClass
59+
{
60+
public string GetValue(string[]? values)
61+
{
62+
return values![0]!;
63+
}
64+
65+
public int GetLength(string?[] items)
66+
{
67+
return new string?[0]!.Length + items[0]! .Length;
68+
}
69+
}
70+
}
71+
";
72+
73+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
}
75+
1976
/// <summary>
2077
/// Verify that declaring a null reference type works for arrays.
2178
/// </summary>

documentation/SA1011.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ A closing square bracket should be followed by whitespace unless:
3131
* It is followed by a comma or semicolon
3232
* It is followed by a string interpolation alignment component. For example: `$"{x[i]:C}"`
3333
* It is followed by a string interpolation formatting component. For example: `$"{x[i],3}"`
34-
* It is followed by certain types of operator symbols.
34+
* It is followed by certain types of operator symbols
35+
* It is followed by a nullable reference type annotation for arrays (for example, `string[]? items`)
36+
* It is followed by the null-forgiving operator applied to an array or element access (for example, `values[0]!`)
3537

3638
## How to fix violations
3739

0 commit comments

Comments
 (0)