Since: PMD 0.1
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.
//CatchStatement [count(Block/BlockStatement) = 0 and ($allowCommentedBlocks != 'true' or Block/@containsComment = 'false')] [FormalParameter/Type/ReferenceType /ClassOrInterfaceType[@Image != 'InterruptedException' and @Image != 'CloneNotSupportedException'] ]
public void doSomething() { try { FileInputStream fis = new FileInputStream("/tmp/bugger"); } catch (IOException ioe) { // not good } }
This rule has the following properties:
Name | Default Value | Description |
---|---|---|
allowCommentedBlocks | false | Empty blocks containing comments will be skipped |
Since: PMD 0.1
Empty If Statement finds instances where a condition is checked but nothing is done about it.
//IfStatement/Statement [EmptyStatement or Block[count(*) = 0]]
public class Foo { void bar(int x) { if (x == 0) { // empty! } } }
Since: PMD 0.2
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.
//WhileStatement/Statement[./Block[count(*) = 0] or ./EmptyStatement]
void bar(int a, int b) { while (a == b) { // empty! } }
Since: PMD 0.4
Avoid empty try blocks - what's the point?
//TryStatement/Block[1][count(*) = 0]
public class Foo { public void bar() { try { } catch (Exception e) { e.printStackTrace(); } } }
Since: PMD 0.4
Empty finally blocks serve no purpose and should be removed.
//FinallyStatement[count(Block/BlockStatement) = 0]
public class Foo { public void bar() { try { int x=2; } finally { // empty! } } }
Since: PMD 1.0
Empty switch statements serve no purpose and should be removed.
//SwitchStatement[count(*) = 1]
public void bar() { int x = 2; switch (x) { // once there was code here // but it's been commented out or something } }
Since: PMD 1.3
Empty synchronized blocks serve no purpose and should be removed.
//SynchronizedStatement/Block[1][count(*) = 0]
public class Foo { public void bar() { synchronized (this) { // empty! } } }
Since: PMD 1.5
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.
//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) ]
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");; }
Since: PMD 5.0
Empty initializers serve no purpose and should be removed.
//Initializer/Block[count(*)=0]
public class Foo { static {} // Why ? {} // Again, why ? }