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)
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:
By default, this rule does not analyze test files. This rule sets the default value of the doNotApplyToFilesMatching property to ignore file names ending in 'Test.groovy', 'Tests.groovy' or 'TestCase.groovy'. 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)
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:
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:
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:
Since CodeNarc 0.14 * You can specify an ignore list using the 'ignoreRegex' property. By default, a parameter named 'ignore' or 'ignored' does not trigger a violation (the regex value is 'ignore|ignored'). You can add your own ignore list using this property.
New in CodeNarc 0.16
This rule finds instances of method parameters not being used. It does not analyze private methods (that is done by the UnusedPrivateMethodParameter rule) or methods marked @Override.
Example of violations:
class MyClass { def method(def param) { // param is unused } }
Example of code that does not cause violations:
class MyClass { @Override def otherMethod(def param) { // this is OK because it overrides a super class } } class MyCategory { // Category classes are ignored by default void myMethod1(String string, int value) { } void myMethod1(String string, int value, name) { } } class MainClass1 { // main() methods are ignored public static void main(String[] args) { } } class MainClass2 { // This is also a valid Groovy main() method static main(args) { } }
Checks for variables that are never referenced.
Known limitations: