gendarme icon indicating copy to clipboard operation
gendarme copied to clipboard

AvoidUncalledPrivateCodeRule fires on events and proprties

Open troberson opened this issue 13 years ago • 1 comments

The AvoidUncalledPrivateCodeRule fires on events and properties (add_, remove_, get_, set_). It is quite common to have an event which only utilizes add_ and not remove_. get_ and set_ are caught only when they are not from automatic properties, but it is still quite common to have a get_ or set_ which follow the basic pattern of:

int x;
int X {
    get { return x; }
    set { x = value; }
}

or other trivial thing, in which it is not necessary to warn about unused code. For example, a method may set 'x' directly, rather than using the property, in which case Gendarme will complain that the setter was never utilized.

using System;

class Test
{
    event EventHandler<EventArgs> TestEvent; // <----- Gendarme complains that add_ and remove_ are never used

    int _i;

    int TestProp {  // <----- Gendarme complains that get_ and set_ are never used
        get { return _i; }
        set { _i = value; }
    }

    int TestProp2 { get; set; } // <----- Gendarme recognizes these as automatically generated

    static void Main ()
    {
        new Test ();
    }
}

2. AvoidUncalledPrivateCodeRule

Problem: This private or internal (assembly-level) member does not have callers in the assembly, is not invoked by the common language runtime, and is not invoked by a delegate.
* Severity: High, Confidence: Normal
* Target:   System.Void Test::add_TestEvent(System.EventHandler`1<System.EventArgs>)
* Details:  The private method code is not used in its declaring type.

Solution: Remove the unused code or add code to call it.
More info available at: https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Performance.AvoidUncalledPrivateCodeRule(2.10)


3. AvoidUncalledPrivateCodeRule

Problem: This private or internal (assembly-level) member does not have callers in the assembly, is not invoked by the common language runtime, and is not invoked by a delegate. 
* Severity: High, Confidence: Normal
* Target:   System.Void Test::remove_TestEvent(System.EventHandler`1<System.EventArgs>)
* Details:  The private method code is not used in its declaring type.

Solution: Remove the unused code or add code to call it.
More info available at: https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Performance.AvoidUncalledPrivateCodeRule(2.10)

4. AvoidUncalledPrivateCodeRule

Problem: This private or internal (assembly-level) member does not have callers in the assembly, is not invok
* Severity: High, Confidence: Normal
* Target:   System.Int32 Test::get_TestProp()
* Details:  The private method code is not used in its declaring type.

Solution: Remove the unused code or add code to call it.
More info available at: https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Performance.AvoidUncalledPri


5. AvoidUncalledPrivateCodeRule

Problem: This private or internal (assembly-level) member does not have callers in the assembly, is not invok
* Severity: High, Confidence: Normal
* Target:   System.Void Test::set_TestProp(System.Int32)
* Details:  The private method code is not used in its declaring type.

Solution: Remove the unused code or add code to call it.
More info available at: https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Performance.AvoidUncalledPri

troberson avatar Jan 25 '12 20:01 troberson

My primary concern is not firing on remove_, since this produces many warnings in my code because events are added but not removed, which I am sure is fairly common. It may be useful to fire on add_, however, since you don't want unused events hanging around.

Many times getters and settings are included for completeness. For example, you may not need a getter but it seems strange to have a setter without a getter and so you put in a simple get { return x; }

What should the correct behavior of this rule be?

troberson avatar Jan 25 '12 21:01 troberson