Kotlin: Add a Kotlin 2 copy of the testsuite

This commit is contained in:
Ian Lynagh
2023-10-26 13:13:28 +01:00
parent e6f31c965e
commit 905583e00a
625 changed files with 32808 additions and 0 deletions

View File

@@ -0,0 +1 @@
Violations of Best Practice/Implementation Hiding/AbstractToConcreteCollection.ql

View File

@@ -0,0 +1,5 @@
fun fn(m: MutableList<Int>) {
if (m is ArrayList) {
m.ensureCapacity(5)
}
}

View File

@@ -0,0 +1 @@
Violations of Best Practice/legacy/AutoBoxing.ql

View File

@@ -0,0 +1,5 @@
fun foo(x: Int?) {
if (x != null) {
println(x)
}
}

View File

@@ -0,0 +1,7 @@
import java.io.*
fun test0() {
BufferedReader(FileReader("C:\\test.txt")).use { bw ->
bw.readLine()
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Resource Leaks/CloseReader.ql

View 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")
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Resource Leaks/CloseWriter.ql

View File

@@ -0,0 +1 @@
Violations of Best Practice/Naming Conventions/ConfusingOverloading.ql

View File

@@ -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"
}
}

View 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)
}
}
}

View File

@@ -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 |

View File

@@ -0,0 +1 @@
Likely Bugs/Termination/ConstantLoopCondition.ql

View File

@@ -0,0 +1 @@
DeadCode/DeadClass.ql

View File

@@ -0,0 +1 @@
DeadCode/DeadMethod.ql

View 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()
}

View File

@@ -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. |

View File

@@ -0,0 +1 @@
Violations of Best Practice/Dead Code/DeadRefTypes.ql

View 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)
}

View File

@@ -0,0 +1 @@
Likely Bugs/Statements/EmptyBlock.ql

View File

@@ -0,0 +1,13 @@
class Foo {
abstract inner class Bar {
abstract fun myFun(): Int
}
inner class Baz {
constructor() {
}
fun fn() {
}
}
}

View File

@@ -0,0 +1 @@
Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql

View File

@@ -0,0 +1,3 @@
class ExposesRep {
val strings: Array<String?> = arrayOfNulls(1)
}

View File

@@ -0,0 +1,5 @@
class User {
fun test1(er: ExposesRep) {
er.strings[0] = "Hello world"
}
}

View File

@@ -0,0 +1 @@
Performance/InnerClassCouldBeStatic.ql

View File

@@ -0,0 +1,6 @@
class A {
fun fn1() {}
companion object {
fun fn2() {}
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Comparison/MissingInstanceofInEquals.ql

View File

@@ -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
}
}

View File

@@ -0,0 +1 @@
Advisory/Declarations/MissingOverrideAnnotation.ql

View File

@@ -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
}

View File

@@ -0,0 +1 @@
Architecture/Dependencies/MutualDependency.ql

View File

@@ -0,0 +1,8 @@
package foo.bar
class Foo {
private fun someFun() {
fun say(s: String) { println(s) }
say("Str")
}
}

View File

@@ -0,0 +1 @@
| Test.kt:12:1:12:13 | aaaa | Class and interface names should start in uppercase. |

View File

@@ -0,0 +1 @@
Advisory/Naming/NamingConventionsRefTypes.ql

View File

@@ -0,0 +1,12 @@
class Foo {
fun myFun() {
val nestedStr by lazy {
"another string"
}
fun nestedFun() {
}
}
}
class aaaa {}

View File

@@ -0,0 +1 @@
Likely Bugs/Serialization/NonSerializableField.ql

View File

@@ -0,0 +1,4 @@
class Foo {
fun f(i: Int) {}
fun g(i: Int) { (this::f)(i) }
}

View File

@@ -0,0 +1 @@
Likely Bugs/Serialization/NonSerializableInnerClass.ql

View File

@@ -0,0 +1,11 @@
import java.io.Serializable
class A {
class X : Serializable {
}
}
class B {
inner class X : Serializable {
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Nullness/NullMaybe.ql

View File

@@ -0,0 +1,7 @@
fun fn(b: Boolean) {
var d: Double? = null
if (b) {
d = 1.0
}
println(d!!)
}

View File

@@ -0,0 +1 @@
Advisory/Statements/OneStatementPerLine.ql

View File

@@ -0,0 +1,7 @@
class Foo {
fun foo(): Foo { return this }
fun bar(x: Foo?): Foo? {
return x?.foo()
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Statements/PartiallyMaskedCatch.ql

View File

@@ -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)
}
}

View File

@@ -0,0 +1 @@
Likely Bugs/Statements/ReturnValueIgnored.ql

View 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
}
}

View File

@@ -0,0 +1,13 @@
fun main() {
f(null)
f(true)
f(false)
}
fun f(x: Boolean?) {
if(x == true) {
println("Yes")
} else {
println("No")
}
}

View File

@@ -0,0 +1 @@
Violations of Best Practice/Boolean Logic/SimplifyBoolExpr.ql

View File

@@ -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
}
}

View File

@@ -0,0 +1 @@
Compatibility/JDK9/UnderscoreIdentifier.ql

View File

@@ -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 _ |

View File

@@ -0,0 +1,3 @@
import java
query predicate variables(Variable v) { v.fromSource() }

View File

@@ -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. |

View File

@@ -0,0 +1 @@
Violations of Best Practice/Dead Code/UnreadLocal.ql

View 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%

View 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()
}
}

View File

@@ -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) ... |

View File

@@ -0,0 +1 @@
Language Abuse/UselessNullCheck.ql

View 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) {}

View File

@@ -0,0 +1 @@
DeadCode/UselessParameter.ql

View File

@@ -0,0 +1 @@
Likely Bugs/Arithmetic/WhitespaceContradictsPrecedence.ql

View File

@@ -0,0 +1,5 @@
data class X(val prop: Int)
fun fn(x: X) {
1 - x.prop + 2
}