diff --git a/docs/pages/pmd/rules/apex/empty.md b/docs/pages/pmd/rules/apex/empty.md new file mode 100644 index 0000000000..c185ad1fae --- /dev/null +++ b/docs/pages/pmd/rules/apex/empty.md @@ -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 + +``` + +## 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 + +``` + +## 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 + +``` + +## 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 + +``` + +## 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 + +``` diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index dc2ffb2636..8dd4bee8ac 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -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) diff --git a/pmd-apex/src/main/resources/rulesets/apex/empty.xml b/pmd-apex/src/main/resources/rulesets/apex/empty.xml new file mode 100644 index 0000000000..497d065fe1 --- /dev/null +++ b/pmd-apex/src/main/resources/rulesets/apex/empty.xml @@ -0,0 +1,192 @@ + + + + +The Empty Code ruleset contains rules that find empty statements of any kind (empty method, +empty block statement, empty try or catch block,...). + + + + +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. + + 3 + + + + + + + + + + + + + + +Empty If Statement finds instances where a condition is checked but nothing is done about it. + + 3 + + + + + + + + + + + + + + + +Avoid empty try or finally blocks - what's the point? + + 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. + + 3 + + + + + + + + + + + + + + + +Empty block statements serve no purpose and should be removed. + + 3 + + + + + + + + + + + + diff --git a/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml b/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml index 6d15c33a35..f6c4e0066b 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml @@ -299,38 +299,84 @@ 3 - + - + 3 - + - + 3 - + - + 3 - + - + - \ No newline at end of file + + + 3 + + + + + + + + + 3 + + + + + + + + + 3 + + + + + + + + + 3 + + + + + + + + + 3 + + + + + + + + diff --git a/pmd-apex/src/main/resources/rulesets/apex/rulesets.properties b/pmd-apex/src/main/resources/rulesets/apex/rulesets.properties index 879f524f1d..61d607e55d 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/rulesets.properties +++ b/pmd-apex/src/main/resources/rulesets/apex/rulesets.properties @@ -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 diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/empty/EmptyRulesTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/empty/EmptyRulesTest.java new file mode 100644 index 0000000000..3ece406df7 --- /dev/null +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/empty/EmptyRulesTest.java @@ -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"); + } +} diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyCatchBlock.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyCatchBlock.xml new file mode 100644 index 0000000000..2a82791147 --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyCatchBlock.xml @@ -0,0 +1,40 @@ + + + + + 1 + + + + + 0 + + + diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyIfStmt.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyIfStmt.xml new file mode 100644 index 0000000000..00b5ba0560 --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyIfStmt.xml @@ -0,0 +1,35 @@ + + + + + 1 + + + + + 0 + + + diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyStatementBlock.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyStatementBlock.xml new file mode 100644 index 0000000000..d0fd844c87 --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyStatementBlock.xml @@ -0,0 +1,35 @@ + + + + + 1 + + + + + 0 + + + diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyTryOrFinallyBlock.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyTryOrFinallyBlock.xml new file mode 100644 index 0000000000..74700f4dd1 --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyTryOrFinallyBlock.xml @@ -0,0 +1,78 @@ + + + + + 1 + + + + + 0 + + + + + 1 + + + + + 0 + + + diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyWhileStmt.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyWhileStmt.xml new file mode 100644 index 0000000000..85c98fee7b --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/empty/xml/EmptyWhileStmt.xml @@ -0,0 +1,39 @@ + + + + + 1 + + + + + 0 + + + diff --git a/pmd-core/src/main/resources/rulesets/releases/600.xml b/pmd-core/src/main/resources/rulesets/releases/600.xml index e596b506cf..95a628651c 100644 --- a/pmd-core/src/main/resources/rulesets/releases/600.xml +++ b/pmd-core/src/main/resources/rulesets/releases/600.xml @@ -12,5 +12,10 @@ This ruleset contains links to rules that are new in PMD v6.0.0 + + + + +