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

Add rule to add braces to multiline when-condition #2557

Closed
paul-dingemans opened this issue Feb 20, 2024 · 0 comments · Fixed by #2829
Closed

Add rule to add braces to multiline when-condition #2557

paul-dingemans opened this issue Feb 20, 2024 · 0 comments · Fixed by #2829
Milestone

Comments

@paul-dingemans
Copy link
Collaborator

Similar to the if-else-bracing rule, all branches in a when-statement should be wrapped in braces. Braces are helpful for following reasons:

  • Bodies of the when-conditions are all aligned at same column position
  • Closing braces helps in separation the when-conditions

Invalid:

fun foo(bar: Bar) =
    when (bar) {
        ABC_ABC_ABC_ABC -> "Lorem ipsum 1"
        DE ->
            "Lorem ipsum 2"
        FGHIJ -> {
            """
            Lorem ipsum 3
            """.trimIndent()
        }
        KLMNOPQ -> "Lorem ipsum 4"
        else -> null
    }

Valid:

fun foo(bar: Bar) =
    when (bar) {
        ABC_ABC_ABC_ABC -> {
            "Lorem ipsum 1"
        }
        DE -> {
            "Lorem ipsum 2"
        }
        FGHIJ -> {
            """
            Lorem ipsum 3
            """.trimIndent()
        }
        KLMNOPQ -> {
            "Lorem ipsum 4"
        }
        else -> {
            null
        }
    }

A when-statement for which all branches are single lines should not be affected.

Valid:

fun foo(bar: Bar) =
    when (bar) {
        ABC_ABC_ABC_ABC -> "Lorem ipsum 1"
        DE -> "Lorem ipsum 2"
        FGHIJ -> """Lorem ipsum 3""".trimIndent()
        KLMNOPQ -> "Lorem ipsum 4"
        else -> null
    }
@paul-dingemans paul-dingemans added this to the 1.3.0 milestone Mar 3, 2024
paul-dingemans added a commit that referenced this issue Oct 6, 2024
Enforce consistent usage of braces in when entries. In case a when statement contains at least one when entry which uses braces around the body of the entry, then all when entries should use braces.

Braces are helpful for following reasons:
- Bodies of the when-conditions are all aligned at same column position
- Closing braces helps in separating the when-conditions

This rule is not incorporated in the Kotlin Coding conventions, nor in the Android Kotlin Styleguide. It is based on similar behavior in enforcing consistent use of braces in if-else statements. As of that the rule is only enabled automatically for code style `ktlint_official`. It can be enabled explicitly for other code styles.

Closes #2557
paul-dingemans added a commit that referenced this issue Oct 14, 2024
Enforce consistent usage of braces in when entries. In case a when statement contains at least one when entry which uses braces around the body of the entry, then all when entries should use braces.

Braces are helpful for following reasons:
- Bodies of the when-conditions are all aligned at same column position
- Closing braces helps in separating the when-conditions

This rule is not incorporated in the Kotlin Coding conventions, nor in the Android Kotlin Styleguide. It is based on similar behavior in enforcing consistent use of braces in if-else statements. As of that the rule is only enabled automatically for code style `ktlint_official`. It can be enabled explicitly for other code styles.

Closes #2557
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.

1 participant