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

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

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

<ruleset name="Empty Code"
    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 Empty Code ruleset contains rules that find empty statements of any kind (empty method,
empty block statement, empty try or catch block,...).
  </description>

 <rule name="EmptyCatchBlock"
  		  language="java"
  	  	  since="0.1"
          message="Avoid empty catch blocks"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyCatchBlock">
      <description>
Empty Catch Block finds instances where an exception is caught, but nothing is done.  
In most circumstances, this swallows an exception which should either be acted on 
or reported.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
    <![CDATA[
//CatchStatement
 [count(Block/BlockStatement) = 0 and ($allowCommentedBlocks != 'true' or Block/@containsComment = 'false')]
 [FormalParameter/Type/ReferenceType
   /ClassOrInterfaceType[@Image != 'InterruptedException' and @Image != 'CloneNotSupportedException']
 ]
 ]]>
             </value>
          </property>
          <property name="allowCommentedBlocks" type="Boolean" description="Empty blocks containing comments will be skipped" value="false"/>
      </properties>
      <example>
  <![CDATA[
public void doSomething() {
  try {
    FileInputStream fis = new FileInputStream("/tmp/bugger");
  } catch (IOException ioe) {
      // not good
  }
}
 ]]>
      </example>
    </rule>

    <rule name="EmptyIfStmt"
    		 language="java"
    		 since="0.1"
          message="Avoid empty 'if' statements"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyIfStmt">
      <description>
Empty If Statement finds instances where a condition is checked but nothing is done about it.
    </description>
        <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//IfStatement/Statement
 [EmptyStatement or Block[count(*) = 0]]
 ]]>
              </value>
          </property>
      </properties>
      <example>
    <![CDATA[
public class Foo {
 void bar(int x) {
  if (x == 0) {
   // empty!
  }
 }
}
 ]]>
       </example>
    </rule>


    <rule name="EmptyWhileStmt"
    		 language="java"
    		 since="0.2"
          message="Avoid empty 'while' statements"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyWhileStmt">
       <description>
Empty While Statement finds all instances where a while statement does nothing.  
If it is a timing loop, then you should use Thread.sleep() for it; if it is
a while loop that does a lot in the exit expression, rewrite it to make it clearer.
       </description>
       <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//WhileStatement/Statement[./Block[count(*) = 0]  or ./EmptyStatement]
]]>
              </value>
          </property>
      </properties>
       <example>
  <![CDATA[
void bar(int a, int b) {
	while (a == b) {
	// empty!
	}
}
 ]]>
       </example>
    </rule>


    <rule name="EmptyTryBlock"
    		 language="java"
    		 since="0.4"
          message="Avoid empty try blocks"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyTryBlock">
      <description>
Avoid empty try blocks - what's the point?
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//TryStatement/Block[1][count(*) = 0]
]]>
              </value>
          </property>
      </properties>
      <example>
  <![CDATA[
public class Foo {
 public void bar() {
  try {
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
}
]]>
      </example>
    </rule>

    <rule name="EmptyFinallyBlock"
    		 language="java"
    		 since="0.4"
          message="Avoid empty finally blocks"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyFinallyBlock">
      <description>
Empty finally blocks serve no purpose and should be removed.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//FinallyStatement[count(Block/BlockStatement) = 0]
 ]]>
              </value>
          </property>
      </properties>
      <example>
  <![CDATA[
public class Foo {
 public void bar() {
  try {
    int x=2;
   } finally {
    // empty!
   }
 }
}
 ]]>
      </example>
    </rule>


    <rule name="EmptySwitchStatements"
    		 language="java"
    		 since="1.0"
          message="Avoid empty switch statements"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptySwitchStatements">
      <description>
Empty switch statements serve no purpose and should be removed.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//SwitchStatement[count(*) = 1]
 ]]>
              </value>
          </property>
      </properties>
      <example>
  <![CDATA[
public void bar() {
	int x = 2;
	switch (x) {
	// once there was code here
	// but it's been commented out or something
	}
}
]]>
      </example>
      </rule>

    <rule name="EmptySynchronizedBlock"
    		 language="java"
    		 since="1.3"
          message="Avoid empty synchronized blocks"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptySynchronizedBlock">
      <description>
Empty synchronized blocks serve no purpose and should be removed.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value>
<![CDATA[
//SynchronizedStatement/Block[1][count(*) = 0]
]]>
              </value>
          </property>
      </properties>
      <example>
<![CDATA[
public class Foo {
 public void bar() {
  synchronized (this) {
   // empty!
  }
 }
}
]]>
      </example>
    </rule>


    <rule name="EmptyStatementNotInLoop"
    		 language="java"
    		 since="1.5"
          message="An empty statement (semicolon) not part of a loop"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyStatementNotInLoop">
       <description>
An empty statement (or a semicolon by itself) that is not used as the sole body of a 'for' 
or 'while' loop is probably a bug.  It could also be a double semicolon, which has no purpose
and should be removed.
       </description>
       <priority>3</priority>
        <properties>
            <property name="xpath">
                <value>
<![CDATA[
//EmptyStatement
 [not(
       ../../../ForStatement
       or ../../../WhileStatement
       or ../../../BlockStatement/ClassOrInterfaceDeclaration
       or ../../../../../../ForStatement/Statement[1]
        /Block[1]/BlockStatement[1]/Statement/EmptyStatement
       or ../../../../../../WhileStatement/Statement[1]
        /Block[1]/BlockStatement[1]/Statement/EmptyStatement)
 ]
]]>
                </value>
            </property>
        </properties>
       <example>
<![CDATA[
public void doit() {
      // this is probably not what you meant to do
      ;
      // the extra semicolon here this is not necessary
      System.out.println("look at the extra semicolon");;
}
]]>
       </example>
     </rule>

<rule 	name="EmptyInitializer"
		    language="java"
	  		since="5.0"   	
          	message="Empty initializer was found"
          	class="net.sourceforge.pmd.lang.rule.XPathRule"
          	externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyInitializer">
       <description>
Empty initializers serve no purpose and should be removed.
       </description>
       <priority>3</priority>
         <properties>
             <property name="xpath">
                 <value>
<![CDATA[
//Initializer/Block[count(*)=0]
]]>
                 </value>
             </property>
         </properties>
       <example>
   <![CDATA[
public class Foo {

   static {} // Why ?

   {} // Again, why ?

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

  <rule name="EmptyStatementBlock"
        language="java"
        since="5.0"
        message="Avoid empty block statements."
        class="net.sourceforge.pmd.lang.rule.XPathRule"
	externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyStatementBlock">
    <description>
Empty block statements serve no purpose and should be removed.
	</description>
    <priority>3</priority>
    <properties>
       <property name="xpath">
          <value>
          <![CDATA[
//BlockStatement/Statement/Block[count(*) = 0]
          ]]>
          </value>
       </property>
    </properties>
    <example>
    <![CDATA[
public class Foo {

   private int _bar;

   public void setBar(int bar) {
      { _bar = bar; } // Why not?
      {} // But remove this.
   }

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

    <rule name="EmptyStaticInitializer"
    		 language="java"
    		  since="1.5"
           message="Empty static initializer was found"
           class="net.sourceforge.pmd.lang.rule.XPathRule"
           externalInfoUrl="https://pmd.github.io/pmd-5.4.1/pmd-java/rules/java/empty.html#EmptyStaticInitializer">
       <description>
An empty static initializer serve no purpose and should be removed.
       </description>
       <priority>3</priority>
         <properties>
             <property name="xpath">
                 <value>
<![CDATA[
//Initializer[@Static='true']/Block[count(*)=0]
]]>
                 </value>
             </property>
         </properties>
       <example>
   <![CDATA[
public class Foo {
	static {
	// empty
	}
}
]]>
       </example>
     </rule>

</ruleset>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy