google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

One Line if statements - Don't add extra line

Open BhavyaaArora-08 opened this issue 2 years ago • 2 comments

Desired :

if(something) doSomething()

Formatter updates it to :

if(something) 
       doSomething()

BhavyaaArora-08 avatar May 04 '23 20:05 BhavyaaArora-08

Neither of these are actually compliant with Google Java Style, which is what google-java-format generates, but the second one is closer (within g-j-f's constraint of only modifying whitespace).

As per https://google.github.io/styleguide/javaguide.html#s4.1-braces and https://google.github.io/styleguide/javaguide.html#s4.3-one-statement-per-line, the required format to adhere to Google Java Style is

if (something) {
  doSomething();
}

lowasser avatar May 04 '23 20:05 lowasser

but the second one is closer (within g-j-f's constraint of only modifying whitespace)

I agree that the second one is closer. However, it seems to me that the first one is what actually happens, at least with versions 1.13.0 and 1.18.1 under Eclipse - the formatter removes the linebreak and shoves it all into a single line.

(I also agree that brackets are what is "correct" according to the Google Java Style, but as the formatter needs to do something with the statement it should probably do the closer variant)

MrThaler avatar Oct 18 '23 14:10 MrThaler