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

Fix S2583 FP: nested try catch blocks #8484

Merged
merged 9 commits into from
Dec 20, 2023

Conversation

Tim-Pohlmann
Copy link
Contributor

Fixes #8140

Copy link
Contributor

Choose a reason for hiding this comment

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

The test case for nested try-catch blocks is missing. Add it to ConditionEvaluatesToConstant.cs

// Reproducer for https://github.com/SonarSource/sonar-dotnet/issues/8484
public class Repro_8484
{
    void TestMethod()
    {
        Exception failure = null;

        try
        {
            try
            {
                var x = new object();
            }
            catch (InvalidOperationException)
            {
            }
        }
        catch (NotSupportedException exception)
        {
            failure = exception;
        }

        if (failure != null) // Compliant
        {
            Console.WriteLine("Found failures.");
        }
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good. I like the refactoring with handlersAreExhaustive variable; removing the yield break makes the code a bit more readable

@@ -52,7 +52,7 @@ public void Method4(bool condition)

public void Method5(string arg)
{
Monitor.Enter(obj); // Compliant
Monitor.Enter(obj); // Noncompliant
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was actually an FN. In case of a not-NullReference-Exception, Exit would not be called.

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

The "because" seems wrong. And it actually turns out it is really needed there :)

@Tim-Pohlmann Tim-Pohlmann force-pushed the TimCriZsolt/NestedCatch branch from 3cb2551 to 91f036c Compare December 20, 2023 15:16
@Tim-Pohlmann
Copy link
Contributor Author

@pavel-mikula-sonarsource I have rewritten the code to be more readable and have improved precision at the same time. I will be off on holiday. If more changes are necessary, please forward the PR to @cristian-ambrosini-sonarsource.

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

@pavel-mikula-sonarsource pavel-mikula-sonarsource merged commit a314cfc into master Dec 20, 2023
25 checks passed
@pavel-mikula-sonarsource pavel-mikula-sonarsource deleted the TimCriZsolt/NestedCatch branch December 20, 2023 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S2583 FP: nested try catch blocks
4 participants