Fork me on GitHub

Unused Code

The Unused Code ruleset contains rules that find unused or ineffective code.

UnusedPrivateField

Since: PMD 0.1

Priority: 3

Detects when a private field is declared and/or assigned a value, but not used.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.unusedcode.UnusedPrivateFieldRule

Example(s):


public class Something {
  private static int FOO = 2; // Unused
  private int i = 5; // Unused
  private int j = 6;
  public int addOne() {
    return j++;
  }
}

    

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.

UnusedLocalVariable

Since: PMD 0.1

Priority: 3

Detects when a local variable is declared and/or assigned, but not used.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.unusedcode.UnusedLocalVariableRule

Example(s):


public class Foo {
	public void doSomething() {
		int i = 5; // Unused
	}
}

    

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.

UnusedPrivateMethod

Since: PMD 0.7

Priority: 3

Unused Private Method detects when a private method is declared but is unused.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.unusedcode.UnusedPrivateMethodRule

Example(s):


public class Something {
	private void foo() {} // unused
}

    

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.

UnusedFormalParameter

Since: PMD 0.8

Priority: 3

Avoid passing parameters to methods or constructors without actually referencing them in the method body.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.unusedcode.UnusedFormalParameterRule

Example(s):


public class Foo {
	private void bar(String howdy) {
	// howdy is not used
	}
}

    

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.
checkAll false Check all methods, including non-private ones

UnusedModifier

Since: PMD 1.02

Priority: 3

Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.unusedcode.UnusedModifierRule

Example(s):

 
public interface Foo {
  public abstract void bar(); 		// both abstract and public are ignored by the compiler
  public static final int X = 0; 	// public, static, and final all ignored
  public static class Bar {} 		// public, static ignored
  public static interface Baz {} 	// ditto
}
public class Bar {
  public static interface Baz {} // static ignored
}
 
     

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.