Skip to content

Commit

Permalink
S1854: Add FP repro for #9466, #9467, #9468
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jun 26, 2024
1 parent 3e340a6 commit 5192baa
Showing 1 changed file with 117 additions and 6 deletions.
123 changes: 117 additions & 6 deletions analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1402,7 +1402,7 @@ public class Repro_4948

public void UsedInFinally()
{
int value = 42; // Compliant, Muted
int value = 42; // Compliant
try
{
SomethingThatCanThrow();
Expand Down Expand Up @@ -1439,7 +1439,7 @@ public void UsedInFinally_NestedInLambda()

public void UsedInFinally_Throw()
{
var value = 42; // Compliant
var value = 42; // Compliant
try
{
throw new Exception();
Expand All @@ -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();
Expand All @@ -1470,7 +1470,7 @@ public void UsedInFinally_Throw_FilteredCatch()
Use(value);
}
}

public void UsedInFinally_Throw_CatchAll()
{
var value = 42; // Noncompliant
Expand Down Expand Up @@ -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) { }
}

0 comments on commit 5192baa

Please sign in to comment.