|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.rules; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.AbstractRule; |
|
7 |
| import net.sourceforge.pmd.ast.ASTAssignmentOperator; |
|
8 |
| import net.sourceforge.pmd.ast.ASTExpression; |
|
9 |
| import net.sourceforge.pmd.ast.ASTName; |
|
10 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
|
11 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
|
12 |
| import net.sourceforge.pmd.ast.ASTStatementExpression; |
|
13 |
| import net.sourceforge.pmd.ast.Node; |
|
14 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
15 |
| |
|
16 |
| public class IdempotentOperations extends AbstractRule { |
|
17 |
| |
|
18 |
6
| public Object visit(ASTStatementExpression node, Object data) {
|
|
19 |
6
| if (node.jjtGetNumChildren() != 3
|
|
20 |
| || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression) |
|
21 |
| || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator) |
|
22 |
| || (((ASTAssignmentOperator) (node.jjtGetChild(1))).isCompound()) |
|
23 |
| || !(node.jjtGetChild(2) instanceof ASTExpression) |
|
24 |
| || node.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 |
|
25 |
| || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 |
|
26 |
| ) { |
|
27 |
3
| return super.visit(node, data);
|
|
28 |
| } |
|
29 |
| |
|
30 |
3
| SimpleNode lhs = (SimpleNode) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
|
31 |
3
| if (!(lhs instanceof ASTName)) {
|
|
32 |
0
| return super.visit(node, data);
|
|
33 |
| } |
|
34 |
| |
|
35 |
3
| SimpleNode rhs = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
|
36 |
3
| if (!(rhs instanceof ASTName)) {
|
|
37 |
0
| return super.visit(node, data);
|
|
38 |
| } |
|
39 |
| |
|
40 |
3
| if (!lhs.hasImageEqualTo(rhs.getImage())) {
|
|
41 |
0
| return super.visit(node, data);
|
|
42 |
| } |
|
43 |
| |
|
44 |
3
| if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
|
|
45 |
0
| Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
|
|
46 |
0
| if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) {
|
|
47 |
0
| return super.visit(node, data);
|
|
48 |
| } |
|
49 |
| } |
|
50 |
| |
|
51 |
3
| if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
|
|
52 |
2
| Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
|
|
53 |
2
| if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) {
|
|
54 |
2
| return super.visit(node, data);
|
|
55 |
| } |
|
56 |
| } |
|
57 |
| |
|
58 |
1
| if (lhs.findChildrenOfType(ASTPrimarySuffix.class).size() != rhs.findChildrenOfType(ASTPrimarySuffix.class).size()) {
|
|
59 |
0
| return super.visit(node, data);
|
|
60 |
| } |
|
61 |
| |
|
62 |
1
| addViolation(data, node);
|
|
63 |
1
| return super.visit(node, data);
|
|
64 |
| } |
|
65 |
| } |