Merge branch 'pr-625'

This commit is contained in:
Juan Martín Sotuyo Dodero
2017-10-08 20:20:10 -03:00
12 changed files with 686 additions and 9 deletions

View File

@ -0,0 +1,176 @@
---
title: Empty Code
summary: The Empty Code ruleset contains rules that find empty statements of any kind (empty method, empty block statement, empty try or catch block,...).
permalink: pmd_rules_apex_empty.html
folder: pmd/rules/apex
sidebaractiveurl: /pmd_rules_apex.html
editmepath: ../pmd-apex/src/main/resources/rulesets/apex/empty.xml
keywords: Empty Code, EmptyCatchBlock, EmptyIfStmt, EmptyWhileStmt, EmptyTryOrFinallyBlock
---
## EmptyCatchBlock
**Since:** PMD 6.0.0
**Priority:** Medium (3)
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.
```
//CatchBlockStatement
[./BlockStatement[count(*) = 0]]
```
**Example(s):**
``` java
public void doSomething() {
...
try {
insert accounts;
} catch (DmlException dmle) {
// not good
}
}
```
**Use this rule by referencing it:**
``` xml
<rule ref="rulesets/apex/empty.xml/EmptyCatchBlock" />
```
## EmptyIfStmt
**Since:** PMD 6.0.0
**Priority:** Medium (3)
Empty If Statement finds instances where a condition is checked but nothing is done about it.
```
//IfBlockStatement
[BlockStatement[count(*) = 0]]
```
**Example(s):**
``` java
public class Foo {
void bar(Integer x) {
if (x == 0) {
// empty!
}
}
}
```
**Use this rule by referencing it:**
``` xml
<rule ref="rulesets/apex/empty.xml/EmptyIfStmt" />
```
## EmptyTryOrFinallyBlock
**Since:** PMD 6.0.0
**Priority:** Medium (3)
Avoid empty try or finally blocks - what's the point?
```
//TryCatchFinallyBlockStatement[./BlockStatement[count(*) = 0]]
```
**Example(s):**
``` java
public class Foo {
public void bar() {
try {
// empty !
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Foo {
public void bar() {
try {
int x=2;
} catch (Exception e) {
system.debug(e):
} finally {
// empty!
}
}
}
```
**Use this rule by referencing it:**
``` xml
<rule ref="rulesets/apex/empty.xml/EmptyTryOrFinallyBlock" />
```
## EmptyWhileStmt
**Since:** PMD 6.0.0
**Priority:** Medium (3)
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.
```
//WhileLoopStatement[./BlockStatement[count(*) = 0]]
```
**Example(s):**
``` java
public class Foo {
public void bar(Integer a, Integer b) {
while (a == b) {
// empty!
}
}
}
```
**Use this rule by referencing it:**
``` xml
<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

@ -143,6 +143,14 @@ Notice this last scenario is slightly different to the Java syntax. This is due
which can produce bugs by iether accessing non-existing indexes, or them leaving out. You should use for-each-loops
instead.
* A whole new ruleset has been added to Apex, `apex-empty`. It currently migrates 5 rules from the equivalent
`java-empty` ruleset for Apex. The ruleset includes:
* `EmptyCatchBlock` to detect catch blocks completely ignoring exceptions.
* `EmptyIfStmt` for if blocks with no content, that can be safely removed.
* `EmptyTryOrFinallyBlock` for empty try / finally blocks that can be safely removed.
* `EmptyWhileStmt` for empty while loops that can be safely removed.
* `EmptyStatementBlock` for empty code blocks that can be safely removed.
#### Modified Rules
* The rule `UnnecessaryFinalModifier` (ruleset `java-unnecessarycode`) has been revamped to detect more cases.
@ -365,6 +373,7 @@ a warning will now be produced suggesting users to adopt it for better performan
* [#598](https://github.com/pmd/pmd/pull/598): \[java] Fix #388: controversial.AvoidLiteralsInIfCondition 0.0 false positive - [Clément Fournier](https://github.com/oowekyala)
* [#602](https://github.com/pmd/pmd/pull/602): \[java] \[apex] Separate multifile analysis from metrics - [Clément Fournier](https://github.com/oowekyala)
* [#620](https://github.com/pmd/pmd/pull/620): \[core] Moved properties to n.s.pmd.properties - [Clément Fournier](https://github.com/oowekyala)
* [#625](https://github.com/pmd/pmd/pull/625): \[apex] empty code ruleset for apex - [Jan Aertgeerts](https://github.com/JAertgeerts)
* [#632](https://github.com/pmd/pmd/pull/632): \[apex] Add AvoidDirectAccessTriggerMap rule to the style set - [Jan Aertgeerts](https://github.com/JAertgeerts)
* [#644](https://github.com/pmd/pmd/pull/644): \[core] Prevent internal dev-properties from being displayed on CodeClimate renderer - [Filipe Esperandio](https://github.com/filipesperandio)

View File

@ -0,0 +1,192 @@
<?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="apex"
since="6.0.0"
message="Avoid empty catch blocks"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_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[
//CatchBlockStatement[./BlockStatement[count(*) = 0]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public void doSomething() {
...
try {
insert accounts;
} catch (DmlException dmle) {
// not good
}
}
]]>
</example>
</rule>
<rule name="EmptyIfStmt"
language="apex"
since="6.0.0"
message="Avoid empty 'if' statements"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_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[
//IfBlockStatement
[BlockStatement[count(*) = 0]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar(Integer x) {
if (x == 0) {
// empty!
}
}
}
]]>
</example>
</rule>
<rule name="EmptyTryOrFinallyBlock"
language="apex"
since="6.0.0"
message="Avoid empty try or finally blocks"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_empty.html#emptytryorfinallyblock">
<description>
Avoid empty try or finally blocks - what's the point?
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//TryCatchFinallyBlockStatement[./BlockStatement[count(*) = 0]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
try {
// empty !
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Foo {
public void bar() {
try {
int x=2;
} finally {
// empty!
}
}
}
]]>
</example>
</rule>
<rule name="EmptyWhileStmt"
language="apex"
since="6.0.0"
message="Avoid empty 'while' statements"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_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[
//WhileLoopStatement[./BlockStatement[count(*) = 0]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public void bar(Integer a, Integer b) {
while (a == b) {
// empty!
}
}
]]>
</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

@ -299,38 +299,84 @@
<!-- BRACES -->
<rule ref="rulesets/apex/braces.xml/IfStmtsMustUseBraces" message="Avoid using if statements without curly braces">
<priority>3</priority>
<properties>
<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>
</properties>
</rule>
<rule ref="rulesets/apex/braces.xml/WhileLoopsMustUseBraces" message="Avoid using 'while' statements without curly braces">
<priority>3</priority>
<properties>
<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>
</properties>
</rule>
<rule ref="rulesets/apex/braces.xml/IfElseStmtsMustUseBraces" message="Avoid using 'if...else' statements without curly braces">
<priority>3</priority>
<properties>
<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>
</properties>
</rule>
<rule ref="rulesets/apex/braces.xml/ForLoopsMustUseBraces" message="Avoid using 'for' statements without curly braces">
<priority>3</priority>
<properties>
<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>
</properties>
</rule>
</ruleset>
<!-- EMPTY -->
<rule ref="rulesets/apex/empty.xml/EmptyCatchBlock" message="Avoid empty catch blocks">
<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>
<rule ref="rulesets/apex/empty.xml/EmptyIfStmt" message="Avoid empty 'if' 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>
<rule ref="rulesets/apex/empty.xml/EmptyWhileStmt" message="Avoid empty 'while' 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>
<rule ref="rulesets/apex/empty.xml/EmptyTryOrFinallyBlock" message="Avoid empty try or finally blocks">
<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>
<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

@ -6,6 +6,7 @@ rulesets.filenames=\
rulesets/apex/apexunit.xml,\
rulesets/apex/braces.xml,\
rulesets/apex/complexity.xml,\
rulesets/apex/empty.xml,\
rulesets/apex/performance.xml,\
rulesets/apex/security.xml,\
rulesets/apex/style.xml

View File

@ -0,0 +1,21 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.rule.empty;
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class EmptyRulesTest extends SimpleAggregatorTst {
private static final String RULESET = "apex-empty";
@Override
public void setUp() {
addRule(RULESET, "EmptyCatchBlock");
addRule(RULESET, "EmptyIfStmt");
addRule(RULESET, "EmptyTryOrFinallyBlock");
addRule(RULESET, "EmptyWhileStmt");
addRule(RULESET, "EmptyStatementBlock");
}
}

View File

@ -0,0 +1,40 @@
<?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 Catch Block
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void bar() {
try {
system.debug(1);
}
catch(Exception e) {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success case: Empty Catch Block
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
try {
system.debug(1);
} catch(Exception e) {
system.debug(e);
}
}
}
]]></code>
</test-code>
</test-data>

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 If Statement
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
if(true) {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success Case: Empty If statement
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
if(true) {
system.debug(true);
}
}
}
]]></code>
</test-code>
</test-data>

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>

View File

@ -0,0 +1,78 @@
<?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 Try Block
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
try {
}
catch(Exception e) {
system.debug(e);
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success Case: Empty Try Block
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
try {
insert account;
} catch(Exception e) {
system.debug(e);
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Failure Case: Empty Finally Block
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
try {
insert account;
} catch(Exception e) {
// Generic exception handling code
system.debug(e);
} finally {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success Case: Empty Finally Block
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
try {
insert account;
} catch(Exception e) {
system.debug(e);
} finally {
someAction();
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -0,0 +1,39 @@
<?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 While Statement
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
Integer count = 1;
while (count < 11) {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Success Case: Empty While Statement
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void foo() {
Integer count = 1;
while (count < 11) {
System.debug(count);
count++;
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -12,5 +12,10 @@ This ruleset contains links to rules that are new in PMD v6.0.0
<rule ref="rulesets/java/migrating.xml/ForLoopCanBeForeach"/>
<rule ref="rulesets/apex/style.xml/AvoidDirectAccessTriggerMap"/>
<rule ref="rulesets/apex/empty.xml/EmptyCatchBlock"/>
<rule ref="rulesets/apex/empty.xml/EmptyIfStmt"/>
<rule ref="rulesets/apex/empty.xml/EmptyTryOrFinallyBlock"/>
<rule ref="rulesets/apex/empty.xml/EmptyWhileStmt"/>
<rule ref="rulesets/apex/empty.xml/EmptyStatementBlock"/>
</ruleset>