All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rulesets.java.unnecessary.xml Maven / Gradle / Ivy

There is a newer version: 2.0.9
Show newest version
<?xml version="1.0"?>

<ruleset name="Unnecessary"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
  <description>
The Unnecessary Ruleset contains a collection of rules for unnecessary code.
  </description>
   
	
    <rule name="UnnecessaryConversionTemporary"
    		 since="0.1"
          message="Avoid unnecessary temporaries when converting primitives to Strings"
          class="net.sourceforge.pmd.lang.java.rule.unnecessary.UnnecessaryConversionTemporaryRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UnnecessaryConversionTemporary">
      <description>
Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods
on the wrapper classes instead.
      </description>
        <priority>3</priority>
      <example>
  <![CDATA[
public String convert(int x) {
	String foo = new Integer(x).toString();	// this wastes an object
	
	return Integer.toString(x);				// preferred approach
}
 ]]>
      </example>
    </rule>



    <rule name="UnnecessaryReturn"
    		 since="1.3"
          message="Avoid unnecessary return statements"
          class="net.sourceforge.pmd.lang.java.rule.unnecessary.UnnecessaryReturnRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UnnecessaryReturn">
      <description>
Avoid the use of unnecessary return statements.
      </description>
      <priority>3</priority>
      <example>
		<![CDATA[
public class Foo {
  public void bar() {
    int x = 42;
    return;
  }
}
		]]>
      </example>
    </rule>


    <rule name="UnnecessaryFinalModifier"
    	  language="java"
    	  since="3.0"
          message="Unnecessary final modifier in final class"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UnnecessaryFinalModifier">
      <description>
When a class has the final modifier, all the methods are automatically final and do not need to be
tagged as such.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
    <![CDATA[
//ClassOrInterfaceDeclaration[@Final='true' and @Interface='false']
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[@Final='true']
    ]]>
              </value>
          </property>
      </properties>
      <example>
<![CDATA[
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() {
    }
}

]]>
      </example>
    </rule>


    <rule name="UselessOverridingMethod"
    since="3.3"
    message="Overriding method merely calls super"
    class="net.sourceforge.pmd.lang.java.rule.unnecessary.UselessOverridingMethodRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UselessOverridingMethod">
        <description>
The overriding method merely calls the same method defined in a superclass.
        </description>
        <priority>3</priority>
        <example><![CDATA[
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
}
        ]]></example>
    </rule>

    <rule  name="UselessOperationOnImmutable"
           since="3.5"
           message="An operation on an Immutable object (String, BigDecimal or BigInteger) won't change the object itself"
           class="net.sourceforge.pmd.lang.java.rule.unnecessary.UselessOperationOnImmutableRule"
           externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UselessOperationOnImmutable">
      <description>
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.
      </description>
      <priority>3</priority>
      <example>
    <![CDATA[
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
  }
}
    ]]>
      </example>
    </rule>

    <rule name="UnusedNullCheckInEquals"
    		 language="java"
        since="3.5"
        message="Invoke equals() on the object you''ve already ensured is not null"
        class="net.sourceforge.pmd.lang.rule.XPathRule"
        externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UnusedNullCheckInEquals">
    <description>
After checking an object reference for null, you should invoke equals() on that object rather than passing it to another object's equals() method.
    </description>
	<priority>3</priority>
    <properties>
        <property name="xpath">
        <value>
        <![CDATA[
(//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]
        ]]>
        </value>
        </property>
    </properties>
		<example>
		<![CDATA[
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
	}
  }
}
}
				]]>
			</example>
		</rule>

  <rule name="UselessParentheses"
  		language="java"
        since="5.0"
        message="Useless parentheses."
        class="net.sourceforge.pmd.lang.rule.XPathRule"
        externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UselessParentheses">
    <description>Useless parentheses should be removed.</description>
    <priority>4</priority>
    <properties>
       <property name="xpath">
          <value>
          <![CDATA[
//Expression/PrimaryExpression/PrimaryPrefix/Expression
[count(*)=1][count(./CastExpression)=0][count(./ConditionalExpression[@Ternary='true'])=0]
[not(./AdditiveExpression[//Literal[@StringLiteral='true']])]
|
//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[1]/PrimaryPrefix/Expression[
    count(*)=1 and
    not(./CastExpression) and
    not(./AdditiveExpression[@Image = '-']) and
    not(./ShiftExpression) and
    not(./RelationalExpression) and
    not(./InstanceOfExpression) and
    not(./EqualityExpression) and
    not(./AndExpression) and
    not(./ExclusiveOrExpression) and
    not(./InclusiveOrExpression) and
    not(./ConditionalAndExpression) and
    not(./ConditionalOrExpression) 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 and
    count(./EqualityExpression)=0]
          ]]>
          </value>
       </property>
    </properties>
    <example>
    <![CDATA[
public class Foo {

   private int _bar1;
   private Integer _bar2;

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

}
    ]]>
    </example>
  </rule>

  <rule name="UselessQualifiedThis"
      language="java"
      since="5.4.0"
      message="Useless qualified this usage in the same class."
      class="net.sourceforge.pmd.lang.rule.XPathRule"
      externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/unnecessary.html#UselessQualifiedThis">
    <description>Look for qualified this usages in the same class.</description>
    <priority>3</priority>
    <properties>
       <property name="xpath">
          <value>
          <![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name[@Image]]
[PrimarySuffix[@Arguments='false']]
[not(PrimarySuffix/MemberSelector)]
[ancestor::ClassOrInterfaceBodyDeclaration[1][@AnonymousInnerClass='false']]
/PrimaryPrefix/Name[@Image = ancestor::ClassOrInterfaceDeclaration[1]/@Image]
          ]]>
          </value>
       </property>
    </properties>
    <example>
    <![CDATA[
public class Foo {
    final Foo otherFoo = Foo.this;  // use "this" directly

    public void doSomething() {
         final Foo anotherFoo = Foo.this;  // use "this" directly
    }

    private ActionListener returnListener() {
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doSomethingWithQualifiedThis(Foo.this);  // This is fine
            }
        };
    }

    private class Foo3 {
        final Foo myFoo = Foo.this;  // This is fine
    }

    private class Foo2 {
        final Foo2 myFoo2 = Foo2.this;  // Use "this" direclty
    }
}
    ]]>
    </example>
  </rule>

</ruleset>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy