Since Checkstyle 3.0
Checks for empty blocks. This check does not validate sequential blocks.
Sequential blocks won't be checked. Also, no violations for fallthrough:
switch (a) { case 1: // no violation case 2: // no violation case 3: someMethod(); { } // no violation default: break; }
NOTE: This check processes LITERAL_CASE and LITERAL_DEFAULT separately. Verification empty block is done for single nearest {@code case} or {@code default}.
name | description | type | default value | since |
---|---|---|---|---|
option | Specify the policy on block contents. | BlockOption | statement |
3.0 |
tokens | tokens to check | subset of tokens LITERAL_WHILE , LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_DO , LITERAL_IF , LITERAL_ELSE , LITERAL_FOR , INSTANCE_INIT , STATIC_INIT , LITERAL_SWITCH , LITERAL_SYNCHRONIZED , LITERAL_CASE , LITERAL_DEFAULT , ARRAY_INIT . | LITERAL_WHILE , LITERAL_TRY , LITERAL_FINALLY , LITERAL_DO , LITERAL_IF , LITERAL_ELSE , LITERAL_FOR , INSTANCE_INIT , STATIC_INIT , LITERAL_SWITCH , LITERAL_SYNCHRONIZED . | 3.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="EmptyBlock"/> </module> </module>
Example:
public class Example1 { private void emptyLoop() { for (int i = 0; i < 10; i++) { // violation 'Must have at least one statement' } try { // violation 'Must have at least one statement' } catch (Exception e) { // ignored } } }
To configure the check for the text
policy and only try
blocks:
<module name="Checker"> <module name="TreeWalker"> <module name="EmptyBlock"> <property name="option" value="text"/> <property name="tokens" value="LITERAL_TRY"/> </module> </module> </module>
Example:
public class Example2 { private void emptyLoop() { for (int i = 0; i < 10; i++) { // ignored } try { } // violation above 'Empty try block' catch (Exception e) { // ignored } } }
To configure the check for default in switch block:
<module name="Checker"> <module name="TreeWalker"> <module name="EmptyBlock"> <property name="tokens" value="LITERAL_DEFAULT"/> </module> </module> </module>
Example:
public class Example3 { private void test(int a) { switch (a) { case 1: someMethod(); default: // OK, as there is no block } switch (a) { case 1: someMethod(); default: {} // violation 'Must have at least one statement' } } }
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