SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
781 stars 226 forks source link

Fix S1854 FP: Value used after catch #9467

Open pavel-mikula-sonarsource opened 3 months ago

pavel-mikula-sonarsource commented 3 months ago

Removing the if will make this work as expected. The block before try should already be connected to catch block. It's not clear why it produces a FP

    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;
    }

This likely has the same root cause as

    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);
        }
    }