Geeksltd / GCop

GCop packages
https://geeksltd.github.io/GCop/
24 stars 8 forks source link

GCop302 false positive #76

Open PaymonK opened 6 years ago

PaymonK commented 6 years ago

If a disposable object is returned from the method then this rule should be skipped.

Also if it's being passed as an argument to another object, it should be skipped, unless the object to which this is passed is itself being disposed in the same method.

Foroughi commented 6 years ago

@PaymonK Would you please provide an actual example of this false alarm ?! based on what I`ve seen on the codes of this rule, all the points you raised up is being checked there!

sungam3r commented 5 years ago

I also encountered such an error.

 internal class Bus
    {
        private readonly System.Collections.Generic.List<Subscription> _subscriptions = new System.Collections.Generic.List<Subscription>();

        public void NotifyAllSubscriptions(string msg) => _subscriptions.ForEach(s => s.Callback(msg));

        public IDisposable CreateSubscription(Action<string> callback)
        {
            return callback == null ? new Noop() : (IDisposable)new Subscription(this, callback); // GCop302    Since 'Noop' / 'Subscription' implements IDisposable, wrap it in a using() statement    
        }

        private class Subscription : IDisposable
        {
            public Subscription(Bus bus, Action<string> callback)
            {
                Bus = bus;
                Callback = callback;
            }

            public Bus Bus { get; }
            public Action<string> Callback { get; }

            public void Dispose()
            {
                Bus._subscriptions.Remove(this);
            }
        }

        private sealed class Noop : IDisposable
        {
            void IDisposable.Dispose() { }
        }
    }

I think that the matter is in the ternary operator.

 public IDisposable CreateSubscription(Action<string> callback)
        {
            if (callback == null) return new Noop(); // OK

            return new Subscription(this, callback); // OK
        }