Skip to content

Commit

Permalink
Emit CA1822 for recursive method. (#6641)
Browse files Browse the repository at this point in the history
* Emit diagnostic on recursive method.

* Remove blank line.
  • Loading branch information
CollinAlpert authored May 25, 2023
1 parent 615f625 commit 2b6ab8d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)

context.RegisterOperationAction(context =>
{
if (((IInstanceReferenceOperation)context.Operation).ReferenceKind == InstanceReferenceKind.ContainingTypeInstance)
if (((IInstanceReferenceOperation)context.Operation).ReferenceKind == InstanceReferenceKind.ContainingTypeInstance
&& (context.Operation.Parent is not IInvocationOperation invocation || !invocation.TargetMethod.Equals(methodSymbol, SymbolEqualityComparer.Default)))
{
isInstanceReferenced = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1461,5 +1461,44 @@ Private Sub M()
End Sub
End Class");
}

[Fact, WorkItem(6540, "https://github.com/dotnet/roslyn-analyzers/issues/6540")]
public Task RecursiveMethod_DiagnosticAsync()
{
return new VerifyCS.Test
{
TestCode = @"
using System;
public class Test
{
public void [|Recursive|](string argument)
{
if (argument.Length > 1)
{
Recursive(argument[1..]);
}
Console.WriteLine($""argument[-1]: {argument}"");
}
}",
FixedCode = @"
using System;
public class Test
{
public static void Recursive(string argument)
{
if (argument.Length > 1)
{
Recursive(argument[1..]);
}
Console.WriteLine($""argument[-1]: {argument}"");
}
}",
LanguageVersion = LanguageVersion.CSharp8
}.RunAsync();
}
}
}

0 comments on commit 2b6ab8d

Please sign in to comment.