1 package net.sourceforge.pmd.rules;
2
3 import net.sourceforge.pmd.AbstractRule;
4 import net.sourceforge.pmd.RuleContext;
5 import net.sourceforge.pmd.ast.ASTAssignmentOperator;
6 import net.sourceforge.pmd.ast.ASTExpression;
7 import net.sourceforge.pmd.ast.ASTName;
8 import net.sourceforge.pmd.ast.ASTPrimaryExpression;
9 import net.sourceforge.pmd.ast.ASTStatementExpression;
10 import net.sourceforge.pmd.ast.SimpleNode;
11
12 public class IdempotentOperationsRule extends AbstractRule {
13
14 public Object visit(ASTStatementExpression node, Object data) {
15 if (node.jjtGetNumChildren() != 3
16 || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression)
17 || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator)
18 || !(node.jjtGetChild(2) instanceof ASTExpression)
19 ) {
20 return super.visit(node, data);
21 }
22
23 SimpleNode lhs = (SimpleNode)node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
24 if (!(lhs instanceof ASTName)) {
25 return super.visit(node, data);
26 }
27
28 SimpleNode rhs = (SimpleNode)node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
29 if (!(rhs instanceof ASTName)) {
30 return super.visit(node, data);
31 }
32
33 if (!lhs.getImage().equals(rhs.getImage())) {
34 return super.visit(node, data);
35 }
36
37 RuleContext ctx = (RuleContext) data;
38 ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine(), "Avoid idempotent operations"));
39 return data;
40 }
41 }
This page was automatically generated by Maven