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

Added Annotation for custom tracking methods #72

Open BADF00D opened 6 years ago

BADF00D commented 6 years ago

Prerequisites

Description

In order to detect possible NullReferencesExceptions, JetBrains implemented some attributes within JetBrains.Annotations, so that everybody can mark their custom methods with attributes like CanBeNull. Something like that can be useful, too.

IgnoreMethodDuringDisposeDetectionAttribute

IgnoreClassDuringDisposeDetectionAttribute

TrackingMethodAttribute

CallsDisposeAttribute

BADF00D commented 4 years ago

New version

Attribute Description Target
Piped Defines that return value it that marked argument. Only on per argument in signature is allowed to be marked with this attribute Argument
Tracked On a Argument, it defined that the argument will get tracked by the method; On a ReturnValue, it defines that the return value is tracked by the method Argument or ReturnValue
TrackedSet Show file differences that haven't been staged Property
DisposeCall Defines that method is equivalent to a Dispose method Method

Piped

Often fluent APIs use this pattern to configure a object wit (extension) method. In order to allow the callee to detect such a pattern, the Argument that is used an input an output can be marked with this attribute.

Example:

BADF00D commented 4 years ago

We need an additional attribute to mark an setter as Safe during ObjectInitialization. E.g.

using System.Net;
using System.Net.Http;

namespace SomeNamespace
{
    internal class HttpResponseMessageSpec
    {
        public HttpResponseMessage CreateResponse1()
        {
            var msg = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                /* This should not yield an error because this is initialization and we Content if null after ctor was called. */
                Content = new StringContent("some content")
            };

            return msg;
        }
    }
}

There should be a Attribute that marks Content in order to signal, that Content is Null after ctor of HttpResponseMessage was called. Therefore the first set to Content with an IDisposable is allowed, We can modify TrackedSet. It can have a mode Always or Once. In order to simplify the code, a property marked with TrackedSet(Once) could only be set when using object initializer.

BADF00D commented 4 years ago

When DisposeCallAttribute is put on an interface method, the implementations and overrides should be treated as dispose calls as well

BADF00D commented 4 years ago

Checkout this https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/