Skip to content

Commit e86607e

Browse files
committed
Fix SA1122 support for constant patterns
Fixes #3028
1 parent 34c2672 commit e86607e

3 files changed

Lines changed: 151 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1122CSharp7UnitTests.cs

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

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

815
public class SA1122CSharp7UnitTests : SA1122UnitTests
916
{
17+
/// <summary>
18+
/// Verifies the analyzer will properly handle an empty string in a case label.
19+
/// </summary>
20+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
21+
[Fact]
22+
[WorkItem(3028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3028")]
23+
public async Task TestEmptyStringInCaseLabelWithConditionAsync()
24+
{
25+
string testCode = @"
26+
public class TestClass
27+
{
28+
public void TestMethod(string condition)
29+
{
30+
switch (""Test string"")
31+
{
32+
case """" when condition == [|""""|]:
33+
break;
34+
case ("""" + ""a"") when condition == [|""""|]:
35+
break;
36+
}
37+
}
38+
}
39+
";
40+
string fixedCode = @"
41+
public class TestClass
42+
{
43+
public void TestMethod(string condition)
44+
{
45+
switch (""Test string"")
46+
{
47+
case """" when condition == string.Empty:
48+
break;
49+
case ("""" + ""a"") when condition == string.Empty:
50+
break;
51+
}
52+
}
53+
}
54+
";
55+
56+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
57+
}
58+
59+
[Fact]
60+
[WorkItem(3028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3028")]
61+
public async Task TestEmptyStringInSimplePatternAsync()
62+
{
63+
string testCode = @"
64+
public class TestClass
65+
{
66+
public bool TestMethod(string condition)
67+
{
68+
return condition is """";
69+
}
70+
}
71+
";
72+
73+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
}
1075
}
1176
}

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,92 @@
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.SA1122UseStringEmptyForEmptyStrings,
13+
StyleCop.Analyzers.ReadabilityRules.SA1122CodeFixProvider>;
714

815
public class SA1122CSharp8UnitTests : SA1122CSharp7UnitTests
916
{
17+
/// <summary>
18+
/// Verifies the analyzer will properly handle an empty string in a switch expression.
19+
/// </summary>
20+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
21+
[Fact]
22+
[WorkItem(3028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3028")]
23+
public async Task TestEmptyStringInSwitchExpressionAsync()
24+
{
25+
string testCode = @"
26+
public class TestClass
27+
{
28+
public void TestMethod(string condition)
29+
{
30+
_ = [|""""|] switch
31+
{
32+
"""" when condition == [|""""|] =>
33+
0,
34+
("""" + ""a"") when condition == [|""""|] =>
35+
0,
36+
};
37+
}
38+
}
39+
";
40+
string fixedCode = @"
41+
public class TestClass
42+
{
43+
public void TestMethod(string condition)
44+
{
45+
_ = string.Empty switch
46+
{
47+
"""" when condition == string.Empty =>
48+
0,
49+
("""" + ""a"") when condition == string.Empty =>
50+
0,
51+
};
52+
}
53+
}
54+
";
55+
56+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
57+
}
58+
59+
[Fact]
60+
[WorkItem(3028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3028")]
61+
public async Task TestEmptyStringInTuplePatternAsync()
62+
{
63+
string testCode = @"
64+
public class TestClass
65+
{
66+
public bool TestMethod((string, string) condition)
67+
{
68+
return condition is ("""", null);
69+
}
70+
}
71+
";
72+
73+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
}
75+
76+
[Fact]
77+
[WorkItem(3028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3028")]
78+
public async Task TestEmptyStringInRecursivePatternAsync()
79+
{
80+
string testCode = @"
81+
using System.Collections.Generic;
82+
public class TestClass
83+
{
84+
public bool TestMethod(KeyValuePair<string, string> condition)
85+
{
86+
return condition is { Key: """" };
87+
}
88+
}
89+
";
90+
91+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
92+
}
1093
}
1194
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1122UseStringEmptyForEmptyStrings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
12+
using StyleCop.Analyzers.Lightup;
1213

1314
/// <summary>
1415
/// The C# code includes an empty string, written as <c>""</c>.
@@ -81,7 +82,8 @@ private static bool HasToBeConstant(LiteralExpressionSyntax literalExpression)
8182
ExpressionSyntax outermostExpression = FindOutermostExpression(literalExpression);
8283

8384
if (outermostExpression.Parent.IsKind(SyntaxKind.AttributeArgument)
84-
|| outermostExpression.Parent.IsKind(SyntaxKind.CaseSwitchLabel))
85+
|| outermostExpression.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
86+
|| outermostExpression.Parent.IsKind(SyntaxKindEx.ConstantPattern))
8587
{
8688
return true;
8789
}

0 commit comments

Comments
 (0)