Skip to content

Commit c927ac9

Browse files
authored
Merge pull request #10048 from igfoo/igfoo/NotNullExpr
Kotlin: Remove not-null-expressions from KotlinUsesExtractor
2 parents 19c2ca8 + 48e6b4c commit c927ac9

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,12 @@ open class KotlinUsesExtractor(
309309
f.valueParameters
310310
}
311311

312-
val paramTypes = parameters.map { useType(erase(it.type)) }
313-
val signature = paramTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { it.javaResult.signature!! }
312+
val paramSigs = parameters.map { useType(erase(it.type)).javaResult.signature }.requireNoNullsOrNull()
313+
if (paramSigs == null) {
314+
logger.warn("Null signature for a parameter of ${f.name}")
315+
return
316+
}
317+
val signature = paramSigs.joinToString(separator = ",", prefix = "(", postfix = ")")
314318
dependencyCollector?.addDependency(f, signature)
315319
externalClassExtractor.extractLater(f, signature)
316320
}
@@ -440,15 +444,20 @@ open class KotlinUsesExtractor(
440444
return null
441445
}
442446

447+
val fqName = c.fqNameWhenAvailable
448+
if (fqName == null) {
449+
return null
450+
}
451+
443452
fun tryGetPair(arity: Int): Pair<IrClass, List<IrTypeArgument>?>? {
444-
val replaced = pluginContext.referenceClass(c.fqNameWhenAvailable!!)?.owner ?: return null
453+
val replaced = pluginContext.referenceClass(fqName)?.owner ?: return null
445454
return Pair(replaced, List(arity) { makeTypeProjection(pluginContext.irBuiltIns.anyNType, Variance.INVARIANT) })
446455
}
447456

448457
// The list of types handled here match https://github.com/JetBrains/kotlin/blob/d7c7d1efd2c0983c13b175e9e4b1cda979521159/plugins/parcelize/parcelize-compiler/src/org/jetbrains/kotlin/parcelize/ir/AndroidSymbols.kt
449458
// Specifically, types are added for generic types created in AndroidSymbols.kt.
450459
// This replacement is from a raw type to its matching parameterized type with `Object` type arguments.
451-
return when (c.fqNameWhenAvailable?.asString()) {
460+
return when (fqName.asString()) {
452461
"java.util.ArrayList" -> tryGetPair(1)
453462
"java.util.LinkedHashMap" -> tryGetPair(2)
454463
"java.util.LinkedHashSet" -> tryGetPair(1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.codeql
2+
3+
/**
4+
* Turns this list of nullable elements into a list of non-nullable
5+
* elements if they are all non-null, or returns null otherwise.
6+
*/
7+
public fun <T : Any> List<T?>.requireNoNullsOrNull(): List<T>? {
8+
try {
9+
return this.requireNoNulls()
10+
} catch (e: IllegalArgumentException) {
11+
return null;
12+
}
13+
}

0 commit comments

Comments
 (0)