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

Add CodeFix "Wrap in Using" #21

Closed BADF00D closed 6 years ago

BADF00D commented 7 years ago

If an undisposed object is detected, there should be a possibility to wrap this in a using statement via CodeFix. There are several situation that should be treated differently:

An undisposed local variable

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            var mem = new MemoryStream();
            mem.Write(...)        
        }
    }
}

Should become:

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            using(var mem = new MemoryStream()){
                mem.Write(...);
            }
        }
    }
}

An undisposed anonymous variable

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            new MemoryStream();
        }
    }
}

Should become:

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            using(new MemoryStream()){
            }
        }
    }
}

An undisposed anonymous variable within an ctor call of a class that is not tracking

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            using(var reader = new NonTrackingReader(new MemoryStream()){

            }
        }
    }
}

Should become:

namespace SomeNamespace {
    public class SomeClass(){
        public void SomeClass(){
            using(var somename = new MemoryStream()){
                using(var reader = new NonTrackingReader(somename){

                }
            }
        }
    }
}
BADF00D commented 6 years ago

Will be part of release 0.39