diff --git a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt index 49be8502a4a..2d95a078879 100644 --- a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt +++ b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt @@ -822,7 +822,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) { flowOfChecked("hello") return 42 } - """ + """.trimIndent() val findings = subject.compileAndLintWithContext(env, code) assertThat(findings) .singleElement() @@ -913,7 +913,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) { fun foo() { flowOf(1, 2, 3) } - """ + """.trimIndent() val findings = subject.compileAndLintWithContext(env, code) assertThat(findings) @@ -931,7 +931,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) { flowOf(1, 2, 3) .onEach { println(it) } } - """ + """.trimIndent() val findings = subject.compileAndLintWithContext(env, code) assertThat(findings) @@ -946,7 +946,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) { import kotlinx.coroutines.flow.flowOf fun foo() = flowOf(1, 2, 3) - """ + """.trimIndent() val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } @@ -961,7 +961,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) { .onEach { println(it) } .collect() } - """ + """.trimIndent() val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } diff --git a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNaming.kt b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNaming.kt index f7316344865..d0ab166f7fa 100644 --- a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNaming.kt +++ b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNaming.kt @@ -38,6 +38,8 @@ class TopLevelPropertyNaming(config: Config = Config.empty) : Rule(config) { private val privatePropertyPattern: Regex by config("_?[A-Za-z][_A-Za-z0-9]*") { it.toRegex() } override fun visitProperty(property: KtProperty) { + super.visitProperty(property) + if (!property.isTopLevel) return if (property.isConstant()) { handleConstant(property) } else { diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNamingSpec.kt index 3d238104ba6..79e1b4246ca 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNamingSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/TopLevelPropertyNamingSpec.kt @@ -12,18 +12,12 @@ class TopLevelPropertyNamingSpec { val subject = TopLevelPropertyNaming() @Test - fun `should use custom name top level propeties`() { - assertThat( - TopLevelPropertyNaming(TestConfig(mapOf(TopLevelPropertyNaming.CONSTANT_PATTERN to "^lowerCaseConst$"))).compileAndLint( - """ - class Foo{ - companion object { - const val lowerCaseConst = "" - } - } - """.trimIndent() - ) - ).isEmpty() + fun `should use custom name top level properties`() { + val code = """ + const val lowerCaseConst = "" + """.trimIndent() + val subject = TopLevelPropertyNaming(TestConfig(TopLevelPropertyNaming.CONSTANT_PATTERN to "^lowerCaseConst$")) + assertThat(subject.lint(code)).isEmpty() } @Nested @@ -87,4 +81,19 @@ class TopLevelPropertyNamingSpec { io.gitlab.arturbosch.detekt.test.assertThat(subject.lint(code)).hasSize(1) } } + + @Test + fun `should not care about no top level properties`() { + val code = """ + class Foo{ + val __baz = "" + companion object { + const val __bar = "" + val __foo = "" + } + } + """.trimIndent() + + assertThat(subject.compileAndLint(code)).isEmpty() + } }