Repository: detekt/detekt (kotlin)

Issue:
False positive for unused imports
Detekt is reporting Unused Import for Kdocs references.
As you can see in this [class](https://github.com/GetStream/stream-chat-android/blob/report-detekt-issue/stream-chat-android-ui-components/src/main/kotlin/io/getstream/chat/android/ui/channel/list/ChannelListViewStyle.kt#L27) the `ChannelViewHolder` is referenced into the Kdocs comments, but Detekt doesn't parse it properly and thinks there is an unused import

It seems to be reported on #457 and fixed with #458 but it is not working for me

## Expected Behavior
Any import that is used on Kdocs shouldn't be reported as an issue

## Steps to Reproduce
- Clone [this repo](https://github.com/GetStream/stream-chat-android)
- Checkout branch `report-detekt-issue`
- Run `./gradlew detekt`

The tasks will fail, reporting unused imports, but it is already being used

## Your Environment
* Version of detekt used: io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.19.0
* Version of Gradle used (if applicable): 7.4.2
* Gradle scan link (add `--scan` option when running the gradle task): https://scans.gradle.com/s/t2ryncburuyyg
* Link to your project (if it's a public repository): https://github.com/getstream/stream-chat-android

Pull request description:
Handle all kdoc sections in UnusedImports, not just the first one. The KDoc method getDefaultSection only gets the first section of the KDoc (text before an empty line). This fix now checks all sections/paragraphs of a KDoc instead.

Fixes #4815

Patch:
diff --git a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImports.kt b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImports.kt
index e793942b1f2c..e5b1853974e7 100644
--- a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImports.kt
+++ b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImports.kt
@@ -113,15 +113,16 @@ class UnusedImports(config: Config) : Rule(config) {
         }
 
         override fun visitDeclaration(dcl: KtDeclaration) {
-            val kdoc = dcl.docComment?.getDefaultSection()
+            val kdoc = dcl.docComment?.getAllSections()
 
-            kdoc?.getChildrenOfType<KDocTag>()
-                ?.map { it.text }
-                ?.forEach { handleKDoc(it) }
+            kdoc?.forEach { kdocSection ->
+                kdocSection.getChildrenOfType<KDocTag>()
+                    .map { it.text }
+                    .forEach { handleKDoc(it) }
 
-            kdoc?.getContent()?.let {
-                handleKDoc(it)
+                handleKDoc(kdocSection.getContent())
             }
+
             super.visitDeclaration(dcl)
         }
 


Test patch:
diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt
index 81197fb3ecd2..d9425c640d53 100644
--- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt
+++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt
@@ -609,4 +609,29 @@ class UnusedImportsSpec(val env: KotlinCoreEnvironment) {
         """
         assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty()
     }
+
+    @Test
+    fun `does not report unused import for import used in kdoc - #4815`() {
+        val mainFile = """
+        import x.y.z.SomeClass
+
+        class MyView
+
+        /**
+         * Style for [MyView]
+         * Blablabla
+         * 
+         * @property someVal Someval for [SomeClass]
+         */
+         data class StyleClass(val someVal: String)
+        """
+
+        val additionalFile = """
+        package x.y.z
+
+        class SomeClass
+        """
+
+        assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty()
+    }
 }