Unnecessary

The Unnecessary Ruleset contains a collection of rules for unnecessary code.

UnnecessaryConversionTemporary

Since: PMD 0.1

Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods on the wrapper classes instead.

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

Example(s):
  
public String convert(int x) {
	String foo = new Integer(x).toString();	// this wastes an object
	
	return Integer.toString(x);				// preferred approach
}
 
      

UnnecessaryReturn

Since: PMD 1.3

Avoid the use of unnecessary return statements.

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

Example(s):
		
public class Foo {
  public void bar() {
    int x = 42;
    return;
  }
}
		
      

UnnecessaryFinalModifier

Since: PMD 3.0

When a class has the final modifier, all the methods are automatically final and do not need to be tagged as such.

    
//ClassOrInterfaceDeclaration[@Final='true' and @Interface='false']
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[@Final='true']
    
              
Example(s):

public final class Foo {
    // This final modifier is not necessary, since the class is final
    // and thus, all methods are final
    private final void foo() {
    }
}


      

UselessOverridingMethod

Since: PMD 3.3

The overriding method merely calls the same method defined in a superclass.

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

Example(s):
public void foo(String bar) {
  super.foo(bar);      // why bother overriding?
}

public String foo() {
	return super.foo();  // why bother overriding?
}

@Id
public Long getId() {
  return super.getId();  // OK if 'ignoreAnnotations' is false, which is the default behavior
}
        

UselessOperationOnImmutable

Since: PMD 3.5

An operation on an Immutable object (String, BigDecimal or BigInteger) won't change the object itself since the result of the operation is a new object. Therefore, ignoring the operation result is an error.

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

Example(s):
    
import java.math.*;

class Test {
  void method1() {
    BigDecimal bd=new BigDecimal(10);
    bd.add(new BigDecimal(5)); 		// this will trigger the rule
  }
  void method2() {
    BigDecimal bd=new BigDecimal(10);
    bd = bd.add(new BigDecimal(5)); // this won't trigger the rule
  }
}
    
      

UnusedNullCheckInEquals

Since: PMD 3.5

After checking an object reference for null, you should invoke equals() on that object rather than passing it to another object's equals() method.

        
(//PrimaryPrefix[ends-with(Name/@Image, '.equals') and Name/@Image != 'Arrays.equals'] | //PrimarySuffix[@Image='equals' and not(../PrimaryPrefix/Literal)])
 /following-sibling::PrimarySuffix/Arguments/ArgumentList/Expression
 /PrimaryExpression[count(PrimarySuffix)=0]/PrimaryPrefix
 /Name[@Image = ./../../../../../../../../../../Expression/ConditionalAndExpression
 /EqualityExpression[@Image="!=" and count(./preceding-sibling::*)=0 and
 ./PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
  /PrimaryExpression/PrimaryPrefix/Name/@Image]
        
        
Example(s):
		
public class Test {

  public String method1() { return "ok";}
  public String method2() { return null;}

  public void method(String a) {
    String b;
	// I don't know it method1() can be "null"
	// but I know "a" is not null..
	// I'd better write a.equals(method1())
	
	if (a!=null && method1().equals(a)) { // will trigger the rule
	//whatever
	}
	
	if (method1().equals(a) && a != null) { // won't trigger the rule
	//whatever
	}
	
	if (a!=null && method1().equals(b)) { // won't trigger the rule
	//whatever
	}
	
	if (a!=null && "LITERAL".equals(a)) { // won't trigger the rule
	//whatever
	}
	
	if (a!=null && !a.equals("go")) { // won't trigger the rule
	a=method2();
	if (method1().equals(a)) {
	//whatever
	}
  }
}
}
				
			

UselessParentheses

Since: PMD 5.0

Useless parentheses should be removed.

          
//Expression/PrimaryExpression/PrimaryPrefix/Expression[count(*)=1 and count(./CastExpression)=0]
|
//Expression/ConditionalAndExpression/PrimaryExpression/PrimaryPrefix/Expression[
    count(*)=1 and
    count(./CastExpression)=0 and
    count(./EqualityExpression/MultiplicativeExpression)=0]
|
//Expression/ConditionalOrExpression/PrimaryExpression/PrimaryPrefix/Expression[
    count(*)=1 and
    count(./CastExpression)=0 and
    count(./EqualityExpression/MultiplicativeExpression)=0]
|
//Expression/ConditionalExpression/PrimaryExpression/PrimaryPrefix/Expression[
    count(*)=1 and
    count(./CastExpression)=0 and
    count(./EqualityExpression)=0]
|
//Expression/AdditiveExpression/PrimaryExpression/PrimaryPrefix/Expression[count(*)=1 and count(./CastExpression)=0]
|
//Expression/EqualityExpression/PrimaryExpression/PrimaryPrefix/Expression[
    count(*)=1 and
    count(./CastExpression)=0 and
    count(./AndExpression)=0 and
    count(./InclusiveOrExpression)=0 and
    count(./ExclusiveOrExpression)=0 and
    count(./ConditionalAndExpression)=0 and
    count(./ConditionalOrExpression)=0]
          
          
Example(s):
    
public class Foo {

   private int _bar1;
   private Integer _bar2;

   public void setBar(int n) {
      _bar1 = Integer.valueOf((n)); // here
      _bar2 = (n); // and here
   }

}