kotlinter-gradle
kotlinter-gradle copied to clipboard
ktlint-disable/enable blocks can confuse the linter after updating to 3.12.0
Upgrading from version 3.11.1 to 3.12.0 I've noticed some problems with how ktlint-disabled
is handled.
- Imports only used within a
ktlint-disabled
/ktlint-enabled
block are seen as unused.
package com.test
import java.time.LocalTime
class Application {
companion object {
@JvmStatic
fun main(args: Array<String>) {
/* ktlint-disable */
println(LocalTime.now())
/* ktlint-enable */
}
}
}
If I run formatKotlin
the import is removed.
-
ktlint-disable
/ktlint-enable
-block can confuse the indentation. In this example:
package com.test
class Application {
companion object {
fun data() = mapOf( /* ktlint-disable */
"foo" to 1,
"bar" to 2,
"baz" to 3
) /* ktlint-enable */
@JvmStatic
fun main(args: Array<String>) {
println(data())
}
}
}
running formatKotlin
will generate this:
package com.test
class Application {
companion object {
fun data() = mapOf( /* ktlint-disable */
"foo" to 1,
"bar" to 2,
"baz" to 3
) /* ktlint-enable */
@JvmStatic
fun main(args: Array<String>) {
println(data())
}
}
}
The function below the data()
function is indented weirdly.
This can be fixed by putting ktlint-disable
to the left of mapOf
:
fun data() = /* ktlint-disable */ mapOf(/
I don't know if this is an issue with kotlinter-gradle or the underlying ktlint project.
These behaviors are at the code level so I believe this is ktlint
functionality.
I assume if you run the ktlint
cli you get the same results?
Going ahead and closing this as this behavior is implemented by ktlint
itself. If there's a big over there we'll need to file a issue against ktlint.