Skip to content

--format with blank-line-between-when-conditions enabled (1.8.0 default) moves trailing comments to a misleading place #3209

@glasser

Description

@glasser

Expected Behavior

With blank-line-between-when-conditions enabled (default behavior in ktlint 1.8.0) it should probably be OK to have a comment at the end of a when-condition line. But if that's not OK, --format should not move the comment to a place that is much more associated with the next condition than the current one.

So either this should be OK:

fun foo(x: Int) {
    when (x) {
        5 -> "five" // five comment
        6 -> "six" // six comment
        else -> "other"
    }
}

Or it should be corrected to something along the lines of this (if reordering non-whitespace is allowed):

fun foo(x: Int) {
    when (x) {
        // five comment
        5 -> "five"

        // six comment
        6 -> "six"

        else -> "other"
    }
}

Observed Behavior

ktlint --format rewrites to this:

fun foo(x: Int) {
    when (x) {
        5 -> "five"

        // five comment
        6 -> "six"

        // six comment
        else -> "other"
    }

This is pretty misleading: the comment that used to be on the "five" line is now fully separated from it by a blank line!

Steps to Reproduce

in /tmp/ktlint-demo 󰦜 at 14:46:23-08 ❌1 ❯ cat ./Lint.kt
fun foo(x: Int) {
    when (x) {
        5 -> "five" // five comment
        6 -> "six" // six comment
        else -> "other"
    }
}

in /tmp/ktlint-demo 󰦜 at 14:46:32-08 ❯ mise x [email protected] -- ktlint Lint.kt 
/private/tmp/ktlint-demo/Lint.kt:3:21: Add a blank line between all when-conditions in case at least one multiline when-condition is found in the statement (standard:blank-line-between-when-conditions)
/private/tmp/ktlint-demo/Lint.kt:4:20: Add a blank line between all when-conditions in case at least one multiline when-condition is found in the statement (standard:blank-line-between-when-conditions)
14:46:34.813 [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Lint has found errors than can be autocorrected using 'ktlint --format'

Summary error count (descending) by rule:
  standard:blank-line-between-when-conditions: 2

in /tmp/ktlint-demo 󰦜 at 14:46:34-08 ❌1 ❯ mise x [email protected] -- ktlint Lint.kt --format

in /tmp/ktlint-demo 󰦜 at 14:46:38-08 ❯ cat ./Lint.kt                                 
fun foo(x: Int) {
    when (x) {
        5 -> "five"

        // five comment
        6 -> "six"

        // six comment
        else -> "other"
    }
}

Your Environment

  • Version of ktlint used: 1.8.0
  • Relevant parts of the .editorconfig settings: none
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): CLI
  • Version of Gradle used (if applicable): n/a
  • Operating System and version: Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions