Affects PMD Version: 6.39.0
Rule: EmptyStatementBlock
Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd-6.39.0/pmd_rules_apex_errorprone.html#emptystatementblock
Description:
There are a few situations where empty blocks are actually expected and even support a best practices approach to the problem being solved, e.g.:
private
no-arg constructors in classes which should never be (externally) instantiated, e.g., singletons and utility classesvirtual
methods in abstract
base classes where partial overrides are the normOptions should be added to the EmptyStatementBlock
rule to allow these specific types of instances to be ignored.
Code Sample demonstrating the issue:
Singleton
public with sharing class MySingleton { public static final MySingleton INSTANCE = new MySingleton(); // This gets reported as an empty block, but it's critical to have a private constructor even // when there's no constructor logic private MySingleton() { // Singleton } }
Utility class
public with sharing class CollectionUtil { // This gets reported as an empty block, but it's critical to have a private constructor even // when there's no constructor logic private CollectionUtil() { // Utility class } public static Boolean isEmpty(List<Object> values) { ... } public static Boolean isNotEmpty(List<Object> values) { ... } ... }
Abstract base class
public abstract with sharing class AbstractTriggerHandler { // All of these are reported as empty blocks, but concrete subclasses will generally only override // a subset of these methods, so providing default no-op implementations is critical public virtual void beforeInsert() {} public virtual void afterInsert() {} public virtual void beforeUpdate() {} public virtual void afterUpdate() {} ... }
Expected outcome:
I imagine for backward-compatibility, new options would be added for each of these categorical exceptions that default to the current behavior if unspecified, e.g.:
<rule ref="category/apex/documentation.xml/EmptyStatementBlock" message="..."> <priority>3</priority> <properties> <property name="reportEmptyPrivateNoArgConstructor" value="false" /> <property name="reportEmptyVirtualMethod" value="false" /> </properties> </rule>
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
CLI and direct API-based integration
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4