Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1131 to treat methods as constants #3710

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Typically we would want the inputs to the test to be parameters, but the outputs to be computed within the test method. In this case, shouldTrigger should be a local variable and not a parameter, calculated like this:

bool shouldTrigger = (expr1, expr2) switch
{
  ("Method1", "arg") => true,
  // ... remaining inputs
  _ => throw new NotSupportedException(),
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. I will try to remember 👍

{
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 It's also possible to do this:

case IFieldSymbol { IsStatic: true, IsReadOnly: true }:

case IMethodSymbol:
return true;

return false;
default:
return false;
}
}
}
}