Skip to content

Commit 36fa0a2

Browse files
committed
Java: Rename getTrueExpr/getFalseExpr on ConditionalExpr to getThen/getElse.
1 parent 5e6e64b commit 36fa0a2

File tree

11 files changed

+49
-41
lines changed

11 files changed

+49
-41
lines changed

java/ql/examples/snippets/ternaryconditional.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java
1111

1212
from ConditionalExpr e
1313
where
14-
e.getTrueExpr().getType() != e.getFalseExpr().getType() and
15-
not e.getTrueExpr().getType() instanceof NullType and
16-
not e.getFalseExpr().getType() instanceof NullType
14+
e.getThen().getType() != e.getElse().getType() and
15+
not e.getThen().getType() instanceof NullType and
16+
not e.getElse().getType() instanceof NullType
1717
select e

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ class CompileTimeConstantExpr extends Expr {
166166
// The ternary conditional operator ` ? : `.
167167
exists(ConditionalExpr e | this = e |
168168
e.getCondition().isCompileTimeConstant() and
169-
e.getTrueExpr().isCompileTimeConstant() and
170-
e.getFalseExpr().isCompileTimeConstant()
169+
e.getThen().isCompileTimeConstant() and
170+
e.getElse().isCompileTimeConstant()
171171
)
172172
or
173173
// Access to a final variable initialized by a compile-time constant.
@@ -1463,27 +1463,43 @@ class ConditionalExpr extends Expr, @conditionalexpr {
14631463
/** Gets the condition of this conditional expression. */
14641464
Expr getCondition() { result.isNthChildOf(this, 0) }
14651465

1466+
/**
1467+
* DEPRECATED: Use getThen() instead.
1468+
*
1469+
* Gets the expression that is evaluated if the condition of this
1470+
* conditional expression evaluates to `true`.
1471+
*/
1472+
deprecated Expr getTrueExpr() { result.isNthChildOf(this, 1) }
1473+
1474+
/**
1475+
* DEPRECATED: Use getElse() instead.
1476+
*
1477+
* Gets the expression that is evaluated if the condition of this
1478+
* conditional expression evaluates to `false`.
1479+
*/
1480+
deprecated Expr getFalseExpr() { result.isNthChildOf(this, 2) }
1481+
14661482
/**
14671483
* Gets the expression that is evaluated if the condition of this
14681484
* conditional expression evaluates to `true`.
14691485
*/
1470-
Expr getTrueExpr() { result.isNthChildOf(this, 1) }
1486+
Expr getThen() { result.isNthChildOf(this, 1) }
14711487

14721488
/**
14731489
* Gets the expression that is evaluated if the condition of this
14741490
* conditional expression evaluates to `false`.
14751491
*/
1476-
Expr getFalseExpr() { result.isNthChildOf(this, 2) }
1492+
Expr getElse() { result.isNthChildOf(this, 2) }
14771493

14781494
/**
14791495
* Gets the expression that is evaluated by the specific branch of this
1480-
* conditional expression. If `true` that is `getTrueExpr()`, if `false`
1481-
* it is `getFalseExpr()`.
1496+
* conditional expression. If `true` that is `getThen()`, if `false`
1497+
* it is `getElse()`.
14821498
*/
14831499
Expr getBranchExpr(boolean branch) {
1484-
branch = true and result = this.getTrueExpr()
1500+
branch = true and result = this.getThen()
14851501
or
1486-
branch = false and result = this.getFalseExpr()
1502+
branch = false and result = this.getElse()
14871503
}
14881504

14891505
/**

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ private class PpConditionalExpr extends PpAst, ConditionalExpr {
351351
override PpAst getChild(int i) {
352352
i = 0 and result = this.getCondition()
353353
or
354-
i = 2 and result = this.getTrueExpr()
354+
i = 2 and result = this.getThen()
355355
or
356-
i = 4 and result = this.getFalseExpr()
356+
i = 4 and result = this.getElse()
357357
}
358358
}
359359

java/ql/lib/semmle/code/java/controlflow/Guards.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
315315
)
316316
}
317317

318-
class ConditionalExpr extends Expr instanceof J::ConditionalExpr {
319-
Expr getCondition() { result = super.getCondition() }
320-
321-
Expr getThen() { result = super.getTrueExpr() }
322-
323-
Expr getElse() { result = super.getFalseExpr() }
324-
}
318+
class ConditionalExpr = J::ConditionalExpr;
325319

326320
class Parameter = J::Parameter;
327321

java/ql/lib/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ Expr clearlyNotNullExpr(Expr reason) {
8686
or
8787
exists(ConditionalExpr c, Expr r1, Expr r2 |
8888
c = result and
89-
c.getTrueExpr() = clearlyNotNullExpr(r1) and
90-
c.getFalseExpr() = clearlyNotNullExpr(r2) and
89+
c.getThen() = clearlyNotNullExpr(r1) and
90+
c.getElse() = clearlyNotNullExpr(r2) and
9191
(reason = r1 or reason = r2)
9292
)
9393
or

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ where
2525
t instanceof PrimitiveType and
2626
not t instanceof CharType
2727
)
28-
select ce, "Mismatch between types of branches: $@ and $@.", ce.getTrueExpr(),
29-
ce.getTrueExpr().getType().getName(), ce.getFalseExpr(), ce.getFalseExpr().getType().getName()
28+
select ce, "Mismatch between types of branches: $@ and $@.", ce.getThen(),
29+
ce.getThen().getType().getName(), ce.getElse(), ce.getElse().getType().getName()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class StringValue extends Expr {
2727
)
2828
or
2929
// Ternary conditional operator.
30-
this.(ConditionalExpr).getTrueExpr().(StringValue).isInterned() and
31-
this.(ConditionalExpr).getFalseExpr().(StringValue).isInterned()
30+
this.(ConditionalExpr).getThen().(StringValue).isInterned() and
31+
this.(ConditionalExpr).getElse().(StringValue).isInterned()
3232
or
3333
// Values of type `String` that are compile-time constant expressions (JLS 15.28).
3434
this instanceof CompileTimeConstantExpr

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ Expr overFlowCand() {
134134
result.(LocalVariableDeclExpr).getInit() = overFlowCand()
135135
or
136136
exists(ConditionalExpr c | c = result |
137-
c.getTrueExpr() = overFlowCand() and
138-
c.getFalseExpr() = overFlowCand()
137+
c.getThen() = overFlowCand() and
138+
c.getElse() = overFlowCand()
139139
)
140140
}
141141

java/ql/src/Violations of Best Practice/Boolean Logic/SimplifyBoolExpr.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class BoolCompare extends EqualityTest {
4040

4141
predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite) {
4242
exists(boolean truebranch |
43-
c.getTrueExpr().(BooleanLiteral).getBooleanValue() = truebranch and
44-
not c.getFalseExpr() instanceof BooleanLiteral and
45-
not c.getFalseExpr().getType() instanceof NullType and
43+
c.getThen().(BooleanLiteral).getBooleanValue() = truebranch and
44+
not c.getElse() instanceof BooleanLiteral and
45+
not c.getElse().getType() instanceof NullType and
4646
(
4747
truebranch = true and pattern = "A ? true : B" and rewrite = "A || B"
4848
or
@@ -51,9 +51,9 @@ predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite)
5151
)
5252
or
5353
exists(boolean falsebranch |
54-
not c.getTrueExpr() instanceof BooleanLiteral and
55-
not c.getTrueExpr().getType() instanceof NullType and
56-
c.getFalseExpr().(BooleanLiteral).getBooleanValue() = falsebranch and
54+
not c.getThen() instanceof BooleanLiteral and
55+
not c.getThen().getType() instanceof NullType and
56+
c.getElse().(BooleanLiteral).getBooleanValue() = falsebranch and
5757
(
5858
falsebranch = true and pattern = "A ? B : true" and rewrite = "!A || B"
5959
or
@@ -62,8 +62,8 @@ predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite)
6262
)
6363
or
6464
exists(boolean truebranch, boolean falsebranch |
65-
c.getTrueExpr().(BooleanLiteral).getBooleanValue() = truebranch and
66-
c.getFalseExpr().(BooleanLiteral).getBooleanValue() = falsebranch and
65+
c.getThen().(BooleanLiteral).getBooleanValue() = truebranch and
66+
c.getElse().(BooleanLiteral).getBooleanValue() = falsebranch and
6767
(
6868
truebranch = true and falsebranch = false and pattern = "A ? true : false" and rewrite = "A"
6969
or

java/ql/src/Violations of Best Practice/Implementation Hiding/StaticArray.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ predicate nonEmptyArrayLiteralOrNull(Expr e) {
2828
e instanceof NullLiteral
2929
or
3030
exists(ConditionalExpr cond | cond = e |
31-
nonEmptyArrayLiteralOrNull(cond.getTrueExpr()) and
32-
nonEmptyArrayLiteralOrNull(cond.getFalseExpr())
31+
nonEmptyArrayLiteralOrNull(cond.getThen()) and
32+
nonEmptyArrayLiteralOrNull(cond.getElse())
3333
)
3434
}
3535

0 commit comments

Comments
 (0)