Skip to content

Commit 659fd5b

Browse files
committed
Update SA1131 to support default literal expressions
Fixes #2675
1 parent 64016d7 commit 659fd5b

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

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

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

44
namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
610
using StyleCop.Analyzers.Test.ReadabilityRules;
11+
using TestHelper;
12+
using Xunit;
713

814
public class SA1131CSharp7UnitTests : SA1131UnitTests
915
{
16+
[Theory]
17+
[InlineData("==", "==")]
18+
[InlineData("!=", "!=")]
19+
[WorkItem(2675, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2675")]
20+
public async Task TestDefaultLiteralStructComparismOutsideIfAsync(string oldOperator, string newOperator)
21+
{
22+
var testCode = $@"
23+
using System;
24+
public class TypeName
25+
{{
26+
public void Test()
27+
{{
28+
TestStruct i = default;
29+
bool b = default {oldOperator} i;
30+
}}
31+
}}
32+
33+
struct TestStruct
34+
{{
35+
public static bool operator == (TestStruct a, TestStruct b) {{ return true; }}
36+
public static bool operator != (TestStruct a, TestStruct b) {{ return false; }}
37+
}}
38+
";
39+
var fixedCode = $@"
40+
using System;
41+
public class TypeName
42+
{{
43+
public void Test()
44+
{{
45+
TestStruct i = default;
46+
bool b = i {newOperator} default;
47+
}}
48+
}}
49+
50+
struct TestStruct
51+
{{
52+
public static bool operator == (TestStruct a, TestStruct b) {{ return true; }}
53+
public static bool operator != (TestStruct a, TestStruct b) {{ return false; }}
54+
}}
55+
";
56+
DiagnosticResult[] expected =
57+
{
58+
this.CSharpDiagnostic().WithLocation(8, 18),
59+
};
60+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
61+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
62+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
63+
}
64+
65+
protected override Project ApplyCompilationOptions(Project project)
66+
{
67+
var newProject = base.ApplyCompilationOptions(project);
68+
69+
var parseOptions = (CSharpParseOptions)newProject.ParseOptions;
70+
71+
return newProject.WithParseOptions(parseOptions.WithLanguageVersion(LanguageVersion.Latest));
72+
}
1073
}
1174
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1131UseReadableConditions.cs

Lines changed: 2 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
/// A comparison was made between a variable and a literal or constant value, and the variable appeared on the
@@ -68,7 +69,7 @@ private static void HandleBinaryExpression(SyntaxNodeAnalysisContext context)
6869
private static bool IsLiteral(ExpressionSyntax expression, SemanticModel semanticModel)
6970
{
7071
// Default expressions are most of the time constants, but not for default(MyStruct).
71-
if (expression.IsKind(SyntaxKind.DefaultExpression))
72+
if (expression.IsKind(SyntaxKind.DefaultExpression) || expression.IsKind(SyntaxKindEx.DefaultLiteralExpression))
7273
{
7374
return true;
7475
}

0 commit comments

Comments
 (0)