##### Overview

- Download PMD 5.0.4
- What's new in PMD 5.0.4
- PMD in the news
- PMD-related products and books
- Best practices
- Future directions
- Similar projects
- Credits
- License
- What does 'PMD' mean?

##### Usage

- Installation
- Command line usage
- Ant task usage
- Maven plugin usage
- Mvn plugin usage
- IDE plugin usage
- Suppressing warnings
- Finding duplicated code
- JSP support

##### Customizing PMD

- Compiling PMD
- How to write a rule
- Writing XPath rules
- How to make a rule set
- How it works
- Rule guidelines
- PMD developers information
- PMD Release Process

##### For example

- Run PMD on a Sourceforge project

##### Misc.

- XPath tutorial (Gregorian)

##### Rule Sets

- ecmascript
  - Basic Ecmascript
  - Braces
  - Unnecessary
- java
  - Android
  - Basic
  - Braces
  - Clone Implementation
  - Code Size
  - Comments
  - Controversial
  - Coupling
  - Design
  - Empty Code
  - Finalizer
  - Import Statements
  - J2EE
  - JavaBeans
  - JUnit
  - Jakarta Commons Logging
  - Java Logging
  - Migration
  - Naming
  - Optimization
  - Strict Exceptions
  - String and StringBuffer
  - Security Code Guidelines
  - Type Resolution
  - Unnecessary
  - Unused Code
- jsp
  - Basic JSF
  - Basic JSP
- xml
  - Basic XML
- xsl
  - XPath in XSL

##### Project Documentation

- Project Information
- Project Reports

## 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
    }

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.

### 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;
      }
    }

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.

### 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
    }

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.
ignoreAnnotations | false | Ignore annotations

### 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
      }
    }

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.

### 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 and
        count(./ConditionalOrExpression)=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[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral = 'true'])]/PrimaryExpression/PrimaryPrefix/Expression[
        count(*)=1 and
        not(./CastExpression) and
        not(./ConditionalExpression)]
    |
    //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
       }

    }