[apex] add empty statement block rule

This commit is contained in:
Jan Aertgeerts
2017-09-19 10:21:55 +02:00
parent a890dbf68c
commit 8bc6810196
5 changed files with 110 additions and 0 deletions

View File

@ -144,3 +144,33 @@ public class Foo {
<rule ref="rulesets/apex/empty.xml/EmptyWhileStmt" />
```
## EmptyStatementBlock
**Since:** PMD 6.0.0
**Priority:** Medium (3)
Empty block statements serve no purpose and should be removed.
```
//Method/ModifierNode[@Abstract!='true' and ../BlockStatement[count(*) = 0]]
| //Method/BlockStatement//BlockStatement[count(*) = 0]```
**Example(s):**
``` java
public class Foo {
public void setBar(int bar) {
// empty, not allowed
}
public abstract void foo() {
// this is allowed
}
}
```
**Use this rule by referencing it:**
``` xml
<rule ref="rulesets/apex/empty.xml/EmptyStatementBlock" />
```

View File

@ -154,4 +154,39 @@ public void bar(Integer a, Integer b) {
</example>
</rule>
<rule name="EmptyStatementBlock"
language="apex"
since="6.0.0"
message="Avoid empty block statements."
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_empty.html#emptystatementblock">
<description>
Empty block statements serve no purpose and should be removed.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Method/ModifierNode[@Abstract!='true' and ../BlockStatement[count(*) = 0]]
| //Method/BlockStatement//BlockStatement[count(*) = 0]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
private int _bar;
public void setBar(int bar) {
// empty
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -361,4 +361,13 @@
<property name="cc_block_highlighting" value="false" />
</properties>
</rule>
<rule ref="rulesets/apex/empty.xml/EmptyStatementBlock" message="Avoid empty block statements.">
<priority>3</priority>
<properties>
<!-- relevant for Code Climate output only -->
<property name="cc_categories" value="Style" />
<property name="cc_remediation_points_multiplier" value="5" />
<property name="cc_block_highlighting" value="false" />
</properties>
</rule>
</ruleset>

View File

@ -16,5 +16,6 @@ public class EmptyRulesTest extends SimpleAggregatorTst {
addRule(RULESET, "EmptyIfStmt");
addRule(RULESET, "EmptyTryOrFinallyBlock");
addRule(RULESET, "EmptyWhileStmt");
addRule(RULESET, "EmptyStatementBlock");
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description><![CDATA[
Failure case: Empty Statement Block
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
}
public abstract void bar() {}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success case: Empty Statement Block
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
system.debug(1);
}
}
public abstract void bar() {}
}
]]></code>
</test-code>
</test-data>