Skip to content

Commit 8fad37d

Browse files
committed
Update SA1019 for nullable reference types
1 parent ef038c9 commit 8fad37d

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

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

Lines changed: 70 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;
@@ -17,6 +15,76 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
1715

1816
public partial class SA1019CSharp8UnitTests : SA1019CSharp7UnitTests
1917
{
18+
[Fact]
19+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
20+
public async Task TestNullForgivingOperatorWithMemberAccessAsync()
21+
{
22+
var testCode = @"#nullable enable
23+
24+
class TestClass
25+
{
26+
void Test(string? text)
27+
{
28+
_ = text!.Length;
29+
_ = text! {|#0:.|}Length;
30+
_ = text!{|#1:.|} Length;
31+
_ = text! {|#2:?|}.ToString();
32+
_ = text!?{|#3:.|} ToString();
33+
_ = text! {|#4:?|}{|#5:.|} ToString();
34+
}
35+
}
36+
";
37+
38+
var fixedCode = @"#nullable enable
39+
40+
class TestClass
41+
{
42+
void Test(string? text)
43+
{
44+
_ = text!.Length;
45+
_ = text!.Length;
46+
_ = text!.Length;
47+
_ = text!?.ToString();
48+
_ = text!?.ToString();
49+
_ = text!?.ToString();
50+
}
51+
}
52+
";
53+
54+
DiagnosticResult[] expected =
55+
{
56+
Diagnostic(DescriptorNotPreceded).WithArguments(".").WithLocation(0),
57+
Diagnostic(DescriptorNotFollowed).WithArguments(".").WithLocation(1),
58+
Diagnostic(DescriptorNotPreceded).WithArguments("?").WithLocation(2),
59+
Diagnostic(DescriptorNotFollowed).WithArguments(".").WithLocation(3),
60+
Diagnostic(DescriptorNotPreceded).WithArguments("?").WithLocation(4),
61+
Diagnostic(DescriptorNotFollowed).WithArguments(".").WithLocation(5),
62+
};
63+
64+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
65+
}
66+
67+
[Fact]
68+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
69+
public async Task TestNullForgivingOperatorWithMemberAccessNoDiagnosticsAsync()
70+
{
71+
const string testCode = @"#nullable enable
72+
73+
class TestClass
74+
{
75+
void Test(string? text)
76+
{
77+
_ = text!.Length;
78+
_ = text!?.ToString();
79+
_ = (text![0])!.ToString();
80+
_ = (new TestClass()!).ToString();
81+
}
82+
}
83+
";
84+
85+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
86+
}
87+
2088
[Fact]
2189
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
2290
public async Task TestClosingSquareBracketFollowedByExclamationAsync()

documentation/SA1019.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The spacing around a member access symbol is incorrect, within a C# code file.
2121

2222
## Rule description
2323

24-
A violation of this rule occurs when the spacing around a member access symbol is incorrect. A member access symbol should not have whitespace on either side, unless it is the first character on the line.
24+
A violation of this rule occurs when the spacing around a member access symbol is incorrect. A member access symbol should not have whitespace on either side, unless it is the first character on the line. This includes member access that immediately follows the null-forgiving operator (`!`) introduced with nullable reference types in C# 8, such as `value!.Property` or `value!?.Method()`.
2525

2626
## How to fix violations
2727

0 commit comments

Comments
 (0)