From 36654ed52ec9b8b5e9087ac523563dff6a2ea09f Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 24 Oct 2024 13:08:09 +0200 Subject: [PATCH] [java] UselessOperationOnImmutable: false positive on void results --- .../UselessOperationOnImmutableRule.java | 3 ++- .../errorprone/xml/UselessOperationOnImmutable.xml | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessOperationOnImmutableRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessOperationOnImmutableRule.java index c326446c5e..89434b830d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessOperationOnImmutableRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessOperationOnImmutableRule.java @@ -30,7 +30,8 @@ public class UselessOperationOnImmutableRule extends AbstractJavaRulechainRule { @Override public Object visit(ASTMethodCall node, Object data) { ASTExpression qualifier = node.getQualifier(); - if (node.getParent() instanceof ASTExpressionStatement && qualifier != null) { + boolean returnsVoid = node.getTypeMirror().isVoid(); + if (node.getParent() instanceof ASTExpressionStatement && qualifier != null && !returnsVoid) { // these types are immutable, so any method of those whose // result is ignored is a violation diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessOperationOnImmutable.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessOperationOnImmutable.xml index b311af77f0..35ee48f112 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessOperationOnImmutable.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessOperationOnImmutable.xml @@ -318,6 +318,20 @@ class Tester { period = period.plusWeeks(1); // ok } } +]]> + + + + False positive for package private method calls in openjdk + 0 +