Skip to content

Commit

Permalink
PropertyUsedBeforeDeclaration: fix false positive in nested/inner cla…
Browse files Browse the repository at this point in the history
…ss (detekt#6139)
  • Loading branch information
t-kameyama authored and cortinico committed Jul 15, 2023
1 parent ad80630 commit dee1ee8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PropertyUsedBeforeDeclaration(config: Config = Config.empty) : Rule(config
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
super.visitClassOrObject(classOrObject)

val classMembers = classOrObject.body?.children ?: return
val classMembers = classOrObject.body?.children?.filterNot { it is KtClassOrObject } ?: return

val allProperties = classMembers.filterIsInstance<KtProperty>().mapNotNull {
val name = it.name ?: return@mapNotNull null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,44 @@ class PropertyUsedBeforeDeclarationSpec(private val env: KotlinCoreEnvironment)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

@Test
fun `used before declaration in nested object`() {
val code = """
object Outer {
object O {
val inner = outer1
}
class C {
val inner = outer2
}
annotation class Ann(val value: String)
interface I {
fun f(@Ann(outer3) namedProp: String)
}
val outer1 = "value1"
val outer2 = "value2"
const val outer3 = "value3"
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

@Test
fun `used before declaration in inner class`() {
val code = """
class A {
inner class B {
val inner = outer
}
val outer = "value"
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}

0 comments on commit dee1ee8

Please sign in to comment.