mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
Kotlin: Add a Kotlin 2 copy of the testsuite
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Implementation Hiding/AbstractToConcreteCollection.ql
|
||||
@@ -0,0 +1,5 @@
|
||||
fun fn(m: MutableList<Int>) {
|
||||
if (m is ArrayList) {
|
||||
m.ensureCapacity(5)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/legacy/AutoBoxing.ql
|
||||
5
java/ql/test-kotlin2/query-tests/AutoBoxing/Test.kt
Normal file
5
java/ql/test-kotlin2/query-tests/AutoBoxing/Test.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Int?) {
|
||||
if (x != null) {
|
||||
println(x)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import java.io.*
|
||||
|
||||
fun test0() {
|
||||
BufferedReader(FileReader("C:\\test.txt")).use { bw ->
|
||||
bw.readLine()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Resource Leaks/CloseReader.ql
|
||||
45
java/ql/test-kotlin2/query-tests/CloseWriter/CloseWriter.kt
Normal file
45
java/ql/test-kotlin2/query-tests/CloseWriter/CloseWriter.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.io.*
|
||||
|
||||
fun test0() {
|
||||
val bw = BufferedWriter(FileWriter("C:\\test.txt"))
|
||||
bw.write("test")
|
||||
}
|
||||
|
||||
fun test1() {
|
||||
BufferedWriter(FileWriter("C:\\test.txt")).use { bw ->
|
||||
bw.write("test")
|
||||
}
|
||||
}
|
||||
|
||||
fun test2() {
|
||||
val bw = FileOutputStream(File.createTempFile("","")).bufferedWriter()
|
||||
bw.write("test")
|
||||
}
|
||||
|
||||
fun test3() {
|
||||
FileOutputStream(File.createTempFile("","")).bufferedWriter().use { bw ->
|
||||
bw.write("test")
|
||||
}
|
||||
}
|
||||
|
||||
fun test4() {
|
||||
val bw = OutputStreamWriter(FileOutputStream(File.createTempFile("",""))).buffered()
|
||||
bw.write("test")
|
||||
}
|
||||
|
||||
fun test5() {
|
||||
OutputStreamWriter(FileOutputStream(File.createTempFile("",""))).buffered().use { bw ->
|
||||
bw.write("test")
|
||||
}
|
||||
}
|
||||
|
||||
fun test6() {
|
||||
val bw = OutputStreamWriter(FileOutputStream(File.createTempFile("","")))
|
||||
bw.write("test")
|
||||
}
|
||||
|
||||
fun test7() {
|
||||
OutputStreamWriter(FileOutputStream(File.createTempFile("",""))).use { bw ->
|
||||
bw.write("test")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Resource Leaks/CloseWriter.ql
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Naming Conventions/ConfusingOverloading.ql
|
||||
@@ -0,0 +1,20 @@
|
||||
class C {
|
||||
var p: Int
|
||||
get() = 1
|
||||
set(value) {}
|
||||
fun fn() {
|
||||
val prop = C::p
|
||||
prop(this)
|
||||
}
|
||||
}
|
||||
|
||||
class A {
|
||||
fun <T : Any> fn(value: T, i: Int = 1) {}
|
||||
fun fn(value: String, i: Int = 1) {}
|
||||
}
|
||||
|
||||
class Foo {
|
||||
val str by lazy {
|
||||
"someString"
|
||||
}
|
||||
}
|
||||
24
java/ql/test-kotlin2/query-tests/ConstantLoopCondition/A.kt
Normal file
24
java/ql/test-kotlin2/query-tests/ConstantLoopCondition/A.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
fun fn0(f: Function0<Unit>) = f()
|
||||
|
||||
fun fn1() {
|
||||
var c = true
|
||||
while (c) { // TODO: false positive
|
||||
fn0 {
|
||||
c = false
|
||||
}
|
||||
}
|
||||
|
||||
var d = true
|
||||
while (d) {
|
||||
fn0 {
|
||||
println(d)
|
||||
}
|
||||
}
|
||||
|
||||
val e = true
|
||||
while (e) {
|
||||
fn0 {
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
| A.kt:5:12:5:12 | c | $@ might not terminate, as this loop condition is constant within the loop. | A.kt:5:5:9:5 | while (...) | Loop |
|
||||
| A.kt:12:12:12:12 | d | $@ might not terminate, as this loop condition is constant within the loop. | A.kt:12:5:16:5 | while (...) | Loop |
|
||||
| A.kt:19:12:19:12 | e | $@ might not terminate, as this loop condition is constant within the loop. | A.kt:19:5:23:5 | while (...) | Loop |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Termination/ConstantLoopCondition.ql
|
||||
@@ -0,0 +1 @@
|
||||
DeadCode/DeadClass.ql
|
||||
@@ -0,0 +1 @@
|
||||
DeadCode/DeadMethod.ql
|
||||
31
java/ql/test-kotlin2/query-tests/DeadCode/Test.kt
Normal file
31
java/ql/test-kotlin2/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()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
| test.kt:1:1:1:20 | C1 | Unused class: C1 is not referenced within this codebase. If not used as an external API it should be removed. |
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Dead Code/DeadRefTypes.ql
|
||||
15
java/ql/test-kotlin2/query-tests/DeadRefTypes/test.kt
Normal file
15
java/ql/test-kotlin2/query-tests/DeadRefTypes/test.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
private class C1 { }
|
||||
|
||||
private class C2 { }
|
||||
|
||||
fun fn() {
|
||||
val c = C2()
|
||||
}
|
||||
|
||||
fun fn1() = 5
|
||||
|
||||
fun fn2(f: () -> Unit) = f()
|
||||
|
||||
fun adapted() {
|
||||
fn2(::fn1)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Statements/EmptyBlock.ql
|
||||
13
java/ql/test-kotlin2/query-tests/EmptyBlock/Test.kt
Normal file
13
java/ql/test-kotlin2/query-tests/EmptyBlock/Test.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
class Foo {
|
||||
abstract inner class Bar {
|
||||
abstract fun myFun(): Int
|
||||
}
|
||||
|
||||
inner class Baz {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
fun fn() {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql
|
||||
@@ -0,0 +1,3 @@
|
||||
class ExposesRep {
|
||||
val strings: Array<String?> = arrayOfNulls(1)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class User {
|
||||
fun test1(er: ExposesRep) {
|
||||
er.strings[0] = "Hello world"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Performance/InnerClassCouldBeStatic.ql
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
fun fn1() {}
|
||||
companion object {
|
||||
fun fn2() {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Comparison/MissingInstanceofInEquals.ql
|
||||
@@ -0,0 +1,25 @@
|
||||
data class D(val x: Int) {}
|
||||
|
||||
data class E(val x: Int) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return (other as? E)?.x == this.x
|
||||
}
|
||||
}
|
||||
|
||||
data class F(val x: Int) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return other != null && other::class == this::class
|
||||
}
|
||||
}
|
||||
|
||||
data class G(val x: Int) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return other != null && other.javaClass == this.javaClass
|
||||
}
|
||||
}
|
||||
|
||||
data class H(val x: Int) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return other != null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Advisory/Declarations/MissingOverrideAnnotation.ql
|
||||
@@ -0,0 +1,25 @@
|
||||
open class A {
|
||||
open fun m(): Int {
|
||||
return 23
|
||||
}
|
||||
|
||||
private fun n() {}
|
||||
|
||||
open val p = 5
|
||||
}
|
||||
|
||||
interface I {
|
||||
fun o()
|
||||
}
|
||||
|
||||
class B : A(), I {
|
||||
override fun m(): Int {
|
||||
return 42
|
||||
}
|
||||
|
||||
fun n() {}
|
||||
|
||||
override fun o() { }
|
||||
|
||||
override val p = 7
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Architecture/Dependencies/MutualDependency.ql
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo.bar
|
||||
|
||||
class Foo {
|
||||
private fun someFun() {
|
||||
fun say(s: String) { println(s) }
|
||||
say("Str")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
| Test.kt:12:1:12:13 | aaaa | Class and interface names should start in uppercase. |
|
||||
@@ -0,0 +1 @@
|
||||
Advisory/Naming/NamingConventionsRefTypes.ql
|
||||
@@ -0,0 +1,12 @@
|
||||
class Foo {
|
||||
fun myFun() {
|
||||
val nestedStr by lazy {
|
||||
"another string"
|
||||
}
|
||||
|
||||
fun nestedFun() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class aaaa {}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Serialization/NonSerializableField.ql
|
||||
@@ -0,0 +1,4 @@
|
||||
class Foo {
|
||||
fun f(i: Int) {}
|
||||
fun g(i: Int) { (this::f)(i) }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Serialization/NonSerializableInnerClass.ql
|
||||
@@ -0,0 +1,11 @@
|
||||
import java.io.Serializable
|
||||
|
||||
class A {
|
||||
class X : Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
inner class X : Serializable {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Nullness/NullMaybe.ql
|
||||
7
java/ql/test-kotlin2/query-tests/NullMaybe/Test.kt
Normal file
7
java/ql/test-kotlin2/query-tests/NullMaybe/Test.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
fun fn(b: Boolean) {
|
||||
var d: Double? = null
|
||||
if (b) {
|
||||
d = 1.0
|
||||
}
|
||||
println(d!!)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Advisory/Statements/OneStatementPerLine.ql
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
fun foo(): Foo { return this }
|
||||
|
||||
fun bar(x: Foo?): Foo? {
|
||||
return x?.foo()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Statements/PartiallyMaskedCatch.ql
|
||||
@@ -0,0 +1,31 @@
|
||||
fun fn0() { throw java.io.IOException() }
|
||||
|
||||
fun fn1() {
|
||||
try {
|
||||
throw java.io.IOException()
|
||||
} catch (e: java.io.FileNotFoundException) {
|
||||
println(e)
|
||||
} catch (e: java.io.IOException) {
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
|
||||
fun fn2() {
|
||||
try {
|
||||
fn0()
|
||||
} catch (e: java.io.FileNotFoundException) {
|
||||
println(e)
|
||||
} catch (e: java.io.IOException) {
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
|
||||
fun fn3() {
|
||||
try {
|
||||
throw java.io.FileNotFoundException()
|
||||
} catch (e: java.io.FileNotFoundException) {
|
||||
println(e)
|
||||
} catch (e: java.io.IOException) { // TODO: False negative
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Statements/ReturnValueIgnored.ql
|
||||
16
java/ql/test-kotlin2/query-tests/ReturnValueIgnored/Test.kt
Normal file
16
java/ql/test-kotlin2/query-tests/ReturnValueIgnored/Test.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
class Foo {
|
||||
fun foo(): Int { return 5 }
|
||||
fun bar() {
|
||||
val x0 = foo()
|
||||
val x1 = foo()
|
||||
val x2 = foo()
|
||||
val x3 = foo()
|
||||
val x4 = foo()
|
||||
val x5 = foo()
|
||||
val x6 = foo()
|
||||
val x7 = foo()
|
||||
val x8 = foo()
|
||||
val x9 = foo()
|
||||
val x = if (true) { foo() } else 6
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fun main() {
|
||||
f(null)
|
||||
f(true)
|
||||
f(false)
|
||||
}
|
||||
|
||||
fun f(x: Boolean?) {
|
||||
if(x == true) {
|
||||
println("Yes")
|
||||
} else {
|
||||
println("No")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Boolean Logic/SimplifyBoolExpr.ql
|
||||
@@ -0,0 +1,11 @@
|
||||
fun fn() {
|
||||
try {
|
||||
val l = listOf(1, 2, 3)
|
||||
l.forEachIndexed { index, _ -> println(index) }
|
||||
|
||||
val p = Pair(1, 2)
|
||||
val (first, _) = p
|
||||
} catch (_: Exception) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Compatibility/JDK9/UnderscoreIdentifier.ql
|
||||
@@ -0,0 +1,7 @@
|
||||
| Test.kt:3:13:3:13 | List<Integer> l |
|
||||
| Test.kt:4:28:4:32 | index |
|
||||
| Test.kt:4:35:4:35 | p1 |
|
||||
| Test.kt:6:13:6:13 | Pair<Integer,Integer> p |
|
||||
| Test.kt:7:14:7:18 | int first |
|
||||
| Test.kt:7:26:7:26 | Pair<Integer,Integer> tmp0_container |
|
||||
| Test.kt:8:14:8:25 | Exception _ |
|
||||
@@ -0,0 +1,3 @@
|
||||
import java
|
||||
|
||||
query predicate variables(Variable v) { v.fromSource() }
|
||||
@@ -0,0 +1,3 @@
|
||||
| test.kt:8:10:8:10 | int e | Variable 'int e' is never read. |
|
||||
| test.kt:14:11:14:13 | int idx | Variable 'int idx' is never read. |
|
||||
| test.kt:14:16:14:16 | int e | Variable 'int e' is never read. |
|
||||
@@ -0,0 +1 @@
|
||||
Violations of Best Practice/Dead Code/UnreadLocal.ql
|
||||
25
java/ql/test-kotlin2/query-tests/UnreadLocal/test.kt
Normal file
25
java/ql/test-kotlin2/query-tests/UnreadLocal/test.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
fun fn0(size: Int) {
|
||||
for (idx in 1..size) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun fn1(a: Array<Int>) {
|
||||
for (e in a) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun fn2(a: Array<Int>) {
|
||||
for ((idx, e) in a.withIndex()) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun fn3() {
|
||||
for (i in 1 until 10) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
// Diagnostic Matches: % Couldn't find a Java equivalent function to kotlin.Int.rangeTo in java.lang.Integer%
|
||||
18
java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt
Normal file
18
java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
fun fn(x:Any?, y: Any?) {
|
||||
if (x == null && y == null) {
|
||||
throw Exception()
|
||||
}
|
||||
|
||||
if (x != null) {
|
||||
println("x not null")
|
||||
} else if (y != null) {
|
||||
println("y not null")
|
||||
}
|
||||
}
|
||||
|
||||
fun fn0(o: Any?) {
|
||||
if (o != null) {
|
||||
o?.toString()
|
||||
o.toString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
| Test.kt:15:12:15:21 | ... (value equals) ... | This check is useless. $@ cannot be null at this check, since it is guarded by $@. | Test.kt:15:9:15:9 | tmp0_safe_receiver | tmp0_safe_receiver | Test.kt:14:9:14:17 | ... (value not-equals) ... | ... (value not-equals) ... |
|
||||
@@ -0,0 +1 @@
|
||||
Language Abuse/UselessNullCheck.ql
|
||||
24
java/ql/test-kotlin2/query-tests/UselessParameter/Test.kt
Normal file
24
java/ql/test-kotlin2/query-tests/UselessParameter/Test.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
interface A<T, V> {
|
||||
fun setValue(a: T, b: V)
|
||||
}
|
||||
|
||||
class B : A<B, Int> {
|
||||
override fun setValue(a: B, b: Int) {
|
||||
println("a")
|
||||
}
|
||||
}
|
||||
|
||||
fun fn(a: Int = 10) {}
|
||||
|
||||
class C {
|
||||
companion object {}
|
||||
}
|
||||
|
||||
object O {}
|
||||
|
||||
fun C.fn() {}
|
||||
fun C.Companion.fn() {}
|
||||
fun O.fn() {}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun fn2(a: Int) {}
|
||||
@@ -0,0 +1 @@
|
||||
DeadCode/UselessParameter.ql
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Arithmetic/WhitespaceContradictsPrecedence.ql
|
||||
@@ -0,0 +1,5 @@
|
||||
data class X(val prop: Int)
|
||||
|
||||
fun fn(x: X) {
|
||||
1 - x.prop + 2
|
||||
}
|
||||
Reference in New Issue
Block a user