Skip to content

Commit 7a6db06

Browse files
committed
Address review feedback
1 parent 905648e commit 7a6db06

File tree

12 files changed

+29
-26
lines changed

12 files changed

+29
-26
lines changed

java/ql/src/Language Abuse/MissedTernaryOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java
1515

1616
predicate complicatedBranch(Stmt branch) {
17-
exists(ConditionalExpr ce | ce.getParent*() = branch) or
17+
any(ConditionalExpr ce).getParent*() = branch or
1818
count(MethodAccess a | a.getParent*() = branch) > 1
1919
}
2020

java/ql/src/Language Abuse/UselessUpcast.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ predicate usefulUpcast(CastExpr e) {
3636
)
3737
or
3838
// Upcasts that are performed on an operand of a ternary expression.
39-
exists(ConditionalExpr ce | e = ce.getBranchExpr(_))
39+
e = any(ConditionalExpr ce).getABranchExpr()
4040
or
4141
// Upcasts to raw types.
4242
e.getType() instanceof RawType

java/ql/src/Likely Bugs/Arithmetic/CondExprTypes.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CharType extends PrimitiveType {
1616
CharType() { this.hasName("char") }
1717
}
1818

19-
private Type getABranchType(ConditionalExpr ce) { result = ce.getBranchExpr(_).getType() }
19+
private Type getABranchType(ConditionalExpr ce) { result = ce.getABranchExpr().getType() }
2020

2121
from ConditionalExpr ce
2222
where

java/ql/src/Likely Bugs/Comparison/NoAssignInBooleanExprs.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import semmle.code.java.Statement
1717
/** An expression that is used as a condition. */
1818
class BooleanExpr extends Expr {
1919
BooleanExpr() {
20-
exists(ConditionalStmt s | s.getCondition() = this) or
21-
exists(ConditionalExpr s | s.getCondition() = this)
20+
this = any(ConditionalStmt s).getCondition() or
21+
this = any(ConditionalExpr s).getCondition()
2222
}
2323
}
2424

java/ql/src/Likely Bugs/Resource Leaks/CloseType.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private predicate flowsInto(Expr e, Variable v) {
1111
or
1212
exists(CastExpr c | flowsInto(c, v) | e = c.getExpr())
1313
or
14-
exists(ConditionalExpr c | flowsInto(c, v) | e = c.getBranchExpr(_))
14+
exists(ConditionalExpr c | flowsInto(c, v) | e = c.getABranchExpr())
1515
}
1616

1717
/**

java/ql/src/Security/CWE/CWE-190/ArithmeticCommon.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ predicate upcastToWiderType(Expr e) {
148148
or
149149
exists(Parameter p | p.getAnArgument() = e and t2 = p.getType())
150150
or
151-
exists(ConditionalExpr cond | cond.getBranchExpr(_) | t2 = cond.getType())
151+
exists(ConditionalExpr cond | cond.getABranchExpr() = e | t2 = cond.getType())
152152
)
153153
}
154154

java/ql/src/Violations of Best Practice/legacy/AutoBoxing.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ predicate unboxed(BoxedExpr e) {
4545
or
4646
flowTarget(e).getType() instanceof PrimitiveType
4747
or
48-
exists(ConditionalExpr cond | cond instanceof PrimitiveExpr | cond.getBranchExpr(_) = e)
48+
exists(ConditionalExpr cond | cond instanceof PrimitiveExpr | cond.getABranchExpr() = e)
4949
}
5050

5151
/**

java/ql/src/semmle/code/java/ControlFlowGraph.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private module ControlFlowGraphImpl {
293293
exists(ConditionalExpr condexpr |
294294
condexpr.getCondition() = b
295295
or
296-
condexpr.getBranchExpr(_) = b and
296+
condexpr.getABranchExpr() = b and
297297
inBooleanContext(condexpr)
298298
)
299299
or
@@ -706,7 +706,7 @@ private module ControlFlowGraphImpl {
706706
or
707707
// The last node of a `ConditionalExpr` is in either of its branches.
708708
exists(ConditionalExpr condexpr | condexpr = n |
709-
last(condexpr.getBranchExpr(_), last, completion)
709+
last(condexpr.getABranchExpr(), last, completion)
710710
)
711711
or
712712
exists(InstanceOfExpr ioe | ioe.isPattern() and ioe = n |

java/ql/src/semmle/code/java/Expr.qll

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ class CompileTimeConstantExpr extends Expr {
184184
// Ternary conditional, with compile-time constant condition.
185185
exists(ConditionalExpr ce, boolean condition |
186186
ce = this and
187-
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue()
188-
|
187+
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
189188
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getStringValue()
190189
)
191190
or
@@ -293,9 +292,8 @@ class CompileTimeConstantExpr extends Expr {
293292
// Ternary expressions, where the `true` and `false` expressions are boolean compile-time constants.
294293
exists(ConditionalExpr ce, boolean condition |
295294
ce = this and
296-
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue()
297-
|
298-
ce.getBranchExpr(condition).(CompileTimeConstantExpr).getBooleanValue()
295+
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
296+
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getBooleanValue()
299297
)
300298
or
301299
// Simple or qualified names where the variable is final and the initializer is a constant.
@@ -376,8 +374,7 @@ class CompileTimeConstantExpr extends Expr {
376374
// Ternary conditional, with compile-time constant condition.
377375
exists(ConditionalExpr ce, boolean condition |
378376
ce = this and
379-
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue()
380-
|
377+
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
381378
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getIntValue()
382379
)
383380
or
@@ -1180,7 +1177,7 @@ class ChooseExpr extends Expr {
11801177

11811178
/** Gets a result expression of this `switch` or conditional expression. */
11821179
Expr getAResultExpr() {
1183-
result = this.(ConditionalExpr).getBranchExpr(_) or
1180+
result = this.(ConditionalExpr).getABranchExpr() or
11841181
result = this.(SwitchExpr).getAResult()
11851182
}
11861183
}
@@ -1212,9 +1209,17 @@ class ConditionalExpr extends Expr, @conditionalexpr {
12121209
* it is `getFalseExpr()`.
12131210
*/
12141211
Expr getBranchExpr(boolean branch) {
1215-
if branch = true then result = getTrueExpr() else result = getFalseExpr()
1212+
branch = true and result = getTrueExpr()
1213+
or
1214+
branch = false and result = getFalseExpr()
12161215
}
12171216

1217+
/**
1218+
* Gets the expressions that is evaluated by one of the branches (`true`
1219+
* or `false` branch) of this conditional expression.
1220+
*/
1221+
Expr getABranchExpr() { result = getBranchExpr(_) }
1222+
12181223
/** Gets a printable representation of this expression. */
12191224
override string toString() { result = "...?...:..." }
12201225

java/ql/src/semmle/code/java/controlflow/internal/GuardsLogic.qll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ predicate implies_v1(Guard g1, boolean b1, Guard g2, boolean b2) {
4040
)
4141
or
4242
exists(ConditionalExpr cond, boolean branch, BooleanLiteral boollit, boolean boolval |
43-
cond.getBranchExpr(branch) = boollit
44-
|
43+
cond.getBranchExpr(branch) = boollit and
4544
cond = g1 and
4645
boolval = boollit.getBooleanValue() and
4746
b1 = boolval.booleanNot() and
4847
(
4948
g2 = cond.getCondition() and b2 = branch.booleanNot()
5049
or
51-
g2 = cond.getBranchExpr(_) and b2 = b1
50+
g2 = cond.getABranchExpr() and b2 = b1
5251
)
5352
)
5453
or
@@ -212,7 +211,7 @@ private predicate hasPossibleUnknownValue(SsaVariable v) {
212211
* `ConditionalExpr`s.
213212
*/
214213
private Expr possibleValue(Expr e) {
215-
result = possibleValue(e.(ConditionalExpr).getBranchExpr(_))
214+
result = possibleValue(e.(ConditionalExpr).getABranchExpr())
216215
or
217216
result = e and not e instanceof ConditionalExpr
218217
}

0 commit comments

Comments
 (0)