mirror of
https://github.com/github/codeql.git
synced 2025-12-21 03:06:31 +01:00
Extract not-null expression
This commit is contained in:
@@ -695,6 +695,7 @@ case @expr.kind of
|
||||
| 79 = @stmtexpr
|
||||
| 80 = @stringtemplateexpr
|
||||
| 81 = @varargexpr
|
||||
| 82 = @notnullexpr
|
||||
;
|
||||
|
||||
/** Holds if this `when` expression was written as an `if` expression. */
|
||||
@@ -771,7 +772,8 @@ when_branch_else(unique int id: @whenbranch ref);
|
||||
| @minusexpr
|
||||
| @plusexpr
|
||||
| @bitnotexpr
|
||||
| @lognotexpr;
|
||||
| @lognotexpr
|
||||
| @notnullexpr;
|
||||
|
||||
@caller = @classinstancexpr
|
||||
| @methodaccess
|
||||
|
||||
@@ -1463,9 +1463,7 @@ class InstanceOfExpr extends Expr, @instanceofexpr {
|
||||
/** An `instanceof` expression. */
|
||||
class NotInstanceOfExpr extends Expr, @notinstanceofexpr {
|
||||
/** Gets the expression on the left-hand side of the `!is` operator. */
|
||||
Expr getExpr() {
|
||||
result.isNthChildOf(this, 0)
|
||||
}
|
||||
Expr getExpr() { result.isNthChildOf(this, 0) }
|
||||
|
||||
/** Gets the access to the type on the right-hand side of the `!is` operator. */
|
||||
Expr getTypeName() { result.isNthChildOf(this, 1) }
|
||||
@@ -2195,19 +2193,13 @@ class WhenBranch extends Top, @whenbranch {
|
||||
Expr getCondition() { result.isNthChildOf(this, 0) }
|
||||
|
||||
/** Gets the result of this branch. */
|
||||
Stmt getRhs() {
|
||||
result.isNthChildOf(this, 1)
|
||||
}
|
||||
Stmt getRhs() { result.isNthChildOf(this, 1) }
|
||||
|
||||
/** Gets a result expression of this `when` branch. */
|
||||
Expr getAResult() {
|
||||
result = getAResult(this.getRhs())
|
||||
}
|
||||
Expr getAResult() { result = getAResult(this.getRhs()) }
|
||||
|
||||
/** Holds if this is an `else` branch. */
|
||||
predicate isElseBranch() {
|
||||
when_branch_else(this)
|
||||
}
|
||||
predicate isElseBranch() { when_branch_else(this) }
|
||||
|
||||
override string toString() { result = "... -> ..." }
|
||||
|
||||
@@ -2290,3 +2282,10 @@ class VarArgExpr extends Expr, @varargexpr {
|
||||
|
||||
override string getAPrimaryQlClass() { result = "VarArgExpr" }
|
||||
}
|
||||
|
||||
/** A Kotlin not-null expression. For example, `expr!!`. */
|
||||
class NotNullExpr extends UnaryExpr, @notnullexpr {
|
||||
override string toString() { result = "...!!" }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "NotNullExpr" }
|
||||
}
|
||||
|
||||
@@ -534,6 +534,9 @@
|
||||
| exprs.kt:193:31:193:37 | ... + ... | exprs.kt:192:16:194:9 | <obinit> | AddExpr |
|
||||
| exprs.kt:193:36:193:37 | a2 | exprs.kt:192:16:194:9 | <obinit> | VarAccess |
|
||||
| exprs.kt:193:40:193:49 | toString(...) | exprs.kt:192:16:194:9 | <obinit> | MethodAccess |
|
||||
| exprs.kt:199:5:199:20 | y | exprs.kt:198:1:200:1 | notNullAssertion | LocalVariableDeclExpr |
|
||||
| exprs.kt:199:18:199:18 | x | exprs.kt:198:1:200:1 | notNullAssertion | VarAccess |
|
||||
| exprs.kt:199:19:199:20 | ...!! | exprs.kt:198:1:200:1 | notNullAssertion | NotNullExpr |
|
||||
| file://:0:0:0:0 | C | exprs.kt:146:5:146:33 | foo | TypeAccess |
|
||||
| file://:0:0:0:0 | Color | exprs.kt:175:6:179:1 | Color | TypeAccess |
|
||||
| file://:0:0:0:0 | Direction | exprs.kt:171:6:173:1 | Direction | TypeAccess |
|
||||
|
||||
@@ -194,3 +194,7 @@ class Class1 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun notNullAssertion(x: Any?) {
|
||||
val y: Any = x!!
|
||||
}
|
||||
|
||||
1
java/ql/test/kotlin/library-tests/exprs/unaryOp.expected
Normal file
1
java/ql/test/kotlin/library-tests/exprs/unaryOp.expected
Normal file
@@ -0,0 +1 @@
|
||||
| exprs.kt:199:19:199:20 | ...!! | exprs.kt:199:18:199:18 | x |
|
||||
36
java/ql/test/kotlin/library-tests/exprs/unaryOp.ql
Normal file
36
java/ql/test/kotlin/library-tests/exprs/unaryOp.ql
Normal file
@@ -0,0 +1,36 @@
|
||||
import java
|
||||
|
||||
newtype TMaybeElement =
|
||||
TElement(Element e) or
|
||||
TNoElement()
|
||||
|
||||
class MaybeElement extends TMaybeElement {
|
||||
abstract string toString();
|
||||
|
||||
abstract Location getLocation();
|
||||
}
|
||||
|
||||
class YesMaybeElement extends MaybeElement {
|
||||
Element e;
|
||||
|
||||
YesMaybeElement() { this = TElement(e) }
|
||||
|
||||
override string toString() { result = e.toString() }
|
||||
|
||||
override Location getLocation() { result = e.getLocation() }
|
||||
}
|
||||
|
||||
class NoMaybeElement extends MaybeElement {
|
||||
NoMaybeElement() { this = TNoElement() }
|
||||
|
||||
override string toString() { result = "<none>" }
|
||||
|
||||
override Location getLocation() { none() }
|
||||
}
|
||||
|
||||
MaybeElement op(UnaryExpr e) {
|
||||
if exists(e.getExpr()) then result = TElement(e.getExpr()) else result = TNoElement()
|
||||
}
|
||||
|
||||
from Expr e
|
||||
select e, op(e)
|
||||
Reference in New Issue
Block a user