Skip to content

Commit d5dbb1f

Browse files
committed
Update SA1131 for nullable reference types
1 parent f7bf65b commit d5dbb1f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/ReadabilityRules/SA1131CSharp8UnitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,37 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.ReadabilityRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1131UseReadableConditions,
13+
StyleCop.Analyzers.ReadabilityRules.SA1131CodeFixProvider>;
714

815
public partial class SA1131CSharp8UnitTests : SA1131CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")]
19+
public async Task TestPatternMatchingNullChecksAsync()
20+
{
21+
var testCode = @"#nullable enable
22+
public class TestClass
23+
{
24+
public bool IsNull(string? value)
25+
{
26+
return value is null;
27+
}
28+
29+
public bool IsNotNull(string? value)
30+
{
31+
return value is { };
32+
}
33+
}
34+
";
35+
36+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
37+
}
1038
}
1139
}

documentation/SA1131.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ For the purposes of this rule, a *literal or constant value* is any of the follo
4747
* `default(T)` (for any type `T`)
4848
* Any expression which evaluates to a constant value at compile time
4949
* A reference to a `static readonly` field, such as `IntPtr.Zero`
50+
* A pattern
5051

5152
A *variable value* is any expression which is not considered a literal or constant value.
5253

0 commit comments

Comments
 (0)