Skip to content

Commit 0fc50cc

Browse files
committed
Update SA1309/SX1309 for pattern matching
1 parent 5131fde commit 0fc50cc

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SA1309CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.NamingRules.SA1309FieldNamesMustNotBeginWithUnderscore,
14+
StyleCop.Analyzers.NamingRules.SA1309CodeFixProvider>;
715

816
public partial class SA1309CSharp8UnitTests : SA1309CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
20+
public async Task TestDiscardDesignationInPropertyPatternAsync()
21+
{
22+
var testCode = @"
23+
public class TestClass
24+
{
25+
public int Test(SomeType value)
26+
{
27+
if (value is { Number: _ })
28+
{
29+
return 1;
30+
}
31+
32+
return 0;
33+
}
34+
}
35+
36+
public class SomeType
37+
{
38+
public int Number { get; set; }
39+
}
40+
";
41+
42+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
43+
}
44+
45+
[Fact]
46+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
47+
public async Task TestDiscardDesignationInSwitchExpressionAsync()
48+
{
49+
var testCode = @"
50+
public class TestClass
51+
{
52+
public int Test(SomeType value) =>
53+
value switch
54+
{
55+
{ Number: _ } => 1,
56+
_ => 0,
57+
};
58+
}
59+
60+
public class SomeType
61+
{
62+
public int Number { get; set; }
63+
}
64+
";
65+
66+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
67+
}
1068
}
1169
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SX1309CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.NamingRules.SX1309FieldNamesMustBeginWithUnderscore,
14+
StyleCop.Analyzers.NamingRules.SX1309CodeFixProvider>;
715

816
public partial class SX1309CSharp8UnitTests : SX1309CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
20+
public async Task TestDiscardDesignationInPropertyPatternAsync()
21+
{
22+
var testCode = @"
23+
public class TestClass
24+
{
25+
public int Test(SomeType value)
26+
{
27+
if (value is { Number: _ })
28+
{
29+
return 1;
30+
}
31+
32+
return 0;
33+
}
34+
}
35+
36+
public class SomeType
37+
{
38+
public int Number { get; set; }
39+
}
40+
";
41+
42+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
43+
}
44+
45+
[Fact]
46+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
47+
public async Task TestDiscardDesignationInSwitchExpressionAsync()
48+
{
49+
var testCode = @"
50+
public class TestClass
51+
{
52+
public int Test(SomeType value) =>
53+
value switch
54+
{
55+
{ Number: _ } => 1,
56+
_ => 0,
57+
};
58+
}
59+
60+
public class SomeType
61+
{
62+
public int Number { get; set; }
63+
}
64+
";
65+
66+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
67+
}
1068
}
1169
}

documentation/SA1309.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ By default, StyleCop disallows the use of underscores, *m_*, etc., to mark local
2727

2828
If the field or variable name is intended to match the name of an item associated with Win32 or COM, and thus needs to begin with an underscore, place the field or variable within a special *NativeMethods* class. A NativeMethods class is any class which contains a name ending in NativeMethods, and is intended as a placeholder for Win32 or COM wrappers. StyleCop will ignore this violation if the item is placed within a NativeMethods class.
2929

30+
Discard designations introduced by pattern matching (for example, `_` in C# 8 property or positional patterns) are not subject to this rule.
31+
3032
## How to fix violations
3133

3234
To fix a violation of this rule, remove the underscore from the beginning of the field name, or place the item within a NativeMethods class if appropriate.

documentation/SX1309.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ This rule only checks the name of private instance fields.
3636

3737
The fix an instance of this violation, add an underscore (`_`) prefix to the name of the field.
3838

39+
Discard designations introduced by pattern matching (for example, `_` in C# 8 property or positional patterns) are not considered fields and are ignored by this rule.
40+
3941
## How to suppress violations
4042

4143
```csharp

0 commit comments

Comments
 (0)