-
Notifications
You must be signed in to change notification settings - Fork 507
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
ktlint incorrectly removes a used import #2821
Comments
Lol. Relocating your method to another package, would have worked as well. None of imports below will be removed:
The |
… to the package name In rare cases this could lead to code which no longer can be compiled Closes #2821
Thanks, Paul! |
Summary
ktlintFormat thinks a used import is unused, it removes the import, and my build no longer works.
The weird situation is that it's an import from the same package, which normally is unnecessary. However, it overloads (in a more specific way) another method that is used in the class in question, so it must be imported for the Kotlin compiler to choose the most specific overload.
Expected Behavior
ktlintFormat should not remove used imports and generally should not change behavior of the codebase
Observed Behavior
The import is removed. In my case, tests now fail, because methods are resolved differently.
Steps to Reproduce
The motivation here was trying to make unit testing with
kotlinx.coroutines.flow.Flow
cleaner.TestUtils.kt:
MyTest.kt
After ktlint removes the not-actually-unused import, the first assertEquals then resolves to the normal assertEquals rather than my helper method. This means that the
String
is directly compared to theFlow<String>
, which of course fails.Your Environment
.editorconfig
settings:ktlint_code_style = ktlint_official
This is not blocking me
In my case, I decided that this kind of complication was a sign that I was trying to be too clever, and I renamed my helper method. However, it does appear to be a bug, so I figured I should report it.
The text was updated successfully, but these errors were encountered: