Skip to content

Commit 78c23c2

Browse files
committed
Kotlin: Exclude constructs in serialization constructors from java/evaluation-to-constant
1 parent 30fc6ac commit 78c23c2

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

java/ql/lib/semmle/code/java/deadcode/DeadCode.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semmle.code.java.deadcode.DeadEnumConstant
33
import semmle.code.java.deadcode.DeadCodeCustomizations
44
import semmle.code.java.deadcode.DeadField
55
import semmle.code.java.deadcode.EntryPoints
6+
private import semmle.code.java.frameworks.kotlin.Serialization
67

78
/**
89
* Holds if the given callable has any liveness causes.
@@ -309,10 +310,7 @@ class RootdefCallable extends Callable {
309310
this.isCompilerGenerated()
310311
or
311312
// Exclude Kotlin serialization constructors.
312-
this.(Constructor)
313-
.getParameterType(this.getNumberOfParameters() - 1)
314-
.(RefType)
315-
.hasQualifiedName("kotlinx.serialization.internal", "SerializationConstructorMarker")
313+
this instanceof SerializationConstructor
316314
}
317315
}
318316

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Provides classes and predicates for working with thi `kotlinx.serialization` plugin.
3+
*/
4+
5+
import java
6+
7+
/**
8+
* A constructor with a `SerializationConstructorMarker` parameter.
9+
*/
10+
class SerializationConstructor extends Constructor {
11+
SerializationConstructor() {
12+
this.getParameterType(this.getNumberOfParameters() - 1)
13+
.(RefType)
14+
.hasQualifiedName("kotlinx.serialization.internal", "SerializationConstructorMarker")
15+
}
16+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
import java
13+
private import semmle.code.java.frameworks.kotlin.Serialization
1314

1415
int eval(Expr e) { result = e.(CompileTimeConstantExpr).getIntValue() }
1516

@@ -59,5 +60,7 @@ where
5960
// Exclude explicit zero multiplication.
6061
not e.(MulExpr).getAnOperand().(IntegerLiteral).getIntValue() = 0 and
6162
// Exclude expressions that appear to be disabled deliberately (e.g. `false && ...`).
62-
not e.(AndLogicalExpr).getAnOperand().(BooleanLiteral).getBooleanValue() = false
63+
not e.(AndLogicalExpr).getAnOperand().(BooleanLiteral).getBooleanValue() = false and
64+
// Exclude expressions that are in serialization constructors, which are auto-generated.
65+
not e.getEnclosingCallable() instanceof SerializationConstructor
6366
select e, "Expression always evaluates to the same value."

0 commit comments

Comments
 (0)