mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
UnusedPrivateClass: don't report imported classes (#2812)
This commit is contained in:
committed by
GitHub
parent
194ace6ad7
commit
82108b7d24
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.psi.KtDoubleColonExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtFunctionType
|
||||
import org.jetbrains.kotlin.psi.KtImportDirective
|
||||
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
@@ -63,9 +64,16 @@ class UnusedPrivateClass(config: Config = Config.empty) : Rule(config) {
|
||||
|
||||
private val privateClasses = mutableSetOf<KtNamedDeclaration>()
|
||||
private val namedClasses = mutableSetOf<String>()
|
||||
private val importDirectives = mutableSetOf<String>()
|
||||
|
||||
fun getUnusedClasses(): List<KtNamedDeclaration> {
|
||||
return privateClasses.filter { it.nameAsSafeName.identifier !in namedClasses }
|
||||
return privateClasses.filter { !it.isUsed() }
|
||||
}
|
||||
|
||||
private fun KtNamedDeclaration.isUsed(): Boolean {
|
||||
if (nameAsSafeName.identifier in namedClasses) return true
|
||||
val fqName = fqName?.asString()
|
||||
return fqName != null && importDirectives.any { it.startsWith(fqName) }
|
||||
}
|
||||
|
||||
override fun visitClass(klass: KtClass) {
|
||||
@@ -78,6 +86,11 @@ class UnusedPrivateClass(config: Config = Config.empty) : Rule(config) {
|
||||
super.visitClass(klass)
|
||||
}
|
||||
|
||||
override fun visitImportDirective(importDirective: KtImportDirective) {
|
||||
importDirectives.addIfNotNull(importDirective.importedFqName?.asString())
|
||||
super.visitImportDirective(importDirective)
|
||||
}
|
||||
|
||||
override fun visitAnnotationEntry(annotationEntry: KtAnnotationEntry) {
|
||||
namedClasses.addIfNotNull(annotationEntry.typeReference?.text)
|
||||
super.visitAnnotationEntry(annotationEntry)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.gitlab.arturbosch.detekt.rules.style
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import io.gitlab.arturbosch.detekt.test.lint
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
@@ -361,5 +362,27 @@ class UnusedPrivateClassSpec : Spek({
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report imported enum class - #2809") {
|
||||
val code = """
|
||||
package com.example
|
||||
|
||||
import com.example.C.E.E1
|
||||
|
||||
class C {
|
||||
fun test() {
|
||||
println(E1)
|
||||
}
|
||||
|
||||
private enum class E {
|
||||
E1,
|
||||
E2,
|
||||
E3
|
||||
}
|
||||
}
|
||||
"""
|
||||
val findings = UnusedPrivateClass().lint(code)
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user