From de2a7e15ee139ca09bdc7801f43357547455d6c9 Mon Sep 17 00:00:00 2001 From: Andreas Bergander Date: Mon, 27 Nov 2023 15:35:06 +0100 Subject: [PATCH] Add allowCommentedBlocks property to EmptyControlStatementRule --- .../codestyle/EmptyControlStatementRule.java | 16 +++++++++++++- .../codestyle/xml/EmptyControlStatement.xml | 21 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java index 81faf170a8..00b626373b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java @@ -21,13 +21,25 @@ import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement; import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil; +import net.sourceforge.pmd.properties.PropertyDescriptor; +import net.sourceforge.pmd.properties.PropertyFactory; public class EmptyControlStatementRule extends AbstractJavaRulechainRule { + private static final PropertyDescriptor ALLOW_COMMENTED_BLOCKS + = PropertyFactory.booleanProperty("allowCommentedBlocks") + .desc("Option for allowing empty but commented blocks. This is useful where a developer " + + "wants to have the code structure and explain why a condition does not require " + + "logic or to hold TODO comments for future work.") + .defaultValue(Boolean.FALSE) + .build(); + public EmptyControlStatementRule() { super(ASTFinallyClause.class, ASTSynchronizedStatement.class, ASTTryStatement.class, ASTDoStatement.class, ASTBlock.class, ASTForStatement.class, ASTForeachStatement.class, ASTWhileStatement.class, ASTIfStatement.class, ASTSwitchStatement.class, ASTInitializer.class); + + definePropertyDescriptor(ALLOW_COMMENTED_BLOCKS); } @Override @@ -145,7 +157,9 @@ public class EmptyControlStatementRule extends AbstractJavaRulechainRule { } private boolean isEmpty(JavaNode node) { - return node instanceof ASTBlock && node.getNumChildren() == 0 + boolean allowCommentedBlocks = getProperty(ALLOW_COMMENTED_BLOCKS); + + return (node instanceof ASTBlock && node.getNumChildren() == 0 && !(((ASTBlock) node).containsComment() && allowCommentedBlocks)) || node instanceof ASTEmptyStatement; } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml index b0a5134918..528d6dd89f 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml @@ -228,7 +228,7 @@ } ]]> - + empty initializer failure case (non static) 1 @@ -502,7 +502,8 @@ public class Foo { - empty if statement with comment + empty if statement with comment, disallow commented block + false 1 Empty if statement @@ -516,5 +517,21 @@ public class Foo { } } ]]> + + + + empty if statement with comment, allow commented block + true + 0 + +