Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Nov 22, 2024
1 parent 6c8cb10 commit 3abd9a7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
15 changes: 6 additions & 9 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1734,16 +1734,13 @@ private fun KotlinFileExtractor.extractWhen(
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, callable, exprParent.enclosingStmt)

/* OLD: KE1, Should we remove this `when_if` DB construct?
if (e.origin == IrStatementOrigin.IF) {
tw.writeWhen_if(id)
}
*/
val subjectVariable = e.subjectVariable
val subjectExpression = e.subjectExpression

if (e.subjectVariable != null) {
extractVariableExpr(e.subjectVariable!!, callable, id, -1, exprParent.enclosingStmt)
} else if (e.subjectExpression != null) {
extractExpressionExpr(e.subjectExpression!!, callable, id, -1, exprParent.enclosingStmt)
if (subjectVariable != null) {
extractVariableExpr(subjectVariable, callable, id, -1, exprParent.enclosingStmt)
} else if (subjectExpression != null) {
extractExpressionExpr(subjectExpression, callable, id, -1, exprParent.enclosingStmt)
}

e.entries.forEachIndexed { i, b ->
Expand Down
60 changes: 55 additions & 5 deletions java/ql/lib/semmle/code/java/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2571,10 +2571,28 @@ class WhenExpr extends Expr, StmtParent, @whenexpr {
/** Holds if this was written as an `if` expression. */
predicate isIf() { when_if(this) }

/** Gets the expression of this `when` expression, if any. */
/**
* Gets the expression of this `when` expression, if any; such as `foo()` in the below sample.
*
* ```
* when (foo()) {
* 1 -> ...
* 2 -> ...
* }
*/
Expr getExpr() { result.isNthChildOf(this, -1) }

/** Gets the local variable declaration of this `when` expression, if any. */
/**
* Gets the local variable declaration of this `when` expression, if any; such as
* `val x = foo()` in the below sample.
*
* ```
* when (val x = foo()) {
* 1 -> ...
* 2 -> ...
* }
* ```
*/
LocalVariableDeclExpr getAVariableDeclExpr() { result.isNthChildOf(this, -1) }
}

Expand All @@ -2589,13 +2607,32 @@ class WhenBranch extends Stmt, @whenbranch {
result = this.getCondition(0).(WhenBranchConditionWithExpression).getExpression()
}

/** Gets the `i`th condition of this branch. */
/**
* Gets the `i`th condition of this branch. The first branch in the below sample has two conditions:
*
* ```
* when (foo()) {
* 1, !in 4..10 -> ...
* 3 -> ...
* }
* ```
*/
WhenBranchCondition getCondition(int i) { i <= 0 and result.isNthChildOf(this, i) }

/** Gets a condition of this branch. */
WhenBranchCondition getACondition() { result = this.getCondition(_) }

/** Gets the guard applicable to this branch, if any. */
/**
* Gets the guard applicable to this branch, if any. Guards are currently experimental Kotlin features.
* In the below sample, the first branch has a guard: `bar() == 42`.
*
* ```
* when (foo()) {
* 1 if bar() == 42 -> ...
* else -> ..
* }
* ```
*/
Expr getGuard() { result.isNthChildOf(this, 2) }

/** Gets the result of this branch. */
Expand All @@ -2615,7 +2652,20 @@ class WhenBranch extends Stmt, @whenbranch {
override string getAPrimaryQlClass() { result = "WhenBranch" }
}

/** A Kotlin `when` branch condition. */
/**
* A Kotlin `when` branch condition. Sample conditions are shown below:
*
* ```
* fun foo(): Number = ...
*
* when (foo()) {
* 1 -> ...
* in 2..10 -> ...
* is Int -> ...
* !is Int -> ...
* }
* ```
*/
abstract class WhenBranchCondition extends Stmt, @whenbranchcondition { }

/** A Kotlin `when` branch condition with an expression. */
Expand Down

0 comments on commit 3abd9a7

Please sign in to comment.