Ignore catch clauses with throw statements or with "_" as the identifier

This commit is contained in:
TSR Berry 2024-03-17 18:38:59 +01:00
parent 81988c9ed8
commit 8449c8e2ab
No known key found for this signature in database
GPG key ID: 52353C0A4CCA15E2
2 changed files with 95 additions and 8 deletions

View file

@ -117,6 +117,12 @@ namespace Ryujinx.Analyzers
var catchDeclaration = catchClauseSyntax.Declaration;
// Ignore if catch block contains a throw statement.
if (catchClauseSyntax.Block.DescendantNodes().Any(x => x is ThrowStatementSyntax))
{
return;
}
// Find catch clauses without declaration.
if (catchDeclaration == null)
{
@ -145,6 +151,12 @@ namespace Ryujinx.Analyzers
var catchDeclarationIdentifier = catchDeclaration.Identifier;
bool exceptionLogged = false;
// Ignore if identifier is equal to "_"
if (catchDeclarationIdentifier.Text == "_")
{
return;
}
// Iterate through all expression statements
foreach (var blockNode in catchClauseSyntax.Block.DescendantNodes())
{

View file

@ -32,7 +32,7 @@ public class MyClass
}
catch
{
throw;
// Skip
}
}
}
@ -72,13 +72,88 @@ public class MyClass
await Verifier.VerifyAnalyzerAsync(Text, expected).ConfigureAwait(false);
}
[Fact]
public async Task CatchWithEmptyThrowStatement_NoDiagnostic()
{
string text = @"
using System;
public class MyClass
{
public void MyMethod3()
{
try
{
Console.WriteLine(""test"");
}
catch
{
throw;
}
}
}
";
await Verifier.VerifyAnalyzerAsync(text).ConfigureAwait(false);
}
[Fact]
public async Task CatchWithIdentifierAndThrowStatement_NoDiagnostic()
{
string text = @"
using System;
public class MyClass
{
public void MyMethod4()
{
try
{
Console.WriteLine(""test"");
}
catch (NullReferenceException exception)
{
throw new InvalidOperationException(""invalid"");
}
}
}
";
await Verifier.VerifyAnalyzerAsync(text).ConfigureAwait(false);
}
[Fact]
public async Task CatchWithIgnoredIdentifier_NoDiagnostic()
{
string text = @"
using System;
public class MyClass
{
public void MyMethod5()
{
try
{
Console.WriteLine(""test"");
}
catch (NullReferenceException _)
{
// Skip
}
}
}
";
await Verifier.VerifyAnalyzerAsync(text).ConfigureAwait(false);
}
[Fact]
public async Task LogWithoutCatchIdentifier_WarningDiagnostic()
{
string text = _loggerText + @"
public class MyClass
{
public void MyMethod3()
public void MyMethod6()
{
try
{
@ -104,7 +179,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod4()
public void MyMethod7()
{
try
{
@ -128,7 +203,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod5()
public void MyMethod8()
{
try
{
@ -152,7 +227,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod6()
public void MyMethod9()
{
try
{
@ -176,7 +251,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod7()
public void MyMethod10()
{
try
{
@ -201,7 +276,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod8()
public void MyMethod11()
{
try
{
@ -225,7 +300,7 @@ public class MyClass
string text = _loggerText + @"
public class MyClass
{
public void MyMethod9()
public void MyMethod12()
{
try
{