Skip to content

Commit 73a46ae

Browse files
authored
Merge pull request #3760 from bjornhellander/feature/sa1131-left-expressions
Fix SA1131 to not treat "complex" expressions as a literal
2 parents 36cc2cc + e099358 commit 73a46ae

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs

+57-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,55 @@ public void Test()
521521
[InlineData("Method1", "Const1", false)]
522522
[InlineData("Method2", "Const1", false)]
523523
[WorkItem(3677, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3677")]
524-
public async Task TestMethodsAsync(string expr1, string expr2, bool shouldTrigger)
524+
public async Task TestSimpleMethodsAsync(string expr1, string expr2, bool shouldTrigger)
525+
{
526+
await this.TestMethodAsync(expr1, expr2, shouldTrigger).ConfigureAwait(false);
527+
}
528+
529+
[Theory]
530+
[InlineData("TestClass.Method1", "arg", true)]
531+
[InlineData("this.Method2", "arg", true)]
532+
[InlineData("TestClass.Method1", "field1", true)]
533+
[InlineData("this.Method2", "field1", true)]
534+
[InlineData("TestClass.Method1", "field2", true)]
535+
[InlineData("this.Method2", "field2", true)]
536+
[InlineData("Const1", "TestClass.Method1", false)]
537+
[InlineData("Const1", "this.Method2", false)]
538+
[InlineData("TestClass.Method1", "Const1", false)]
539+
[InlineData("this.Method2", "Const1", false)]
540+
[InlineData("Method3<int>", "arg", true)]
541+
[InlineData("TestClass.Method3<int>", "arg", true)]
542+
[WorkItem(3759, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3759")]
543+
public async Task TestComplexMethodsAsync(string expr1, string expr2, bool shouldTrigger)
544+
{
545+
await this.TestMethodAsync(expr1, expr2, shouldTrigger).ConfigureAwait(false);
546+
}
547+
548+
[Theory]
549+
[InlineData("==")]
550+
[InlineData("!=")]
551+
[InlineData(">=")]
552+
[InlineData("<=")]
553+
[InlineData(">")]
554+
[InlineData("<")]
555+
[WorkItem(3759, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3759")]
556+
public async Task TestComplexLeftHandSideExpressionAsync(string @operator)
557+
{
558+
var testCode = $@"
559+
using System;
560+
public class TypeName
561+
{{
562+
public void Test(int x, int y, Func<int> a)
563+
{{
564+
var r1 = x + 1 {@operator} y;
565+
var r2 = -x {@operator} y;
566+
var r3 = a() {@operator} y;
567+
}}
568+
}}";
569+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
570+
}
571+
572+
private async Task TestMethodAsync(string expr1, string expr2, bool shouldTrigger)
525573
{
526574
var testExpr = $"{expr1} == {expr2}";
527575
var testCode = $@"
@@ -546,6 +594,10 @@ private static void Method1()
546594
private void Method2()
547595
{{
548596
}}
597+
598+
private static void Method3<T>()
599+
{{
600+
}}
549601
}}
550602
";
551603

@@ -572,6 +624,10 @@ private static void Method1()
572624
private void Method2()
573625
{{
574626
}}
627+
628+
private static void Method3<T>()
629+
{{
630+
}}
575631
}}
576632
";
577633

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ private static bool IsLiteral(ExpressionSyntax expression, SemanticModel semanti
8686
switch (symbol)
8787
{
8888
case IFieldSymbol fieldSymbol when fieldSymbol.IsStatic && fieldSymbol.IsReadOnly:
89-
case IMethodSymbol:
89+
return true;
90+
91+
// NOTE: Without the when clause, this would also treat e.g unary or binary expressions as literals,
92+
// since GetSymbolInfo returns the operator method in those cases.
93+
case IMethodSymbol when expression is NameSyntax or MemberAccessExpressionSyntax:
9094
return true;
9195

9296
default:

0 commit comments

Comments
 (0)