Skip to content

Fix S3966 FP: Rule does not recognize that a new instance is created after the disposal #9799

@paulhickman-ec

Description

@paulhickman-ec

Description

This code generates S3966 erroneously detecting f has already been disposed in the 'finally' block and not realising a new instance was assigned in the 'if'.

Reproducer

public class Foo : IDisposable
{
    public void Dispose()
    {
    }

    public static void Bar(bool x)
    {
        var f = new Foo();
        try
        {
            if (x)
            {
                f.Dispose();
                f = new Foo();
            }
        }
        finally
        {
            f.Dispose();
        }
    }
}

Product and Version

SonarAnalyzer.CSharp Version 10.17.0.131074

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions