BADF00D / DisposableFixer

This is a Visual Studio Extension and NuGet package that should identify and fix problems as memleaks while using IDisposables.
Other
35 stars 7 forks source link

conditional in Using statement incorrectly warns #115

Closed AartBluestoke closed 5 years ago

AartBluestoke commented 5 years ago

Prerequisites

Description

if you have a condition to decide which of 2 potential disposables (eg, 2 different ways to get a database connection) inside the using statement, then it warns that they are not disposed, even though the one returned to the using will be.

Source Code

        Func<IDisposable> f=()=>null;
        public void disposableTest(bool flag)
        {
            using (flag?f():f()) // whichever one we used will be disposed, but we get a warning on both
            { }
        }

Screenshot

also the suggested 'fix' changes the semantic behaviour to always create that IDisposable from the call, rather than just the one we decide to use. image

BADF00D commented 5 years ago

Hi, thanks for reporting. Currently each MethodeInvocation are analysed independently from each other, causing this bug. I think this should be possible to fix. The problem CodeFixes should be gone automatically, since the there will be no more "undispose anonymous variable" warnings

BADF00D commented 5 years ago

Will be part of release 1.7.0