[java] UselessOperationOnImmutable: false positive on void results

This commit is contained in:
Andreas Dangel
2024-10-24 13:08:09 +02:00
parent 63675eaa18
commit 36654ed52e
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -318,6 +318,20 @@ class Tester {
period = period.plusWeeks(1); // ok
}
}
]]></code>
</test-code>
<test-code>
<description>False positive for package private method calls in openjdk</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package java.time;
import java.io.ObjectOutput;
class Tester {
static void writeExternal(ZonedDateTime zonedDateTime, ObjectOutput out) {
zonedDateTime.writeExternal(out); // false positive
}
}
]]></code>
</test-code>
</test-data>