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

Multiline if-else rule could also consider for and while-loop #2223

Closed
hanggrian opened this issue Sep 2, 2023 · 2 comments
Closed

Multiline if-else rule could also consider for and while-loop #2223

hanggrian opened this issue Sep 2, 2023 · 2 comments

Comments

@hanggrian
Copy link
Contributor

hanggrian commented Sep 2, 2023

Forcing braces in multiline if-else improves readability, so why not extend it to all control flow statements: for, while, and if?

fun hasDigit(string: String): Boolean {
    for (character in string)
        if (character.isDigit())
            return true
    return false
}

Expected Behavior

Violations:

  • for (character in string)
  • if (character.isDigit())

Current Behavior

Violations:

  • if (character.isDigit())

Additional information

  • Current version of ktlint: 0.49.1
@paul-dingemans
Copy link
Collaborator

Kotlin Coding coventions do not mention braces for multiline while, do-while and for loops.

Android Kotlin style guide is a bit more explicit:

Braces are otherwise required for any if, for, when branch, do, and while statements and expressions, even when the body is empty or contains only a single statement.

I do think this behavior fits well for ktlint_official code style, and for android_studio code style because it is mentioned explicitly.

This behavior is to be added to new experimental rule control-flow-braces. Also note that the block of a multiline if-statement without else should be wrapped between braces.

@paul-dingemans
Copy link
Collaborator

Resolved by #2298

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

No branches or pull requests

2 participants