Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Need to wrap when statements that use an assignment if/else with a lambda #901

Closed
ZakTaccardi opened this issue Sep 10, 2020 · 2 comments · Fixed by #916
Closed

Need to wrap when statements that use an assignment if/else with a lambda #901

ZakTaccardi opened this issue Sep 10, 2020 · 2 comments · Fixed by #916
Labels
Milestone

Comments

@ZakTaccardi
Copy link

Expected Behavior

I expect the following to pass formatting.

fun broken(letter: Letter): String? = when (letter) {
    Letter.A -> if (hasPrivilegedStatePermission) {
        "subscriberId"
    } else {
        throw SecurityException(
            "does not have privileged phone permission"
        )
    }
    Letter.B -> if (hasPrivilegedStatePermission) {
        "subscriberId"
    } else {
        null
    }
}

Observed Behavior

Fails with: Missing newline after "->" (indent). Wrapping with { .. } fixes the issue

Steps to Reproduce

enum class Letter(val value: String) {
    A("a"),
    B("b");
}
val hasPrivilegedStatePermission = true
fun broken(letter: Letter): String? = when (letter) {
    Letter.A -> if (hasPrivilegedStatePermission) {
        "subscriberId"
    } else {
        throw SecurityException(
            "does not have privileged phone permission"
        )
    }
    Letter.B -> if (hasPrivilegedStatePermission) {
        "subscriberId"
    } else {
        null
    }
}
fun fixed(letter: Letter): String? = when (letter) {
    Letter.A -> {
        if (hasPrivilegedStatePermission) {
            "subscriberId"
        } else {
            throw SecurityException(
                "does not have privileged phone permission"
            )
        }
    }
    Letter.B -> {
        if (hasPrivilegedStatePermission) {
            "subscriberId"
        } else {
            null
        }
    }
}

Your Environment

  • Version of ktlint used: 0.38.1 (command line)
  • AS 4.0.1 with AGP 3.6
  • Operating System and version: MacOS 10.15.6
@ZakTaccardi ZakTaccardi changed the title Need to wrap when statements that use an if/else with a lambda Need to wrap when statements that use an assignment if/else with a lambda Sep 10, 2020
@ZakTaccardi
Copy link
Author

This could likely be the same issue and have the same resolution as #898 (but unable to test 39.0 yet)

This is the last issue I found when upgrading to 0.38.1, so no more from me :)

Thanks!

@Tapchicoma Tapchicoma added the bug label Sep 10, 2020
@Tapchicoma Tapchicoma added this to the 0.40.0 milestone Sep 10, 2020
@Tapchicoma
Copy link
Collaborator

Yes, it fails on master as well.

BTW thank you for detailed bugs description 👍

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

Successfully merging a pull request may close this issue.

2 participants