pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.06k stars 504 forks source link

Imports only used in blocks which may not be formatted are reported as unused #2696

Closed paul-dingemans closed 1 day ago

paul-dingemans commented 2 weeks ago

Based on https://github.com/nbadal/ktlint-intellij-plugin/issues/535#issuecomment-2162188102

In ktlint 1.3.0 an import which is only used inside a block which is excluded from formatting, is reported as being an unused import. Removing the import results in code which can not be compiled.

import bar.Bar1
import bar.Bar2

fun foo123() {
    // @formatter:off
    // Assume that class Bar1 is defined in package. The import
    // for this class is reported as unused
    Bar1()
    // @formatter:on

    // Assume that class Bar1 is defined in package. The import
    //    // for this class is not reported as unused
    Bar2()
}