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

Properties that use extension in getter yield undisposed property #66

Closed dscopra closed 7 years ago

dscopra commented 7 years ago

Prerequisites

Description

When using extension any methods within a property, there is an Property not disposed warning, besides there is no IDisposable at all. When using an ExpressionBody instead, the warning disappears.

Source Code

using System;

namespace ExtensionMethodYieldsNotDisposed {
    internal class ErrorInExtensionMethods {
        public DateTimeOffset Timestamp { get; set; }

        public bool IsNewGetter
        {
            get { return Timestamp.IsNull(); }
        }

        public bool IsNewExpressionBody => Timestamp.IsNull();

        public string EmptyStringGetter
        {
            get { return Timestamp.ReturnStringEmpty(); }
        }
    }

    public static class ObjectExtensions {
        public static bool IsNull(this object @object) {
            return ReferenceEquals(@object, null);
        }

        public static string ReturnStringEmpty(this object @object) {
            return string.Empty;
        }
    }
}

Screenshot

image

BADF00D commented 7 years ago

The problem is, that the analysis of the MethodInvocation never checks if the return type implements IDisposable and therefore should be checked at all.

DisposableAnalyser:308

if (type == null) { } 
else if (node.IsParentADisposeCallIgnoringParenthesis()) 

Should be:

if (type == null) { } 
else if (!ImplementsIDisposable(type)) return;
else if (node.IsParentADisposeCallIgnoringParenthesis())