Skip to content

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

Open

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

Activity

  1. mary-georgiou commented on Mar 25, 2026

    @mary-georgiou
    Contributor

    Hello @paulhickman-ec,
    I confirm this is an FP.

    thanks for the report.

  2. changed the title [-]Fix S3966 FP: Detects[/-] [+]NET-3470 Fix S3966 FP: Detects[/+] on Mar 25, 2026
  3. github-actions commented on Mar 25, 2026

    @github-actions
    Contributor

    Internal ticket NET-3470

  4. changed the title [-]NET-3470 Fix S3966 FP: Detects[/-] [+]Fix S3966 FP: Rule does not recognize that a new instance is created after the disposal[/+] on Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions