Unused Code Rules

The Unused Code Ruleset contains a collection of rules that find unused code.

UnusedPrivateField

Unused Private Field detects when a private field is declared that is not used by the class.

Here's an example of code that would trigger this rule:

 

public class Something {
  private static int FOO = 2; // Unused
  private int i = 5; // Unused
  private int j = 6;

  public int addOne() {
    return j++;
  }
}

     

UnusedLocalVariable

Unused Local Variables detects when a variable is declared, but not used (except for possibly initial assignment)

Here's an example of code that would trigger this rule:

 

public int doSomething() {
  int i = 5; // Unused
  int j = 6;
  j += 3;
  return j;
}

     

UnusedPrivateMethod

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

Here's an example of code that would trigger this rule:

 

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

     

UnusedFormalParameter

Avoid passing parameters to methods and then not using those parameters.

Here's an example of code that would trigger this rule:

 

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