From 6524ae9d3340452c760ede76bbe2f1f01129beb3 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 27 May 2022 17:16:02 +0200 Subject: [PATCH 1/3] [java] Rename ASTResource#getName to getStableName --- .../java/net/sourceforge/pmd/lang/java/ast/ASTResource.java | 2 +- .../pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java index b9aafd7f83..9aab09fe1d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java @@ -25,7 +25,7 @@ public class ASTResource extends ASTFormalParameter { return visitor.visit(this, data); } - public String getName() { + public String getStableName() { ASTVariableDeclaratorId variableDeclaratorId = getVariableDeclaratorId(); if (variableDeclaratorId != null) { return variableDeclaratorId.getName(); 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 c9b075f067..69533488e5 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 @@ -132,7 +132,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { if (resources != null) { for (ASTResource resource : resources.findDescendantsOfType(ASTResource.class)) { hasResource = true; - String name = resource.getName(); + String name = resource.getStableName(); if (!JavaRuleUtil.isExplicitUnusedVarName(name)) { allResourcesIgnored = false; break; From 02581bcc4a20e0ae7dad78b588b97c80d2f6cded Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 27 May 2022 17:36:01 +0200 Subject: [PATCH 2/3] [java] EmptyControlStatementRule - fix messages --- .../codestyle/EmptyControlStatementRule.java | 28 ++++++++++--------- .../resources/category/java/codestyle.xml | 2 +- 2 files changed, 16 insertions(+), 14 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 69533488e5..0a6c954d7a 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 @@ -18,6 +18,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatement; import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement; import net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement; import net.sourceforge.pmd.lang.java.ast.ASTTryStatement; +import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement; import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; @@ -46,7 +47,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTFinallyStatement node, Object data) { if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty finally clause"); + asCtx(data).addViolationWithMessage(node, "Empty finally clause"); } return null; } @@ -54,7 +55,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTSynchronizedStatement node, Object data) { if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty synchronized statement"); + asCtx(data).addViolationWithMessage(node, "Empty synchronized statement"); } return null; } @@ -62,7 +63,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTSwitchStatement node, Object data) { if (node.getNumChildren() == 1) { - addViolation(data, node, "Empty switch statement"); + asCtx(data).addViolationWithMessage(node, "Empty switch statement"); } return null; } @@ -70,7 +71,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTBlock node, Object data) { if (isEmpty(node) && node.getNthParent(3) instanceof ASTBlock) { - addViolation(data, node, "Empty block"); + asCtx(data).addViolationWithMessage(node, "Empty block"); } return null; } @@ -78,10 +79,10 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTIfStatement node, Object data) { if (isEmpty(node.getThenBranch().getChild(0))) { - addViolation(data, node, "Empty if statement"); + asCtx(data).addViolationWithMessage(node, "Empty if statement"); } if (node.hasElse() && isEmpty(node.getElseBranch().getChild(0))) { - addViolation(data, node.getElseBranch(), "Empty else statement"); + asCtx(data).addViolationWithMessage(node.getElseBranch(), "Empty else statement"); } return null; } @@ -89,19 +90,20 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTWhileStatement node, Object data) { if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty while statement"); + asCtx(data).addViolationWithMessage(node, "Empty while statement"); } return null; } @Override public Object visit(ASTForStatement node, Object data) { - if (node.isForeach() && JavaRuleUtil.isExplicitUnusedVarName(node.getFirstChildOfType(ASTLocalVariableDeclaration.class).getVariableName())) { + if (node.isForeach() && JavaRuleUtil.isExplicitUnusedVarName(node.getFirstChildOfType(ASTLocalVariableDeclaration.class) + .getFirstDescendantOfType(ASTVariableDeclaratorId.class).getName())) { // allow `for (ignored : iterable) {}` return null; } if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty for statement"); + asCtx(data).addViolationWithMessage(node, "Empty for statement"); } return null; } @@ -109,7 +111,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTDoStatement node, Object data) { if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty do..while statement"); + asCtx(data).addViolationWithMessage(node, "Empty do..while statement"); } return null; } @@ -117,7 +119,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { @Override public Object visit(ASTInitializer node, Object data) { if (isEmpty(node.getBody())) { - addViolation(data, node, "Empty initializer statement"); + asCtx(data).addViolationWithMessage(node, "Empty initializer statement"); } return null; } @@ -141,9 +143,9 @@ public class EmptyControlStatementRule extends AbstractJavaRule { } if (hasResource && !allResourcesIgnored) { - addViolation(data, node, "Empty try body - you could rename the resource to 'ignored'"); + asCtx(data).addViolationWithMessage(node, "Empty try body - you could rename the resource to 'ignored'"); } else if (!hasResource) { - addViolation(data, node, "Empty try body"); + asCtx(data).addViolationWithMessage(node, "Empty try body"); } } return null; diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 8096ce8309..f9770a891b 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -2192,7 +2192,7 @@ public class Foo { 3 - + Date: Fri, 27 May 2022 17:59:29 +0200 Subject: [PATCH 3/3] [java] EmptyControlStatementRule - fix messages --- .../codestyle/EmptyControlStatementRule.java | 2 +- .../codestyle/xml/EmptyControlStatement.xml | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) 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 0a6c954d7a..5e7f02f60f 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 @@ -143,7 +143,7 @@ public class EmptyControlStatementRule extends AbstractJavaRule { } if (hasResource && !allResourcesIgnored) { - asCtx(data).addViolationWithMessage(node, "Empty try body - you could rename the resource to 'ignored'"); + asCtx(data).addViolationWithMessage(node, "Empty try body - you could rename the resource to ''ignored''"); } else if (!hasResource) { asCtx(data).addViolationWithMessage(node, "Empty try body"); } 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 06a5c97099..c732f1ef5b 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 @@ -7,6 +7,9 @@ pos, empty try block 1 3 + + Empty try body + pos, empty try block 1 3 + + Empty try body + pos, empty finally block 1 5 + + Empty finally clause + pos, empty try and finally block 2 + + Empty try body + Empty finally clause + #432 empty try-with-resource - not ok 1 + + Empty try body - you could rename the resource to 'ignored' + empty concise try-with-resource - not ok 1 4 + + Empty try body - you could rename the resource to 'ignored' + pos, empty synchronized stmt 1 + + Empty synchronized statement + pos, empty switch stmt 1 + + Empty switch statement + pos, empty block 1 + + Empty block + empty initializer failure case (non static) 1 + + Empty initializer statement + empty initializer failure case (static) 1 + + Empty initializer statement + not an initializer - empty statement block 1 + + Empty block + pos, empty for 2 3,5 + + Empty for statement + Empty for statement + pos, empty do..while 2 4,6 + + Empty do..while statement + Empty do..while statement + pos, empty foreach 2 6,8 + + Empty for statement + Empty for statement + pos, empty while 1 3 + + Empty while statement + while(true); 1 + + Empty while statement + one empty if statement 1 3 + + Empty if statement + empty if with else statement 2 3,4 + + Empty if statement + Empty else statement + empty if with else and else if statement 3 3,4,5 + + Empty if statement + Empty if statement + Empty else statement + empty if statement 1 + + Empty if statement + empty if statement with comment 1 + + Empty if statement +