Repository navigation
Expand file tree
/
Copy pathSimplifyBooleanReturnsRule.java
More file actions
193 lines (168 loc) · 8.38 KB
/
Copy pathSimplifyBooleanReturnsRule.java
File metadata and controls
193 lines (168 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.design;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.CONDITIONAL_AND;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.CONDITIONAL_OR;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.EQ;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.GE;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.GT;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.LE;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.LT;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.NE;
import static net.sourceforge.pmd.lang.java.ast.BinaryOp.opsWithGreaterPrecedence;
import static net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils.areComplements;
import static net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils.isBooleanLiteral;
import static net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils.isInfixExprWithOperator;
import java.util.EnumSet;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTCastExpression;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTInfixExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpression;
import net.sourceforge.pmd.lang.java.ast.BinaryOp;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.types.JPrimitiveType.PrimitiveTypeKind;
import net.sourceforge.pmd.reporting.RuleContext;
public class SimplifyBooleanReturnsRule extends AbstractJavaRulechainRule {
private static final Set<BinaryOp> NEGATABLE_OPS = EnumSet.of(EQ, NE, GT, LT, GE, LE);
public SimplifyBooleanReturnsRule() {
super(ASTReturnStatement.class);
}
@Override
public Object visit(ASTReturnStatement node, Object data) {
ASTExpression expr = node.getExpr();
if (expr == null
|| !expr.getTypeMirror().isPrimitive(PrimitiveTypeKind.BOOLEAN)
|| !isThenBranchOfSomeIf(node)) {
return null;
}
checkIf(node.ancestors(ASTIfStatement.class).firstOrThrow(), asCtx(data), expr);
return null;
}
// Only explore the then branch. If we explore the else, then we'll report twice.
// In the case if .. else, both branches need to be symmetric anyway.
private boolean isThenBranchOfSomeIf(ASTReturnStatement node) {
if (node.getParent() instanceof ASTIfStatement) {
return node.getIndexInParent() == 1;
}
return node.getParent() instanceof ASTBlock
&& ((ASTBlock) node.getParent()).size() == 1
&& node.getParent().getParent() instanceof ASTIfStatement
&& node.getParent().getIndexInParent() == 1;
}
private void checkIf(ASTIfStatement node, RuleContext data, ASTExpression thenExpr) {
// that's the case: if..then..return; return;
ASTExpression elseExpr = getElseExpr(node);
if (elseExpr == null) {
return;
}
if (isBooleanLiteral(thenExpr) || isBooleanLiteral(elseExpr)) {
String fix = needsToBeReportedWhenOneBranchIsBoolean(node.getCondition(), thenExpr, elseExpr);
if (fix != null) {
data.addViolation(node, fix);
}
} else if (areComplements(thenExpr, elseExpr)) {
// if (foo) return !a;
// else return a;
data.addViolation(node, "return {condition};");
}
}
/**
* Whether refactoring an if-then-else to use shortcut operators
* would require adding parentheses to respect operator precedence.
* <pre>{@code
* if (cond) true else expr -> cond || expr
* if (cond) false else expr -> !cond && expr
* if (cond) expr else true -> !cond || expr
* if (cond) expr else false -> cond && expr
* }</pre>
* Note that both the `expr` and the `condition` may require parentheses
* (if the cond has to be negated).
*/
private String needsToBeReportedWhenOneBranchIsBoolean(ASTExpression condition,
ASTExpression thenExpr,
ASTExpression elseExpr) {
// at least one of these is true
boolean thenFalse = isBooleanLiteral(thenExpr, false);
boolean thenTrue = isBooleanLiteral(thenExpr, true);
boolean elseTrue = isBooleanLiteral(elseExpr, true);
boolean elseFalse = isBooleanLiteral(elseExpr, false);
assert thenFalse || elseFalse || thenTrue || elseTrue
: "expected boolean branch";
if (isBooleanLiteral(thenExpr) && isBooleanLiteral(elseExpr)) {
// both are boolean
if (thenTrue && elseFalse) {
return "return {condition};";
} else if (thenFalse && elseTrue) {
return "return !{condition};";
} else if (thenTrue) { // both are true
return "return true;";
} else { // both are false
return "return false;";
}
}
BinaryOp op = thenFalse || elseFalse ? CONDITIONAL_AND : CONDITIONAL_OR;
// the branch that is not a literal, if both are literals, prefers elseExpr
ASTExpression branch = thenFalse || thenTrue ? elseExpr : thenExpr;
boolean conditionNegated = thenFalse || elseTrue;
// from the above logic it is guaranteed that exactly one of the four booleans is true
String conditionNegation = conditionNegated ? "!" : "";
String operator = thenTrue || elseTrue ? "||" : "&&";
String branchChoice = thenTrue || thenFalse ? "{elseBranch}" : "{thenBranch}";
boolean conditionNeedsParens = (conditionNegated && needsNewParensWhenNegating(condition))
|| needsParensUnderInfix(condition, op);
boolean branchNeedsParens = needsParensUnderInfix(branch, op);
if (conditionNeedsParens || branchNeedsParens) {
return null;
}
return "return " + conditionNegation + "{condition} " + operator + " "
+ branchChoice + ";";
}
private static boolean needsNewParensWhenNegating(ASTExpression e) {
if (e instanceof ASTPrimaryExpression
|| e instanceof ASTCastExpression
// parenthesized expressions are primary
|| e.isParenthesized()
// == -> !=
|| isInfixExprWithOperator(e, NEGATABLE_OPS)
// !! ->
|| JavaAstUtils.isBooleanNegation(e)) {
return false;
} else if (isInfixExprWithOperator(e, CONDITIONAL_OR)
|| isInfixExprWithOperator(e, CONDITIONAL_AND)) {
// negating these ops can be replaced with complement op
// and the negation pushed down branches using De Morgan's laws
ASTInfixExpression infix = (ASTInfixExpression) e;
return needsNewParensWhenNegating(infix.getLeftOperand())
|| needsNewParensWhenNegating(infix.getRightOperand());
}
return true;
}
private static boolean needsParensUnderInfix(ASTExpression e, BinaryOp op) {
// those nodes have greater precedence than infix
return !(e instanceof ASTPrimaryExpression
|| e instanceof ASTCastExpression
|| e instanceof ASTUnaryExpression
|| isInfixExprWithOperator(e, opsWithGreaterPrecedence(op)));
}
private @Nullable ASTExpression getReturnExpr(JavaNode node) {
if (node instanceof ASTReturnStatement) {
return ((ASTReturnStatement) node).getExpr();
} else if (node instanceof ASTBlock && ((ASTBlock) node).size() == 1) {
return getReturnExpr(((ASTBlock) node).get(0));
}
return null;
}
private @Nullable ASTExpression getElseExpr(ASTIfStatement node) {
return node.hasElse() ? getReturnExpr(node.getElseBranch())
: getReturnExpr(node.getNextSibling()); // may be followed immediately by return
}
}