Unused Rules ("rulesets/unused.xml")

UnusedArray Rule

Checks for array allocations that are not assigned or used, unless it is the last statement within a block (because it may be the intentional return value). Examples include:

    int myMethod() {
        new String[3]               // unused
        return -1
    }

    String[] myMethod() {
        new String[3]               // OK (last statement in block)
    }

    def closure = {
        doStuff()
        new Date[3]                 // unused
        doOtherStuff()
    }

    def closure = { new Date[3] }   // OK (last statement in block)

UnusedObject Rule

Checks for object allocations that are not assigned or used, unless it is the last statement within a block (because it may be the intentional return value). Examples include:

This rule sets the default value of the doNotApplyToClassNames property to only match class names that do not end in 'Test', 'Tests' or 'TestCase'. Invoking constructors without using the result is a common pattern in tests.

    int myMethod() {
        new BigDecimal("23.45")     // unused
        return -1
    }

    BigDecimal myMethod() {
        new BigDecimal("23.45")     // OK (last statement in block)
    }

    def closure = {
        doStuff()
        new Date()                  // unused
        doOtherStuff()
    }

    def closure = { new Date() }    // OK (last statement in block)

UnusedPrivateField Rule

Checks for private fields that are not referenced within the same class. Note that the private modifier is not currently "respected" by Groovy code (i.e., Groovy can access private members within other classes). By default, fields named serialVersionUID are ignored. The rule has a property named ignoreFieldNames, which can be set to ignore other field names as well. For instance, to ignore fields named 'fieldx', set the property to the 'fieldx, serialVersionUID'

Property Description Default Value
ignoreFieldNames Specifies one or more (comma-separated) field names that
should be ignored (i.e., that should not cause a rule
violation). The names may optionally contain wildcards (*,?).
serialVersionUID

Known limitations:

  • Does not recognize field access when field name is a GString (e.g. this."$fieldName")
  • Does not recognize access of private field of another instance (i.e. other than this)

UnusedPrivateMethod Rule

Checks for private methods that are not referenced within the same class. Note that the private modifier is not currently "respected" by Groovy code (i.e., Groovy can access private members within other classes).

Known limitations:

  • Does not recognize method reference through property access (e.g. getName() accessed as x.name)
  • Does not recognize method invocations when method name is a GString (e.g. this."$methodName"())
  • Does not recognize invoking private method of another instance (i.e. other than this)
  • Does not differentiate between multiple private methods with the same name but different parameters (i.e., overloaded)
  • Does not check for unused constructors

UnusedPrivateMethodParameter Rule

Since CodeNarc 0.12

Checks for parameters to private methods that are not referenced within the method body. Note that the private modifier is not currently "respected" by Groovy code (i.e., Groovy can access private members within other classes).

Known limitations:

  • Does not recognize parameter references within an inner class. See CodeNarc bug #3155974.
  • Does not recognize parameter references when parameter name is a GString (e.g. println "$parameterName")

UnusedVariable Rule

Checks for variables that are never referenced.

Known limitations:

  • Does not recognize variable references within an inner class. See CodeNarc bug #3155974.
  • Incorrectly considers a variable referenced if another variable with the same name is referenced elsewhere (in another scope/block).