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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With