Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S1854: Add FP repro for #9466, #9467, #9468 #9469

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
Comment on lines +1556 to +1558
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more ecological.

Suggested change
catch (Exception exc)
{
}
catch (Exception ex) { }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'd normally do that, in this case, the catch has significant impact on the flow so I wanted to waste a bit of space on it.

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)
{
}
Comment on lines +1575 to +1577
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
catch (Exception exc)
{
}
catch (Exception ex) { }

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();
Comment on lines +1607 to +1611
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
catch
{
value = 200; // Noncompliant FP
throw;
}
}
catch
{
Log(value);
}
}

private int CanThrow() =>
throw new Exception();

private void Log(int value) { }
}
Loading