From 5192baa3669ad7191d179bbeb602a0fe9fe6a243 Mon Sep 17 00:00:00 2001 From: Pavel Mikula Date: Wed, 26 Jun 2024 10:58:41 +0200 Subject: [PATCH] S1854: Add FP repro for #9466, #9467, #9468 --- .../TestCases/DeadStores.RoslynCfg.cs | 123 +++++++++++++++++- 1 file changed, 117 insertions(+), 6 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs index af336fe168f..9fef1a6fd89 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs @@ -1030,7 +1030,7 @@ public static class ReproIssues // https://github.com/SonarSource/sonar-dotnet/issues/2760 public static long WithNonIgnored_Declaration(string path) { - long length = 42; // Muted FP, FileInfo can throw and function can return this value + long length = 42; // Compliant, FileInfo can throw and function can return this value try { length = new System.IO.FileInfo(path).Length; @@ -1045,7 +1045,7 @@ public static long WithNonIgnored_Declaration(string path) public static long WithNonIgnored_Assignment(string path) { long length; - length = 42; // Muted FP, FileInfo can throw and function can return this value + length = 42; // Compliant, FileInfo can throw and function can return this value try { length = new System.IO.FileInfo(path).Length; @@ -1402,7 +1402,7 @@ public class Repro_4948 public void UsedInFinally() { - int value = 42; // Compliant, Muted + int value = 42; // Compliant try { SomethingThatCanThrow(); @@ -1439,7 +1439,7 @@ public void UsedInFinally_NestedInLambda() public void UsedInFinally_Throw() { - var value = 42; // Compliant + var value = 42; // Compliant try { throw new Exception(); @@ -1452,7 +1452,7 @@ public void UsedInFinally_Throw() public void UsedInFinally_Throw_FilteredCatch() { - var value = 42; // Compliant + var value = 42; // Compliant try { throw new Exception(); @@ -1470,7 +1470,7 @@ public void UsedInFinally_Throw_FilteredCatch() Use(value); } } - + public void UsedInFinally_Throw_CatchAll() { var value = 42; // Noncompliant @@ -1516,3 +1516,114 @@ public void TryReturns_Loop() private void SomethingThatCanThrow() { } } } + +public class PeachValidation +{ + // https://github.com/SonarSource/sonar-dotnet/issues/9466 + public int ReadInFinallyAfterCatch() + { + var value = 0; + try + { + CanThrow(); + value = 42; + } + catch + { + value = 1; // Noncompliant FP, used in finally after the throw + throw; + } + finally + { + Log(value); + } + return value; + } + + // https://github.com/SonarSource/sonar-dotnet/issues/9467 + public int ReadAfterCatchAll_WithType(bool condition) + { + var value = 100; // Noncompliant FP, used after catch all + try + { + CanThrow(); + if (condition) + { + CanThrow(); + } + value = 200; + } + catch (Exception exc) + { + } + return value; + } + + // https://github.com/SonarSource/sonar-dotnet/issues/9467 + public int ReadAfterCatchAll_NoType(bool condition) + { + var value = 100; // Noncompliant FP, used after catch all + try + { + CanThrow(); + if (condition) + { + CanThrow(); + } + value = 200; + } + catch (Exception exc) + { + } + return value; + } + + // https://github.com/SonarSource/sonar-dotnet/issues/9467 + public void ReadInCatch_WithBranching(bool condition) + { + var value = 100; // Noncompliant FP, used in catch + try + { + value = CanThrow(); + if (condition) + { + CanThrow(); + } + else + { + CanThrow(); + } + } + catch + { + Log(value); + } + } + + // https://github.com/SonarSource/sonar-dotnet/issues/9468 + public void NestedCatchAndRethrow() + { + var value = 100; + try + { + try + { + CanThrow(); + } + catch + { + value = 200; // Noncompliant FP + throw; + } + } + catch + { + Log(value); + } + } + + private int CanThrow() => + throw new Exception(); + + private void Log(int value) { } +}