diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 64bddd15e1..7b7db2bcf5 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,6 +1,7 @@ ????? ??, 2013 - 5.0.5: Fixed bug 991: AvoidSynchronizedAtMethodLevel for static methods +Fixed bug 1104: IdempotentOperation false positive Fixed bug 1111: False positive: Useless parentheses Fixed bug 1114: CPD - Tokenizer not initialized with requested properties Fixed bug 1118: ClassCastException in pmd.lang.ecmascript.ast.ASTElementGet diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/IdempotentOperationsRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/IdempotentOperationsRule.java index 23bf812e4f..2e3e6ba0bb 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/IdempotentOperationsRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/design/IdempotentOperationsRule.java @@ -3,6 +3,8 @@ */ package net.sourceforge.pmd.lang.java.rule.design; +import java.util.List; + import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator; import net.sourceforge.pmd.lang.java.ast.ASTExpression; @@ -59,6 +61,21 @@ public class IdempotentOperationsRule extends AbstractJavaRule { return super.visit(node, data); } + List lhsSuffixes = lhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class); + List rhsSuffixes = rhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class); + if (lhsSuffixes.size() != rhsSuffixes.size()) { + return super.visit(node, data); + } + + for (int i = 0; i < lhsSuffixes.size(); i++) { + ASTPrimarySuffix l = lhsSuffixes.get(i); + ASTPrimarySuffix r = rhsSuffixes.get(i); + + if (!l.hasImageEqualTo(r.getImage())) { + return super.visit(node, data); + } + } + addViolation(data, node); return super.visit(node, data); } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/IdempotentOperations.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/IdempotentOperations.xml index 29f187930c..7895d56946 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/IdempotentOperations.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/IdempotentOperations.xml @@ -61,4 +61,16 @@ public class Foo { } ]]> + + #1104 IdempotentOperation false positive + 0 + +