Skip to content

Commit 7eaef0c

Browse files
authored
Merge pull request github#11436 from igfoo/igfoo/NamingConventionsRefTypes
Kotlin: Enable java/misnamed-type query
2 parents c779b8f + 3b31b50 commit 7eaef0c

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,14 +1507,15 @@ open class KotlinFileExtractor(
15071507
}
15081508
is IrFunction -> {
15091509
if (s.isLocalFunction()) {
1510-
val classId = extractGeneratedClass(s, listOf(pluginContext.irBuiltIns.anyType))
1510+
val compilerGeneratedKindOverride = if (s.origin == IrDeclarationOrigin.ADAPTER_FOR_CALLABLE_REFERENCE) {
1511+
CompilerGeneratedKinds.DECLARING_CLASSES_OF_ADAPTER_FUNCTIONS
1512+
} else {
1513+
null
1514+
}
1515+
val classId = extractGeneratedClass(s, listOf(pluginContext.irBuiltIns.anyType), compilerGeneratedKindOverride = compilerGeneratedKindOverride)
15111516
extractLocalTypeDeclStmt(classId, s, callable, parent, idx)
15121517
val ids = getLocallyVisibleFunctionLabels(s)
15131518
tw.writeKtLocalFunction(ids.function)
1514-
1515-
if (s.origin == IrDeclarationOrigin.ADAPTER_FOR_CALLABLE_REFERENCE) {
1516-
tw.writeCompiler_generated(classId, CompilerGeneratedKinds.DECLARING_CLASSES_OF_ADAPTER_FUNCTIONS.kind)
1517-
}
15181519
} else {
15191520
logger.errorElement("Expected to find local function", s)
15201521
}
@@ -4685,7 +4686,7 @@ open class KotlinFileExtractor(
46854686
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.FunctionReference"))?.owner?.typeWith()
46864687
?: pluginContext.irBuiltIns.anyType
46874688

4688-
val classId = extractGeneratedClass(ids, listOf(baseClass, fnInterfaceType), locId, functionReferenceExpr, declarationParent, { it.valueParameters.size == 1 }) {
4689+
val classId = extractGeneratedClass(ids, listOf(baseClass, fnInterfaceType), locId, functionReferenceExpr, declarationParent, null, { it.valueParameters.size == 1 }) {
46894690
// The argument to FunctionReference's constructor is the function arity.
46904691
extractConstantInteger(type.arguments.size - 1, locId, it, 0, ids.constructor, it)
46914692
}
@@ -5389,13 +5390,15 @@ open class KotlinFileExtractor(
53895390
locId: Label<DbLocation>,
53905391
elementToReportOn: IrElement,
53915392
declarationParent: IrDeclarationParent,
5393+
compilerGeneratedKindOverride: CompilerGeneratedKinds? = null,
53925394
superConstructorSelector: (IrFunction) -> Boolean = { it.valueParameters.isEmpty() },
5393-
extractSuperconstructorArgs: (Label<DbSuperconstructorinvocationstmt>) -> Unit = {}
5395+
extractSuperconstructorArgs: (Label<DbSuperconstructorinvocationstmt>) -> Unit = {},
53945396
): Label<out DbClass> {
53955397
// Write class
53965398
val id = ids.type.javaResult.id.cast<DbClass>()
53975399
val pkgId = extractPackage("")
53985400
tw.writeClasses(id, "", pkgId, id)
5401+
tw.writeCompiler_generated(id, (compilerGeneratedKindOverride ?: CompilerGeneratedKinds.CALLABLE_CLASS).kind)
53995402
tw.writeHasLocation(id, locId)
54005403

54015404
// Extract constructor
@@ -5442,11 +5445,15 @@ open class KotlinFileExtractor(
54425445
/**
54435446
* Extracts the class around a local function or a lambda. The superclass must have a no-arg constructor.
54445447
*/
5445-
private fun extractGeneratedClass(localFunction: IrFunction, superTypes: List<IrType>) : Label<out DbClass> {
5448+
private fun extractGeneratedClass(
5449+
localFunction: IrFunction,
5450+
superTypes: List<IrType>,
5451+
compilerGeneratedKindOverride: CompilerGeneratedKinds? = null
5452+
) : Label<out DbClass> {
54465453
with("generated class", localFunction) {
54475454
val ids = getLocallyVisibleFunctionLabels(localFunction)
54485455

5449-
val id = extractGeneratedClass(ids, superTypes, tw.getLocation(localFunction), localFunction, localFunction.parent)
5456+
val id = extractGeneratedClass(ids, superTypes, tw.getLocation(localFunction), localFunction, localFunction.parent, compilerGeneratedKindOverride = compilerGeneratedKindOverride)
54505457

54515458
// Extract local function as a member
54525459
extractFunction(localFunction, id, extractBody = true, extractMethodAndParameterTypeAccesses = true, null, listOf())
@@ -5520,5 +5527,6 @@ open class KotlinFileExtractor(
55205527
DEFAULT_ARGUMENTS_METHOD(10),
55215528
INTERFACE_FORWARDER(11),
55225529
ENUM_CONSTRUCTOR_ARGUMENT(12),
5530+
CALLABLE_CLASS(13),
55235531
}
55245532
}

java/ql/lib/semmle/code/java/Element.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Element extends @element, Top {
7171
i = 11 and result = "Forwarder for a Kotlin class inheriting an interface default method"
7272
or
7373
i = 12 and result = "Argument for enum constructor call"
74+
or
75+
i = 13 and result = "The class around a local function, a lambda, or a function reference"
7476
)
7577
}
7678
}

java/ql/src/Advisory/Naming/NamingConventionsRefTypes.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import java
1212

1313
from RefType t
1414
where
15-
t.getFile().isJavaSourceFile() and
15+
t.fromSource() and
1616
not t instanceof AnonymousClass and
17+
not t.isCompilerGenerated() and
1718
not t.getName().substring(0, 1).toUpperCase() = t.getName().substring(0, 1)
1819
select t, "Class and interface names should start in uppercase."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `java/misnamed-type` is now enabled for Kotlin.

java/ql/test/kotlin/library-tests/reflection/reflection.expected

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,58 @@ compGenerated
282282
| file://<external>/LongRange.class:0:0:0:0 | forEach | Forwarder for a Kotlin class inheriting an interface default method |
283283
| file://<external>/LongRange.class:0:0:0:0 | spliterator | Forwarder for a Kotlin class inheriting an interface default method |
284284
| file://<external>/String.class:0:0:0:0 | isEmpty | Forwarder for a Kotlin class inheriting an interface default method |
285+
| reflection.kt:7:49:7:54 | new Function2<Ccc,Integer,Double>(...) { ... } | The class around a local function, a lambda, or a function reference |
286+
| reflection.kt:10:38:10:42 | new KProperty1<C,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
287+
| reflection.kt:14:38:14:44 | new Function1<C,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
288+
| reflection.kt:15:35:15:41 | new KProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
289+
| reflection.kt:17:45:17:49 | new KMutableProperty1<C,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
290+
| reflection.kt:21:44:21:50 | new Function2<C,Integer,Unit>(...) { ... } | The class around a local function, a lambda, or a function reference |
291+
| reflection.kt:22:42:22:48 | new KMutableProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
292+
| reflection.kt:24:46:24:64 | new Function1<KCallable<?>,Boolean>(...) { ... } | The class around a local function, a lambda, or a function reference |
285293
| reflection.kt:33:9:33:23 | getP0 | Default property accessor |
286294
| reflection.kt:34:9:34:23 | getP1 | Default property accessor |
287295
| reflection.kt:34:9:34:23 | setP1 | Default property accessor |
296+
| reflection.kt:50:13:50:28 | new KProperty1<String,Character>(...) { ... } | The class around a local function, a lambda, or a function reference |
297+
| reflection.kt:51:13:51:28 | new KProperty0<Character>(...) { ... } | The class around a local function, a lambda, or a function reference |
298+
| reflection.kt:60:17:60:32 | new Function2<Generic<Integer>,Integer,String>(...) { ... } | The class around a local function, a lambda, or a function reference |
299+
| reflection.kt:61:17:61:34 | new Function1<Integer,String>(...) { ... } | The class around a local function, a lambda, or a function reference |
300+
| reflection.kt:62:17:62:34 | new Function1<Generic<Integer>,String>(...) { ... } | The class around a local function, a lambda, or a function reference |
301+
| reflection.kt:63:17:63:36 | new Function0<String>(...) { ... } | The class around a local function, a lambda, or a function reference |
302+
| reflection.kt:64:17:64:34 | new Function1<Generic<Integer>,String>(...) { ... } | The class around a local function, a lambda, or a function reference |
303+
| reflection.kt:65:17:65:36 | new Function0<String>(...) { ... } | The class around a local function, a lambda, or a function reference |
304+
| reflection.kt:67:17:67:32 | new KMutableProperty1<Generic<Integer>,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
305+
| reflection.kt:68:17:68:34 | new KMutableProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
306+
| reflection.kt:70:17:70:30 | new KProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
307+
| reflection.kt:71:17:71:34 | new KProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
308+
| reflection.kt:72:17:72:35 | new KMutableProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
288309
| reflection.kt:83:17:83:28 | getValue | Default property accessor |
310+
| reflection.kt:90:18:90:24 | new Function1<String,Inner<String>>(...) { ... } | The class around a local function, a lambda, or a function reference |
311+
| reflection.kt:97:14:97:21 | new Function1<String,Class2<String>>(...) { ... } | The class around a local function, a lambda, or a function reference |
312+
| reflection.kt:98:14:98:17 | new Function1<String,Unit>(...) { ... } | The class around a local function, a lambda, or a function reference |
313+
| reflection.kt:99:14:99:29 | new Function1<String,Inner<String>>(...) { ... } | The class around a local function, a lambda, or a function reference |
289314
| reflection.kt:105:18:105:31 | getProp1 | Default property accessor |
290315
| reflection.kt:105:18:105:31 | setProp1 | Default property accessor |
316+
| reflection.kt:109:17:109:27 | new KMutableProperty0<Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
317+
| reflection.kt:115:9:115:27 | | The class around a local function, a lambda, or a function reference |
318+
| reflection.kt:116:40:116:44 | new Function1<Integer,Unit>(...) { ... } | The class around a local function, a lambda, or a function reference |
291319
| reflection.kt:126:9:126:13 | | Declaring classes of adapter functions in Kotlin |
320+
| reflection.kt:126:9:126:13 | new Function0<Unit>(...) { ... } | The class around a local function, a lambda, or a function reference |
292321
| reflection.kt:131:1:131:50 | takesOptionalParam$default | Forwarder for Kotlin calls that need default arguments filling in |
293322
| reflection.kt:134:21:134:40 | | Declaring classes of adapter functions in Kotlin |
323+
| reflection.kt:134:21:134:40 | new Function1<Integer,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
294324
| reflection.kt:140:5:140:54 | takesOptionalParam$default | Forwarder for Kotlin calls that need default arguments filling in |
295325
| reflection.kt:144:21:144:41 | | Declaring classes of adapter functions in Kotlin |
326+
| reflection.kt:144:21:144:41 | new Function1<Integer,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
296327
| reflection.kt:145:32:145:70 | | Declaring classes of adapter functions in Kotlin |
328+
| reflection.kt:145:32:145:70 | new Function2<MemberOptionalsTest,Integer,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
297329
| reflection.kt:150:1:150:60 | extTakesOptionalParam$default | Forwarder for Kotlin calls that need default arguments filling in |
298330
| reflection.kt:153:21:153:44 | | Declaring classes of adapter functions in Kotlin |
331+
| reflection.kt:153:21:153:44 | new Function1<Integer,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
299332
| reflection.kt:154:33:154:61 | | Declaring classes of adapter functions in Kotlin |
333+
| reflection.kt:154:33:154:61 | new Function2<String,Integer,Integer>(...) { ... } | The class around a local function, a lambda, or a function reference |
300334
| reflection.kt:157:1:157:49 | ConstructorOptional | Forwarder for Kotlin calls that need default arguments filling in |
301335
| reflection.kt:162:25:162:45 | | Declaring classes of adapter functions in Kotlin |
336+
| reflection.kt:162:25:162:45 | new Function1<Integer,ConstructorOptional>(...) { ... } | The class around a local function, a lambda, or a function reference |
302337
propertyReferenceOverrides
303338
| reflection.kt:10:38:10:42 | ...::... | reflection.kt:10:38:10:42 | get | kotlin.reflect.KProperty1<C,Integer>.get(Reflection.C) |
304339
| reflection.kt:10:38:10:42 | ...::... | reflection.kt:10:38:10:42 | invoke | kotlin.jvm.functions.Function1<C,Integer>.invoke(Reflection.C) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Test.kt:12:1:12:13 | aaaa | Class and interface names should start in uppercase. |

0 commit comments

Comments
 (0)