mirror of
https://github.com/github/codeql.git
synced 2025-12-22 11:46:32 +01:00
Kotlin: Add FP test case for dead code queries
This commit is contained in:
@@ -14,6 +14,8 @@ import semmle.code.java.deadcode.DeadCode
|
||||
|
||||
from DeadClass c, Element origin, string reason
|
||||
where
|
||||
not c.getFile().isKotlinSourceFile() and
|
||||
not origin.getFile().isKotlinSourceFile() and
|
||||
if exists(DeadRoot root | root = c.getADeadRoot() | not root = c.getACallable())
|
||||
then (
|
||||
// Report a list of the dead roots.
|
||||
|
||||
@@ -15,6 +15,8 @@ import semmle.code.java.deadcode.DeadCode
|
||||
|
||||
from DeadField f, Element origin, string reason
|
||||
where
|
||||
not f.getFile().isKotlinSourceFile() and
|
||||
not origin.getFile().isKotlinSourceFile() and
|
||||
not f.isInDeadScope() and
|
||||
if f.getAnAccess() instanceof FieldRead
|
||||
then (
|
||||
|
||||
@@ -15,6 +15,8 @@ import semmle.code.java.deadcode.DeadCode
|
||||
|
||||
from DeadMethod c, Callable origin, string reason
|
||||
where
|
||||
not c.getFile().isKotlinSourceFile() and
|
||||
not origin.getFile().isKotlinSourceFile() and
|
||||
not c.isInDeadScope() and
|
||||
if exists(DeadRoot deadRoot | deadRoot = getADeadRoot(c) | deadRoot.getSourceDeclaration() != c)
|
||||
then (
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
| Test.kt:0:0:0:0 | TestKt | The class TestKt is entirely unused. | Test.kt:0:0:0:0 | TestKt | TestKt |
|
||||
| Test.kt:1:1:1:26 | DbAddexpr | The class DbAddexpr is entirely unused. | Test.kt:1:1:1:26 | DbAddexpr | DbAddexpr |
|
||||
| Test.kt:3:1:4:1 | Label | The class Label is only used from dead code originating at $@. | Test.kt:15:1:17:1 | main1 | main1 |
|
||||
| Test.kt:19:1:27:1 | Foo | The class Foo is only used from dead code originating at $@. | Test.kt:29:1:31:1 | main2 | main2 |
|
||||
1
java/ql/test/kotlin/query-tests/DeadCode/DeadClass.qlref
Normal file
1
java/ql/test/kotlin/query-tests/DeadCode/DeadClass.qlref
Normal file
@@ -0,0 +1 @@
|
||||
DeadCode/DeadClass.ql
|
||||
@@ -0,0 +1,8 @@
|
||||
| Test.kt:0:0:0:0 | component1 | The method component1 is entirely unused. | Test.kt:0:0:0:0 | component1 | component1 |
|
||||
| Test.kt:0:0:0:0 | component2 | The method component2 is entirely unused. | Test.kt:0:0:0:0 | component2 | component2 |
|
||||
| Test.kt:0:0:0:0 | copy | The method copy is only used from dead code originating at $@. | Test.kt:0:0:0:0 | copy$default | copy$default |
|
||||
| Test.kt:0:0:0:0 | copy$default | The method copy$default is entirely unused. | Test.kt:0:0:0:0 | copy$default | copy$default |
|
||||
| Test.kt:20:10:20:41 | DC | The method DC is only used from dead code originating at $@. | Test.kt:0:0:0:0 | copy$default | copy$default |
|
||||
| Test.kt:20:10:20:41 | DC | The method DC is only used from dead code originating at $@. | Test.kt:29:1:31:1 | main2 | main2 |
|
||||
| Test.kt:20:19:20:28 | getX | The method getX is only used from dead code originating at $@. | Test.kt:29:1:31:1 | main2 | main2 |
|
||||
| Test.kt:20:31:20:40 | getY | The method getY is only used from dead code originating at $@. | Test.kt:29:1:31:1 | main2 | main2 |
|
||||
@@ -0,0 +1 @@
|
||||
DeadCode/DeadMethod.ql
|
||||
31
java/ql/test/kotlin/query-tests/DeadCode/Test.kt
Normal file
31
java/ql/test/kotlin/query-tests/DeadCode/Test.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
sealed interface DbAddexpr
|
||||
|
||||
class Label<T> {
|
||||
}
|
||||
|
||||
fun <T> getFreshIdLabel(): Label<T> {
|
||||
return Label()
|
||||
}
|
||||
|
||||
fun foo(): Label<DbAddexpr> {
|
||||
val x = getFreshIdLabel<DbAddexpr>()
|
||||
return x
|
||||
}
|
||||
|
||||
fun main1() {
|
||||
print(foo())
|
||||
}
|
||||
|
||||
class Foo {
|
||||
data class DC(val x: Int, val y: Int)
|
||||
|
||||
fun foo() {
|
||||
val dc = DC(3, 4)
|
||||
print(dc.x)
|
||||
print(dc.y)
|
||||
}
|
||||
}
|
||||
|
||||
fun main2() {
|
||||
Foo().foo()
|
||||
}
|
||||
Reference in New Issue
Block a user