DisposableFixer icon indicating copy to clipboard operation
DisposableFixer copied to clipboard

Added Annotation for custom tracking methods

Open BADF00D opened this issue 7 years ago • 4 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of DisposableFixer: YOUR_VERSION
  • [x] I have searched open and closed issues to ensure it has not already been reported

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

  • ca be allied to classed and methods
  • marks a method that should be ignored during dispose detection.

IgnoreClassDuringDisposeDetectionAttribute

  • ca be allied to classed and methods
  • Used for classes that implements IDisposable, but there is no need to dispose it,

TrackingMethodAttribute

  • can be applied to methods
  • defines that a method keeps track of IDisposable given via parameters
  • has optional parameter like: third parameter should be true

CallsDisposeAttribute

  • can be applied to methods
  • if this method was called, instance should be considered as disposed. Example
internal class Usage{
   public Usage(){
      var instance = new SomeSpecialClass() //this is not marked as undisposed, because Cleanup is called
      instance.Cleanup(); 
   }
}
internal class SomeSpecialClass : IDisposeable{
   [CallsDispose]
   public void Cleanup(){
      
   }
   public void Dispose(){
   }
}

BADF00D avatar Dec 20 '17 17:12 BADF00D

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 avatar Oct 27 '19 11:10 BADF00D

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 avatar Nov 08 '19 14:11 BADF00D

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

BADF00D avatar Mar 12 '20 08:03 BADF00D

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

BADF00D avatar Mar 12 '20 14:03 BADF00D