diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs index 72cd1013f..ae5fd8f42 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs @@ -508,5 +508,81 @@ public void Test() }; await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } + + [Theory] + [InlineData("Method1", "arg", true)] + [InlineData("Method2", "arg", true)] + [InlineData("Method1", "field1", true)] + [InlineData("Method2", "field1", true)] + [InlineData("Method1", "field2", true)] + [InlineData("Method2", "field2", true)] + [InlineData("Const1", "Method1", false)] + [InlineData("Const1", "Method2", false)] + [InlineData("Method1", "Const1", false)] + [InlineData("Method2", "Const1", false)] + [WorkItem(3677, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3677")] + public async Task TestMethodsAsync(string expr1, string expr2, bool shouldTrigger) + { + var testExpr = $"{expr1} == {expr2}"; + var testCode = $@" +using System; + +public class TestClass +{{ + private static readonly Action Const1 = Method1; + + private Action field1 = Method1; + private readonly Action field2 = Method1; + + public bool TestMethod(Action arg) + {{ + return {(shouldTrigger ? $"[|{testExpr}|]" : testExpr)}; + }} + + private static void Method1() + {{ + }} + + private void Method2() + {{ + }} +}} +"; + + var fixedExpr = $"{expr2} == {expr1}"; + var fixedCode = $@" +using System; + +public class TestClass +{{ + private static readonly Action Const1 = Method1; + + private Action field1 = Method1; + private readonly Action field2 = Method1; + + public bool TestMethod(Action arg) + {{ + return {fixedExpr}; + }} + + private static void Method1() + {{ + }} + + private void Method2() + {{ + }} +}} +"; + + if (shouldTrigger) + { + await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false); + } + else + { + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1131UseReadableConditions.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1131UseReadableConditions.cs index d9be391ce..e3af5070e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1131UseReadableConditions.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1131UseReadableConditions.cs @@ -82,12 +82,16 @@ private static bool IsLiteral(ExpressionSyntax expression, SemanticModel semanti return true; } - if (semanticModel.GetSymbolInfo(expression).Symbol is IFieldSymbol fieldSymbol) + var symbol = semanticModel.GetSymbolInfo(expression).Symbol; + switch (symbol) { - return fieldSymbol.IsStatic && fieldSymbol.IsReadOnly; - } + case IFieldSymbol fieldSymbol when fieldSymbol.IsStatic && fieldSymbol.IsReadOnly: + case IMethodSymbol: + return true; - return false; + default: + return false; + } } } }