Skip to content

Commit 983851f

Browse files
committed
Kotlin: Add FP test case for dead code queries
1 parent c70f3d3 commit 983851f

File tree

8 files changed

+51
-0
lines changed

8 files changed

+51
-0
lines changed

java/ql/src/DeadCode/DeadClass.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import semmle.code.java.deadcode.DeadCode
1414

1515
from DeadClass c, Element origin, string reason
1616
where
17+
not c.getFile().isKotlinSourceFile() and
18+
not origin.getFile().isKotlinSourceFile() and
1719
if exists(DeadRoot root | root = c.getADeadRoot() | not root = c.getACallable())
1820
then (
1921
// Report a list of the dead roots.

java/ql/src/DeadCode/DeadField.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import semmle.code.java.deadcode.DeadCode
1515

1616
from DeadField f, Element origin, string reason
1717
where
18+
not f.getFile().isKotlinSourceFile() and
19+
not origin.getFile().isKotlinSourceFile() and
1820
not f.isInDeadScope() and
1921
if f.getAnAccess() instanceof FieldRead
2022
then (

java/ql/src/DeadCode/DeadMethod.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import semmle.code.java.deadcode.DeadCode
1515

1616
from DeadMethod c, Callable origin, string reason
1717
where
18+
not c.getFile().isKotlinSourceFile() and
19+
not origin.getFile().isKotlinSourceFile() and
1820
not c.isInDeadScope() and
1921
if exists(DeadRoot deadRoot | deadRoot = getADeadRoot(c) | deadRoot.getSourceDeclaration() != c)
2022
then (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Test.kt:0:0:0:0 | TestKt | The class TestKt is entirely unused. | Test.kt:0:0:0:0 | TestKt | TestKt |
2+
| Test.kt:1:1:1:26 | DbAddexpr | The class DbAddexpr is entirely unused. | Test.kt:1:1:1:26 | DbAddexpr | DbAddexpr |
3+
| 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 |
4+
| 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 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DeadCode/DeadClass.ql
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| Test.kt:0:0:0:0 | component1 | The method component1 is entirely unused. | Test.kt:0:0:0:0 | component1 | component1 |
2+
| Test.kt:0:0:0:0 | component2 | The method component2 is entirely unused. | Test.kt:0:0:0:0 | component2 | component2 |
3+
| 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 |
4+
| 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 |
5+
| 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 |
6+
| 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 |
7+
| 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 |
8+
| 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 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DeadCode/DeadMethod.ql
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
sealed interface DbAddexpr
2+
3+
class Label<T> {
4+
}
5+
6+
fun <T> getFreshIdLabel(): Label<T> {
7+
return Label()
8+
}
9+
10+
fun foo(): Label<DbAddexpr> {
11+
val x = getFreshIdLabel<DbAddexpr>()
12+
return x
13+
}
14+
15+
fun main1() {
16+
print(foo())
17+
}
18+
19+
class Foo {
20+
data class DC(val x: Int, val y: Int)
21+
22+
fun foo() {
23+
val dc = DC(3, 4)
24+
print(dc.x)
25+
print(dc.y)
26+
}
27+
}
28+
29+
fun main2() {
30+
Foo().foo()
31+
}

0 commit comments

Comments
 (0)