AvoidNestedBlocks

Description

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

public void guessTheOutput()
{
    int whichIsWhich = 0;
    {
        int whichIsWhich = 2;
    }
    System.out.println("value = " + whichIsWhich);
}
        

and debugging / refactoring leftovers such as

// if (conditionThatIsNotUsedAnyLonger)
{
    System.out.println("unconditional");
}
        

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.

switch (a)
{
    case 0:
        // Never OK, break outside block
        {
            x = 1;
        }
        break;
    case 1:
        // Never OK, statement outside block
        System.out.println("Hello");
        {
            x = 2;
            break;
        }
    case 1:
        // OK if allowInSwitchCase is true
        {
            System.out.println("Hello");
            x = 2;
            break;
        }
}
        

Properties

name description type default value
allowInSwitchCase Allow nested blocks in case statements boolean false

Examples

To configure the check:

<module name="AvoidNestedBlocks"/>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker

EmptyBlock

Description

Checks for empty blocks.

Examples

To configure the check:

<module name="EmptyBlock"/>
        

To configure the check for the text policy and only try blocks:

<module name="EmptyBlock">
    <property name="option" value="text"/>
    <property name="tokens" value="LITERAL_TRY"/>
</module>
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker

EmptyCatchBlock

Description

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):

Properties

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 .*

Examples

To configure the Check to suppress empty catch block if exception's variable name is expected or ignore or there's any comment inside:

<module name="EmptyCatchBlock">
    <property name="exceptionVariableName" value="expected|ignore"/>
</module>
        

To configure the Check to suppress empty catch block if single-line comment inside is "//This is expected":

<module name="EmptyCatchBlock">
    <property name="commentFormat" value="This is expected"/>
</module>
        

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):

<module name="EmptyCatchBlock">
    <property name="commentFormat" value="This is expected"/>
    <property name="exceptionVariableName" value="myException"/>
</module>
        

Such empty blocks would be suppressed:

try {
    throw new RuntimeException();
} catch (RuntimeException e) {
    //This is expected
}
...
try {
    throw new RuntimeException();
} catch (RuntimeException e) {
    //   This is expected
}
...
try {
    throw new RuntimeException();
} catch (RuntimeException e) {
    // This is expected
    // some another comment
}
...
try {
    throw new RuntimeException();
} catch (RuntimeException e) {
    /* This is expected */
}
...
try {
    throw new RuntimeException();
} catch (RuntimeException e) {
    /*
    *
    * This is expected
    * some another comment
    */
}
...
try {
    throw new RuntimeException();
} catch (RuntimeException myException) {

}
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker

LeftCurly

Description

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.

Properties

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 when left curly brace policy is EOL boolean true
maxLineLength maximum number of characters in a line. ATTENTION: The option has been marked as deprecated since checkstyle 6.10 release. integer 80
tokens tokens to check subset of tokens INTERFACE_DEF, CLASS_DEF, ANNOTATION_DEF, ENUM_DEF, CTOR_DEF, METHOD_DEF, ENUM_CONSTANT_DEF, LITERAL_WHILE, LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_SYNCHRONIZED, LITERAL_SWITCH, LITERAL_DO, LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, STATIC_INIT, OBJBLOCK, LAMBDA. INTERFACE_DEF, CLASS_DEF, ANNOTATION_DEF, ENUM_DEF, CTOR_DEF, METHOD_DEF, ENUM_CONSTANT_DEF, LITERAL_WHILE, LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_SYNCHRONIZED, LITERAL_SWITCH, LITERAL_DO, LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, STATIC_INIT, OBJBLOCK, LAMBDA.

Examples

To configure the check:

<module name="LeftCurly"/>
        

To configure the check to apply the nl policy to type blocks:

<module name="LeftCurly">
    <property name="option" value="nl"/>
    <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
</module>
        

An example of how to configure the check to validate enum definitions:

<module name="LeftCurly">
    <property name="ignoreEnums" value="false"/>
</module>
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker

NeedBraces

Description

Checks for braces around code blocks.

Properties

name description type default value
allowSingleLineStatement allows single-line statements without braces boolean false
allowEmptyLoopBody allows loops with empty bodies boolean false
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.

Examples

To configure the check:

<module name="NeedBraces"/>
        

To configure the check for if and else blocks:

<module name="NeedBraces">
    <property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/>
</module>
        

To configure the check to allow single-line statements (if, while, do-while, for) without braces:

<module name="NeedBraces">
    <property name="allowSingleLineStatement" value="true"/>
</module>
        

Next statements won't be violated by Check:

if (obj.isValid()) return true; // 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:

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"/>
</module>
          

Next statements won't be violated by Check:

while (value.incrementValue() < 5); // OK
for(int i = 0; i < 10; value.incrementValue()); // OK
          

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker

RightCurly

Description

Checks the placement of right curly braces ('}') for if-else, try-catch-finally blocks, while-loops, for-loops, method definitions, class definitions, constructor definitions, instance and static initialization blocks. The policy to verify is specified using the property option.

Properties

name description type default value
option policy on placement of a right curly brace ('}') right curly brace policy same
shouldStartLine should we check if '}' starts line. boolean true
tokens tokens 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.

Examples

To configure the check:

<module name="RightCurly"/>
        

To configure the check with policy alone for else and METHOD_DEF tokens:

<module name="RightCurly">
    <property name="option" value="alone"/>
    <property name="tokens" value="LITERAL_ELSE, METHOD_DEF"/>
</module>
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker