Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ class UnusedImport(config: Config) : Rule(config) {
private fun handleKDoc(content: String) {
kotlinDocReferencesRegExp.findAll(content, 0)
.map { it.groupValues[1] }
.forEach { namedReferencesInKDoc.add(it.split(".")[0]) }
.forEach {
val referenceNames = it.split(".")
namedReferencesInKDoc.add(referenceNames[0])
namedReferencesInKDoc.add(referenceNames.last())
}
kotlinDocBlockTagReferenceRegExp.find(content)?.let {
val str = it.groupValues[2].split(whiteSpaceRegex)[0]
namedReferencesInKDoc.add(str.split(".")[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,94 @@ class UnusedImportSpec(val env: KotlinCoreEnvironment) {
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}

@Test
fun `does not report KDoc references with companion method calls`() {
val main = """
package com.example

import android.text.TextWatcher.beforeTextChanged

class Test {
/**
* [beforeTextChanged]
*/
fun test() {
TODO()
}
}
""".trimIndent()
val additional = """
package android.text

object TextWatcher {
fun beforeTextChanged() {}
}
""".trimIndent()
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}

@Test
fun `does not report KDoc references with extension method calls`() {
val main = """
package com.example

import android.text.TextWatcher
import android.text.beforeTextChanged
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the false negative that you were talking about, right?

Copy link
Contributor Author

@atulgpt atulgpt Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BraisGabin yes it is a false negative. There are two currently I can think of

@Test
fun `false -ve #1`() {
    val main = """
        package com.example

        import android.text.TextWatcher
        import android.text.beforeTextChanged

        class TestClass {
            /**
             * Below we are referring instance method no extension method
             * [TextWatcher.beforeTextChanged]
             */
            fun test() {
                TODO()
            }
        }
    """.trimIndent()
    val additional1 = """
        package android.text

        class TextWatcher {
            fun beforeTextChanged() {}
        }
    """.trimIndent()
    val additional2 = """
        package android.text

        fun TextWatcher.beforeTextChanged() {}
    """.trimIndent()
    assertThat(subject.lintWithContext(env, main, additional1, additional2)).isEmpty()
}

@Test
fun `false -ve #2`() {
    val main = """
        package com.example

        import android.text.TextWatcher
        import android.text.beforeTextChanged

        import test.TextWatcher
        import test.beforeTextChanged
        
        class TestClass {
            /**
             * Below we are referring non existing method extension method receiver is different
             * [TextWatcher.beforeTextChanged]
             */
            fun test() {
                TODO()
            }
        }
    """.trimIndent()
    val additional1 = """
        package android.text

        class TextWatcher
        class TextWatcher2
    """.trimIndent()
    val additional2 = """
        package android.text

        fun TextWatcher2.beforeTextChanged() {}
    """.trimIndent()
    assertThat(subject.lintWithContext(env, main, additional1, additional2)).isEmpty()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I'll remove the import. We don't want a test that enforces a false positive. We could have an ignored test with the false positive. But, probably, it's better to just remove this import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I have updated the TC to make the TC a valid cass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this case valid shouldn't we remove this line?

Suggested change
import android.text.beforeTextChanged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it needs to be imported otherwise you will get the squiggly line in the Kdoc. See class TextWatcher doesn't have this method defined there


class TestClass {
/**
* [TextWatcher.beforeTextChanged]
*/
fun test() {
TODO()
}
}
""".trimIndent()
val additional1 = """
package android.text

class TextWatcher
""".trimIndent()
val additional2 = """
package android.text

fun TextWatcher.beforeTextChanged() {}
""".trimIndent()
assertThat(subject.lintWithContext(env, main, additional1, additional2)).isEmpty()
}

@Test
fun `does report imported extension method which is not used`() {
val main = """
package com.example

import android.text.TextWatcher
import android.text.beforeTextChanged
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Suggested change
import android.text.beforeTextChanged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

import android.text.afterTextChanged

class TestClass {
/**
* [TextWatcher.beforeTextChanged]
*/
fun test() {
TODO()
}
}
""".trimIndent()
val additional1 = """
package android.text

class TextWatcher
""".trimIndent()
val additional2 = """
package android.text

fun TextWatcher.beforeTextChanged() {}
fun TextWatcher.afterTextChanged() {}
""".trimIndent()
assertThat(subject.lintWithContext(env, main, additional1, additional2)).hasSize(1)
}

@Test
fun `reports imports with different cases`() {
val main = """
Expand Down