kotlinter-gradle icon indicating copy to clipboard operation
kotlinter-gradle copied to clipboard

ktlint-disable/enable blocks can confuse the linter after updating to 3.12.0

Open mw-sebastianzander opened this issue 2 years ago • 1 comments

Upgrading from version 3.11.1 to 3.12.0 I've noticed some problems with how ktlint-disabled is handled.

  1. 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.

  1. 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.

mw-sebastianzander avatar Nov 24 '22 09:11 mw-sebastianzander

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?

jeremymailen avatar Dec 01 '22 08:12 jeremymailen

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.

jeremymailen avatar Feb 18 '23 17:02 jeremymailen