Code quality improvements + add dedicated DeadRefTypes test

This commit is contained in:
Tamas Vajk
2022-04-08 09:48:26 +02:00
committed by Ian Lynagh
parent cdc7ed0e14
commit 47799ae040
7 changed files with 25 additions and 5 deletions

View File

@@ -1224,7 +1224,7 @@ ktPropertyDelegates(
) )
compiler_generated( compiler_generated(
unique int id: @top ref, unique int id: @element ref,
int kind: int ref int kind: int ref
// 1: Kotlin declaring classes of adapter functions // 1: Declaring classes of adapter functions in Kotlin
); )

View File

@@ -41,6 +41,9 @@ class Element extends @element, Top {
/** Cast this element to a `Documentable`. */ /** Cast this element to a `Documentable`. */
Documentable getDoc() { result = this } Documentable getDoc() { result = this }
/** Holds if this is an auxiliary program element generated by the compiler. */
predicate isCompilerGenerated() { compiler_generated(this, _) }
} }
/** /**

View File

@@ -52,7 +52,7 @@ predicate dead(RefType dead) {
// Insist all source ancestors are dead as well. // Insist all source ancestors are dead as well.
forall(RefType t | t.fromSource() and t = getASuperTypePlus(dead) | dead(t)) and forall(RefType t | t.fromSource() and t = getASuperTypePlus(dead) | dead(t)) and
// Exclude compiler generated classes (e.g. declaring type of adapter functions in Kotlin) // Exclude compiler generated classes (e.g. declaring type of adapter functions in Kotlin)
not compiler_generated(dead, _) not dead.isCompilerGenerated()
} }
from RefType t, string kind from RefType t, string kind

View File

@@ -87,4 +87,4 @@ query predicate modifiers(ClassInstanceExpr e, Method m, string modifier) {
m.hasModifier(modifier) m.hasModifier(modifier)
} }
query predicate compGenerated(Top t, int i) { compiler_generated(t, i) } query predicate compGenerated(Element e, int i) { compiler_generated(e, i) }

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