Java: Rename getTrueExpr/getFalseExpr on ConditionalExpr to getThen/getElse.

This commit is contained in:
Anders Schack-Mulligen
2026-02-04 13:38:11 +01:00
parent 5e6e64b2b7
commit 36fa0a22f9
11 changed files with 49 additions and 41 deletions

View File

@@ -11,7 +11,7 @@ import java
from ConditionalExpr e
where
e.getTrueExpr().getType() != e.getFalseExpr().getType() and
not e.getTrueExpr().getType() instanceof NullType and
not e.getFalseExpr().getType() instanceof NullType
e.getThen().getType() != e.getElse().getType() and
not e.getThen().getType() instanceof NullType and
not e.getElse().getType() instanceof NullType
select e

View File

@@ -166,8 +166,8 @@ class CompileTimeConstantExpr extends Expr {
// The ternary conditional operator ` ? : `.
exists(ConditionalExpr e | this = e |
e.getCondition().isCompileTimeConstant() and
e.getTrueExpr().isCompileTimeConstant() and
e.getFalseExpr().isCompileTimeConstant()
e.getThen().isCompileTimeConstant() and
e.getElse().isCompileTimeConstant()
)
or
// Access to a final variable initialized by a compile-time constant.
@@ -1464,26 +1464,42 @@ class ConditionalExpr extends Expr, @conditionalexpr {
Expr getCondition() { result.isNthChildOf(this, 0) }
/**
* DEPRECATED: Use getThen() instead.
*
* Gets the expression that is evaluated if the condition of this
* conditional expression evaluates to `true`.
*/
Expr getTrueExpr() { result.isNthChildOf(this, 1) }
deprecated Expr getTrueExpr() { result.isNthChildOf(this, 1) }
/**
* DEPRECATED: Use getElse() instead.
*
* Gets the expression that is evaluated if the condition of this
* conditional expression evaluates to `false`.
*/
deprecated Expr getFalseExpr() { result.isNthChildOf(this, 2) }
/**
* Gets the expression that is evaluated if the condition of this
* conditional expression evaluates to `true`.
*/
Expr getThen() { result.isNthChildOf(this, 1) }
/**
* Gets the expression that is evaluated if the condition of this
* conditional expression evaluates to `false`.
*/
Expr getFalseExpr() { result.isNthChildOf(this, 2) }
Expr getElse() { result.isNthChildOf(this, 2) }
/**
* Gets the expression that is evaluated by the specific branch of this
* conditional expression. If `true` that is `getTrueExpr()`, if `false`
* it is `getFalseExpr()`.
* conditional expression. If `true` that is `getThen()`, if `false`
* it is `getElse()`.
*/
Expr getBranchExpr(boolean branch) {
branch = true and result = this.getTrueExpr()
branch = true and result = this.getThen()
or
branch = false and result = this.getFalseExpr()
branch = false and result = this.getElse()
}
/**

View File

@@ -351,9 +351,9 @@ private class PpConditionalExpr extends PpAst, ConditionalExpr {
override PpAst getChild(int i) {
i = 0 and result = this.getCondition()
or
i = 2 and result = this.getTrueExpr()
i = 2 and result = this.getThen()
or
i = 4 and result = this.getFalseExpr()
i = 4 and result = this.getElse()
}
}

View File

@@ -315,13 +315,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
)
}
class ConditionalExpr extends Expr instanceof J::ConditionalExpr {
Expr getCondition() { result = super.getCondition() }
Expr getThen() { result = super.getTrueExpr() }
Expr getElse() { result = super.getFalseExpr() }
}
class ConditionalExpr = J::ConditionalExpr;
class Parameter = J::Parameter;

View File

@@ -86,8 +86,8 @@ Expr clearlyNotNullExpr(Expr reason) {
or
exists(ConditionalExpr c, Expr r1, Expr r2 |
c = result and
c.getTrueExpr() = clearlyNotNullExpr(r1) and
c.getFalseExpr() = clearlyNotNullExpr(r2) and
c.getThen() = clearlyNotNullExpr(r1) and
c.getElse() = clearlyNotNullExpr(r2) and
(reason = r1 or reason = r2)
)
or

View File

@@ -25,5 +25,5 @@ where
t instanceof PrimitiveType and
not t instanceof CharType
)
select ce, "Mismatch between types of branches: $@ and $@.", ce.getTrueExpr(),
ce.getTrueExpr().getType().getName(), ce.getFalseExpr(), ce.getFalseExpr().getType().getName()
select ce, "Mismatch between types of branches: $@ and $@.", ce.getThen(),
ce.getThen().getType().getName(), ce.getElse(), ce.getElse().getType().getName()

View File

@@ -27,8 +27,8 @@ class StringValue extends Expr {
)
or
// Ternary conditional operator.
this.(ConditionalExpr).getTrueExpr().(StringValue).isInterned() and
this.(ConditionalExpr).getFalseExpr().(StringValue).isInterned()
this.(ConditionalExpr).getThen().(StringValue).isInterned() and
this.(ConditionalExpr).getElse().(StringValue).isInterned()
or
// Values of type `String` that are compile-time constant expressions (JLS 15.28).
this instanceof CompileTimeConstantExpr

View File

@@ -134,8 +134,8 @@ Expr overFlowCand() {
result.(LocalVariableDeclExpr).getInit() = overFlowCand()
or
exists(ConditionalExpr c | c = result |
c.getTrueExpr() = overFlowCand() and
c.getFalseExpr() = overFlowCand()
c.getThen() = overFlowCand() and
c.getElse() = overFlowCand()
)
}

View File

@@ -40,9 +40,9 @@ class BoolCompare extends EqualityTest {
predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite) {
exists(boolean truebranch |
c.getTrueExpr().(BooleanLiteral).getBooleanValue() = truebranch and
not c.getFalseExpr() instanceof BooleanLiteral and
not c.getFalseExpr().getType() instanceof NullType and
c.getThen().(BooleanLiteral).getBooleanValue() = truebranch and
not c.getElse() instanceof BooleanLiteral and
not c.getElse().getType() instanceof NullType and
(
truebranch = true and pattern = "A ? true : B" and rewrite = "A || B"
or
@@ -51,9 +51,9 @@ predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite)
)
or
exists(boolean falsebranch |
not c.getTrueExpr() instanceof BooleanLiteral and
not c.getTrueExpr().getType() instanceof NullType and
c.getFalseExpr().(BooleanLiteral).getBooleanValue() = falsebranch and
not c.getThen() instanceof BooleanLiteral and
not c.getThen().getType() instanceof NullType and
c.getElse().(BooleanLiteral).getBooleanValue() = falsebranch and
(
falsebranch = true and pattern = "A ? B : true" and rewrite = "!A || B"
or
@@ -62,8 +62,8 @@ predicate conditionalWithBool(ConditionalExpr c, string pattern, string rewrite)
)
or
exists(boolean truebranch, boolean falsebranch |
c.getTrueExpr().(BooleanLiteral).getBooleanValue() = truebranch and
c.getFalseExpr().(BooleanLiteral).getBooleanValue() = falsebranch and
c.getThen().(BooleanLiteral).getBooleanValue() = truebranch and
c.getElse().(BooleanLiteral).getBooleanValue() = falsebranch and
(
truebranch = true and falsebranch = false and pattern = "A ? true : false" and rewrite = "A"
or

View File

@@ -28,8 +28,8 @@ predicate nonEmptyArrayLiteralOrNull(Expr e) {
e instanceof NullLiteral
or
exists(ConditionalExpr cond | cond = e |
nonEmptyArrayLiteralOrNull(cond.getTrueExpr()) and
nonEmptyArrayLiteralOrNull(cond.getFalseExpr())
nonEmptyArrayLiteralOrNull(cond.getThen()) and
nonEmptyArrayLiteralOrNull(cond.getElse())
)
}

View File

@@ -56,9 +56,7 @@ predicate boxed(PrimitiveExpr e) {
or
flowTarget(e).getType() instanceof BoxedType
or
exists(ConditionalExpr cond | cond instanceof BoxedExpr |
cond.getTrueExpr() = e or cond.getFalseExpr() = e
)
exists(ConditionalExpr cond | cond instanceof BoxedExpr | cond.getABranchExpr() = e)
}
/**