Skip to content

Commit dfd5ed2

Browse files
Update SA1131 to treat methods as constants
#3677
1 parent d1c94ed commit dfd5ed2

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,5 +508,81 @@ public void Test()
508508
};
509509
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
510510
}
511+
512+
[Theory]
513+
[InlineData("Method1", "arg", true)]
514+
[InlineData("Method2", "arg", true)]
515+
[InlineData("Method1", "field1", true)]
516+
[InlineData("Method2", "field1", true)]
517+
[InlineData("Method1", "field2", true)]
518+
[InlineData("Method2", "field2", true)]
519+
[InlineData("Const1", "Method1", false)]
520+
[InlineData("Const1", "Method2", false)]
521+
[InlineData("Method1", "Const1", false)]
522+
[InlineData("Method2", "Const1", false)]
523+
[WorkItem(3677, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3677")]
524+
public async Task TestMethodsAsync(string expr1, string expr2, bool shouldTrigger)
525+
{
526+
var testExpr = $"{expr1} == {expr2}";
527+
var testCode = $@"
528+
using System;
529+
530+
public class TestClass
531+
{{
532+
private static readonly Action Const1 = Method1;
533+
534+
private Action field1 = Method1;
535+
private readonly Action field2 = Method1;
536+
537+
public bool TestMethod(Action arg)
538+
{{
539+
return {(shouldTrigger ? $"[|{testExpr}|]" : testExpr)};
540+
}}
541+
542+
private static void Method1()
543+
{{
544+
}}
545+
546+
private void Method2()
547+
{{
548+
}}
549+
}}
550+
";
551+
552+
var fixedExpr = $"{expr2} == {expr1}";
553+
var fixedCode = $@"
554+
using System;
555+
556+
public class TestClass
557+
{{
558+
private static readonly Action Const1 = Method1;
559+
560+
private Action field1 = Method1;
561+
private readonly Action field2 = Method1;
562+
563+
public bool TestMethod(Action arg)
564+
{{
565+
return {fixedExpr};
566+
}}
567+
568+
private static void Method1()
569+
{{
570+
}}
571+
572+
private void Method2()
573+
{{
574+
}}
575+
}}
576+
";
577+
578+
if (shouldTrigger)
579+
{
580+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
581+
}
582+
else
583+
{
584+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
585+
}
586+
}
511587
}
512588
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,16 @@ private static bool IsLiteral(ExpressionSyntax expression, SemanticModel semanti
8282
return true;
8383
}
8484

85-
if (semanticModel.GetSymbolInfo(expression).Symbol is IFieldSymbol fieldSymbol)
85+
var symbol = semanticModel.GetSymbolInfo(expression).Symbol;
86+
switch (symbol)
8687
{
87-
return fieldSymbol.IsStatic && fieldSymbol.IsReadOnly;
88-
}
88+
case IFieldSymbol fieldSymbol when fieldSymbol.IsStatic && fieldSymbol.IsReadOnly:
89+
case IMethodSymbol:
90+
return true;
8991

90-
return false;
92+
default:
93+
return false;
94+
}
9195
}
9296
}
9397
}

0 commit comments

Comments
 (0)