Since Checkstyle 3.0
Checks for braces around code blocks.
name | description | type | default value | since |
---|---|---|---|---|
allowSingleLineStatement | allow single-line statements without braces. | boolean | false |
6.5 |
allowEmptyLoopBody | allow loops with empty bodies. | boolean | false |
6.12.1 |
tokens | tokens to check | subset of tokens LITERAL_DO , LITERAL_ELSE , LITERAL_FOR , LITERAL_IF , LITERAL_WHILE , LITERAL_CASE , LITERAL_DEFAULT , LAMBDA . | LITERAL_DO , LITERAL_ELSE , LITERAL_FOR , LITERAL_IF , LITERAL_WHILE . | 3.0 |
To configure the check:
<module name="NeedBraces"/>
Example:
if (obj.isValid()) return true; // violation, single-line statements not allowed without braces if (true) { // OK return true; } else // violation, single-line statements not allowed without braces return false; for (int i = 0; i < 5; i++) { // OK ++count; } do // violation, single-line statements not allowed without braces ++count; while (false); for (int j = 0; j < 10; j++); // violation, empty loop body not allowed for(int i = 0; i < 10; value.incrementValue()); // violation, empty loop body not allowed while (counter < 10) // violation, single-line statements not allowed without braces ++count; while (value.incrementValue() < 5); // violation, empty loop body not allowed switch (num) { case 1: counter++; break; // OK }
To configure the check for if
and
else
blocks:
<module name="NeedBraces"> <property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/> </module>
Example:
if (obj.isValid()) return true; // violation, single-line statements not allowed without braces if (true) { // OK return true; } else // violation, single-line statements not allowed without braces return false; for (int i = 0; i < 5; i++) { // OK ++count; } do // OK ++count; while (false); for (int j = 0; j < 10; j++); // OK for(int i = 0; i < 10; value.incrementValue()); // OK while (counter < 10) // OK ++count; while (value.incrementValue() < 5); // OK switch (num) { case 1: counter++; break; // OK }
To configure the check to allow single-line statements
(if, while, do-while, for
) without braces:
<module name="NeedBraces"> <property name="allowSingleLineStatement" value="true"/> <property name="tokens" value="LITERAL_IF, LITERAL_WHILE, LITERAL_DO, LITERAL_FOR"/> </module>
Example:
if (obj.isValid()) return true; // OK if (true) { // OK return true; } else // OK return false; for (int i = 0; i < 5; i++) { // OK ++count; } do // OK ++count; while (false); for (int j = 0; j < 10; j++); // violation, empty loop body not allowed for(int i = 0; i < 10; value.incrementValue()); // violation, empty loop body not allowed while (counter < 10) // OK ++count; while (value.incrementValue() < 5); // violation, empty loop body not allowed switch (num) { case 1: counter++; break; // OK } while (obj.isValid()) return true; // OK do this.notify(); while (o != null); // OK for (int i = 0; ; ) this.notify(); // OK
To configure the check to allow case, default
single-line statements
without braces:
<module name="NeedBraces"> <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/> <property name="allowSingleLineStatement" value="true"/> </module>
Next statements won't be violated by check:
if (obj.isValid()) return true; // OK if (true) { // OK return true; } else // OK return false; for (int i = 0; i < 5; i++) { // OK ++count; } do // OK ++count; while (false); for (int j = 0; j < 10; j++); // OK for(int i = 0; i < 10; value.incrementValue()); // OK while (counter < 10) // OK ++count; while (value.incrementValue() < 5); // OK switch (num) { case 1: counter++; break; // OK case 6: counter += 10; break; // OK default: counter = 100; break; // OK }
To configure the check to allow loops (while, for
) with empty bodies:
<module name="NeedBraces"> <property name="allowEmptyLoopBody" value="true"/> <property name="tokens" value="LITERAL_WHILE, LITERAL_FOR"/> </module>
Example:
if (obj.isValid()) return true; // OK if (true) { // OK return true; } else // OK return false; for (int i = 0; i < 5; i++) { // OK ++count; } do // OK ++count; while (false); for (int j = 0; j < 10; j++); // OK for(int i = 0; i < 10; value.incrementValue()); // OK while (counter < 10) // violation, single-line statements not allowed without braces ++count; while (value.incrementValue() < 5); // OK switch (num) { case 1: counter++; break; // OK }
To configure the check to lambdas:
<module name="NeedBraces"> <property name="tokens" value="LAMBDA"/> <property name="allowSingleLineStatement" value="true"/> </module>
Results in following:
allowedFuture.addCallback(result -> assertEquals("Invalid response", EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result), // violation, lambda spans 2 lines ex -> fail(ex.getMessage())); // OK allowedFuture.addCallback(result -> { return assertEquals("Invalid response", EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result); }, // OK ex -> fail(ex.getMessage()));
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.blocks