diff --git a/src/Ryujinx.Analyzers/CatchClauseAnalyzer.cs b/src/Ryujinx.Analyzers/CatchClauseAnalyzer.cs index f22be9dfc..a0500cfeb 100644 --- a/src/Ryujinx.Analyzers/CatchClauseAnalyzer.cs +++ b/src/Ryujinx.Analyzers/CatchClauseAnalyzer.cs @@ -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()) { diff --git a/src/Ryujinx.Tests.Analyzers/CatchClauseAnalyzerTests.cs b/src/Ryujinx.Tests.Analyzers/CatchClauseAnalyzerTests.cs index c5e4c8dab..32c5d541c 100644 --- a/src/Ryujinx.Tests.Analyzers/CatchClauseAnalyzerTests.cs +++ b/src/Ryujinx.Tests.Analyzers/CatchClauseAnalyzerTests.cs @@ -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 {