Repository: detekt/detekt (kotlin)

Issue:
False Positive: TopLevelPropertyNaming applying to extension properties
<!-- The bug you're experiencing might have already be reported! -->
<!-- Please search in the [issues](https://github.com/detekt/detekt/issues) before creating one. -->

## Expected Behavior
I have code like this:
```kotlin
private val Foo.myProperty: String = "Hello"
```
and the `TopLevelPropertyNaming` rule is configured to enforce PascalCase for top level properties:
```yaml
  TopLevelPropertyNaming:
    active: true
    constantPattern: '(?:[A-Z][a-z0-9]+)(?:[A-Z]+[a-z0-9]*)*'
    propertyPattern: '(?:[A-Z][a-z0-9]+)(?:[A-Z]+[a-z0-9]*)*'
    privatePropertyPattern: '_?(?:[A-Z][a-z0-9]+)(?:[A-Z]+[a-z0-9]*)*'
```

I would expect `Foo.myProperty` to not be flagged, because this is an extension property on the `Foo` type, not an actual top level property.

## Observed Behavior
Detekt flags `Foo.myProperty` with the `TopLevelPropertyNaming` rule.

## Steps to Reproduce
Configure `TopLevelPropertyNaming` with the above options and test against an extension property.

## Context
We are trying to enforce PascalCase naming for top level properties on our project and many of these false positives are also being flagged.

## Your Environment
* Version of detekt used: 1.23.6
* Version of Gradle used (if applicable): 8.6

Pull request description:
<!-- A similar PR may already be submitted! -->
<!-- Please search among the [Pull requests](https://github.com/detekt/detekt/pulls) before creating one. -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. Link to relevant issues if possible. -->

<!-- For more information, see the [CONTRIBUTING guide](https://github.com/detekt/detekt/blob/main/.github/CONTRIBUTING.md). -->

fixes: #7193

Patch:
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 63f1bfabca04..fe2322353e6b 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
@@ -10,6 +10,7 @@ import io.gitlab.arturbosch.detekt.api.config
 import io.gitlab.arturbosch.detekt.rules.identifierName
 import io.gitlab.arturbosch.detekt.rules.isConstant
 import org.jetbrains.kotlin.psi.KtProperty
+import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
 import org.jetbrains.kotlin.psi.psiUtil.isPrivate
 
 /**
@@ -32,7 +33,8 @@ class TopLevelPropertyNaming(config: Config) : Rule(
 
     override fun visitProperty(property: KtProperty) {
         super.visitProperty(property)
-        if (!property.isTopLevel) return
+        if (!property.isTopLevel || property.psiOrParent.isExtensionDeclaration()) return
+
         if (property.isConstant()) {
             handleConstant(property)
         } else {


Test patch:
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 79933b043bad..9cc141589204 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
@@ -46,6 +46,15 @@ class TopLevelPropertyNamingSpec {
         }
     }
 
+    @Test
+    fun `should not report on top level extension properties`() {
+        val code = """
+            val String._foo = "foo"
+        """.trimIndent()
+
+        assertThat(subject.lint(code)).isEmpty()
+    }
+
     @Nested
     inner class `variables on top level` {