Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive unused import when import is referencing extension function and is used only in kdoc #1282

Closed
matejdro opened this issue Nov 10, 2021 · 8 comments · Fixed by #1292

Comments

@matejdro
Copy link

matejdro commented Nov 10, 2021

Steps to Reproduce

Run ktlint on the following two files (each file needs to be in a directory corresponding to the package):

package some.pkg

fun String.returnSelf(): String {
    return this
}
package other.pkg

import some.pkg.returnSelf

/**
 * Do not forget that you can also return string via [String.returnSelf]
 */
fun test() {}

Expected Behavior

Ktlint should pass

Observed Behavior

Ktlint fails with FileB.kt:1:1 Unused import

Your Environment

  • Version of ktlint used: 0.43.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): JLLeitschuh/ktlint-gradle 10.2.0
  • Version of Gradle used (if applicable): 7.2
  • Operating System and version: 5.15.1-1-MANJARO #1 SMP PREEMPT Sat Nov 6 14:11:10 UTC 2021 x86_64 GNU/Linux
@paul-dingemans
Copy link
Collaborator

Can you provide some background of the use case for which you want to keep the import when it is only used for a reference in a KDoc? IntelliJ IDEA Ultimate has no problem to resolve the reference without the imports.

@matejdro
Copy link
Author

IntelliJ IDEA Ultimate has no problem to resolve the reference without the imports.

Hm, it does not resolve it for me without the import:

image

@paul-dingemans
Copy link
Collaborator

Can you check the version of the kotlin plugin in IntelliJ? I am on version 212-1.5.31-release-546-IJ4638.7. On Jetbrains issuetracker you can mind quite some issues regarding kdoc links (https://youtrack.jetbrains.com/issues?q=kdoc%20link). This issue in particular seems to match with yours: https://youtrack.jetbrains.com/issue/KTIJ-12345

@matejdro
Copy link
Author

matejdro commented Nov 18, 2021

It happens with the latest one 212-1.6.0-release-799-IJ547.46.

Are you sure your IntelliJ can resolve this kdoc if you remove an import? Can you take a screenshot? Because I don't even see how would that work (IntelliJ needs imports to resolve conflicts etc.).

Linked issue is exactly what points against your point (it was false positive that import can be removed, since IntelliJ needs imports for KDoc).

@paul-dingemans
Copy link
Collaborator

I think I do understand the miscommunication between us.

Is it correct that your files are in different directories? If files are both in the same directory then IntelliJ indeed has no problems to display the link inside the kdoc as files belong to same package. However, if the files are in different directories (e.g. not in the same package) then IntelliJ needs the import for displaying the link inside kdoc correctly.

@matejdro
Copy link
Author

Sorry, you are correct. I thought that files having the different package implied that they are in the different directories (I know kotlin allows different packages in the same directory, but this is generally not the practice).

Updated the bug report to make that clear.

@paul-dingemans
Copy link
Collaborator

See commit above. It should be fixed with that.

@matejdro
Copy link
Author

Thanks!

romtsn added a commit that referenced this issue Dec 13, 2021
Closes #1282

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants