Skip to content

Add braces to multiline if-statements #1463

@987Nabil

Description

@987Nabil

I suggest to have an option, to rewrite multiline if-statements to always use curly braces.
Single line should be left untouched in general or optionally formatted.

I used that style before, as it is supported by IntelliJ and I am missing this in scalafmt.
A good discussion, if anybody really needs to get convinced, can be found on stackoverflow.
There is even a real world example of an apple bug from 2014.

And scalastyle supports this too.
They gave the option to make an exception for double lines.
I think this is already to dangerous and should force braces too.

I think, if you turn this option on in IntelliJ, the editor automatically inserts braces if you enter a newline after the if conditions parentheses are closed.
Is it possible to do the same if the scalafmt formatter is selected in IntelliJ?

Example

So this code

if(shouldDoThis)
  doThis()
else
  doThat()

becomes

if(shouldDoThis){
  doThis()
} else {
  doThat()
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions