Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single line braces in checkstyle gives error

I want Checkstyle in Java to not give me any errors for the following line of code -

if (true) { return 1; }

But it gives me errors, '}' should have line break before. and '{' should have line break after. My LeftCurly block is as follows:

 <module name="LeftCurly">
   <!-- Checks for placement of the left curly brace ('{'). -->
   <property name="severity" value="warning"/>
</module>

My RightCurly is as follows:

<module name="RightCurly">
  <property name="option" value="same"/>
  <property name="severity" value="warning"/>
</module>

My NeedBraces is as follows:

<module name="NeedBraces">
    <property name="severity" value="warning"/>
    <property name="tokens" value="LITERAL_FOR, LITERAL_WHILE, LITERAL_DO,    LITERAL_IF, LITERAL_ELSE"/>
</module>

How do I allow single line blocks to have opening and closing braces on the same line? Thanks!

I'm using the Gradle Checkstyle plugin, Checkstyle version: 6.7

like image 334
Manmeet Saluja Avatar asked Nov 09 '22 06:11

Manmeet Saluja


1 Answers

Your right curly option "same" means something different:

The brace should be on the same line as the next part of a multi-block statement

So for your case you should use the option alone_or_singleline:

<property name="option" value="alone_or_singleline"/>

See rcurly for more.

Also it is always a good idea to think about a consistent code style which avoids having exceptions! So think about another developer that will insert code some month later into your block - would be easier when the block is not on the same line.

like image 88
PowerStat Avatar answered Nov 14 '22 23:11

PowerStat