Checks for empty blocks.
| name | description | type | default value |
|---|---|---|---|
| option | policy on block contents | block policy | stmt |
| tokens | blocks to check | subset of tokens LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_IF, LITERAL_FOR, LITERAL_TRY, LITERAL_WHILE, INSTANCE_INIT, STATIC_INIT, LITERAL_SWITCH. | all tokens |
To configure the check:
To configure the check for the text
policy and only try blocks:
com.puppycrawl.tools.checkstyle.checks.blocks
Checks for empty catch blocks. There are two options to make validation more precise (by default Check allows empty catch block with any comment inside):
| name | description | type | default value |
|---|---|---|---|
| exceptionVariableName | The name of variable associated with exception | String | ^$ |
| commentFormat | The format of the first comment inside empty catch | String | .* |
To configure the Check to suppress empty catch block if exception's variable name is
expected or ignore or there's any comment inside:
To configure the Check to suppress empty catch block if single-line comment inside is "//This is expected":
To configure the Check to suppress empty catch block if single-line comment inside is "//This is expected" or exception's variable name is "myException" (any option is matching):
Such empty blocks would be suppressed:
com.puppycrawl.tools.checkstyle.checks.blocks
Checks for the placement of left curly braces
('{') for code blocks. The policy to verify is
specified using the property option. Policies
eol and nlow take into account
the property maxLineLength.
| name | description | type | default value |
|---|---|---|---|
| option | policy on placement of a left curly brace ('{') |
left curly brace policy | eol |
| ignoreEnums | If true, Check will ignore enums | boolean | true |
| maxLineLength | maximum number of characters in a line | integer | 80 |
| tokens | blocks to check | subset of tokens ANNOTATION_DEF, CLASS_DEF, CTOR_DEF, ENUM_DEF, ENUM_CONSTANT_DEF, INTERFACE_DEF, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, METHOD_DEF | all tokens |
To configure the check:
To configure the check to apply the nl policy to
type blocks:
An example of how to configure the check to validate enum definitions:
com.puppycrawl.tools.checkstyle.checks.blocks
Checks for braces around code blocks.
| name | description | type | default value |
|---|---|---|---|
| tokens | blocks to check | subset of tokens LITERAL_DO, LITERAL_ELSE, LITERAL_IF, LITERAL_FOR, LITERAL_WHILE | all tokens |
| allowSingleLineStatement | allows single-line statements without braces | boolean | false |
To configure the check:
To configure the check for if and
else blocks:
To configure the check to allow single-line statements
(if, while, do-while, for) without braces:
Next statements won't be violated by Check:
To configure the Check to allow case, default single-line statements
without braces:
Next statements won't be violated by Check:
com.puppycrawl.tools.checkstyle.checks.blocks
Checks the placement of right curly braces ('}')
for else, try, and
catch tokens. The policy to verify is specified
using the property option.
| name | description | type | default value |
|---|---|---|---|
| option | policy on placement of a right curly brace ('}') |
right curly brace policy | same |
| tokens | blocks to check | subset of tokens LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT. | LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE |
| shouldStartLine | should we check if '}'
starts line. |
boolean | true |
To configure the check:
To configure the check with policy alone for else and METHOD_DEF
tokens:
com.puppycrawl.tools.checkstyle.checks.blocks
Finds nested blocks, i.e. blocks that are used freely in the code.
Rationale: Nested blocks are often leftovers from the debugging process, they confuse the reader.
For example this Check finds the obsolete braces in
and debugging / refactoring leftovers such as
A case in a switch statement does not implicitly form a block. Thus to be able to introduce local variables that have case scope it is necessary to open a nested block. This is supported, set the allowInSwitchCase property to true and include all statements of the case in the block.
| name | description | type | default value |
|---|---|---|---|
| allowInSwitchCase | Allow nested blocks in case statements | boolean | false |
To configure the check:
com.puppycrawl.tools.checkstyle.checks.blocks