Skip to content
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

Wrong format result with when guards #2817

Closed
revonateB0T opened this issue Sep 30, 2024 · 2 comments · Fixed by #2825
Closed

Wrong format result with when guards #2817

revonateB0T opened this issue Sep 30, 2024 · 2 comments · Fixed by #2825
Milestone

Comments

@revonateB0T
Copy link

Original

when (true) {
    true if when (true) {
        true -> true
        false -> false
    } -> true
    else -> false
}

After formatting

when (true) {
    true if when (true) {
        true -> true
        false -> false
    },
    -> true
    else -> false
}

Which is illegal

@paul-dingemans paul-dingemans added this to the 1.4.0 milestone Oct 2, 2024
@paul-dingemans
Copy link
Collaborator

For now you can work around this as follows:

@Suppress("ktlint:standard:trailing-comma-on-declaration-site")
when (true) {
    true if when (true) {
        true -> true
        false -> false
    } -> true
    else -> false
}

@paul-dingemans
Copy link
Collaborator

The problem seems to occur only when the guard clause is a multiline expression.

Code below reproduces the error:

val x2 =
    when (true) {
        true if foo(
            "a",
        )
        -> true

        else -> false
    }

while code below does not reproduce:

val x3 =
    when (true) {
        true if foo("a") -> true
        else -> false
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants