# Block Checks

- About
- Checkstyle
- Documentation
- Release Notes
- Configuration
- Property Types
- Running
- Ant Task
- Command Line
- Available Checks
- Standard Checks
- Javadoc Comments
- Naming Conventions
- Headers
- Imports
- Size Violations
- Whitespace
- Modifiers
- Block Checks
- EmptyBlock
- LeftCurly
- NeedBraces
- RightCurly
- AvoidNestedBlocks
- Coding
- Class Design
- Duplicate Code
- Metrics
- Miscellaneous
- Optional Checks
- J2EE Checks
- Extending Checkstyle
- Writing checks
- Writing filters
- Writing listeners
- Javadoc
- SourceForge
- Mailing Lists
- Bug Database
- Feature Requests
- Developers
- Project Page
- CVS Repository
- Contributing

- Hosted by:

## EmptyBlock

### Description

Checks for empty blocks.

### Properties

name | description | type | default value
--- | --- | --- | ---
option | policy on block contents | block policy | stmt
tokens | blocks to check | subset of tokens LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_IF, LITERAL_FOR, LITERAL_TRY, LITERAL_WHILE, INSTANCE_INIT STATIC_INIT | all tokens

### Examples

To configure the check:

    <module name="EmptyBlock"/>

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

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

### 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 property option. Policies eol and nlow take into account property maxLineLength.

### Properties

name | description | type | default value
--- | --- | --- | ---
option | policy on placement of a left curly brace ('{') | left curly brace policy | eol
maxLineLength | maximum number of characters in a line | integer | 80
tokens | blocks to check | subset of tokens CLASS_DEF, CTOR_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

### 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>

### Package

com.puppycrawl.tools.checkstyle.checks.blocks

### Parent Module

TreeWalker

## NeedBraces

### Description

Checks for braces around code blocks.

### Properties

name | description | type | default value
--- | --- | --- | ---
tokens | blocks to check | subset of tokens LITERAL_DO, LITERAL_ELSE, LITERAL_IF, LITERAL_FOR, LITERAL_WHILE | all tokens

### 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>

### Package

com.puppycrawl.tools.checkstyle.checks.blocks

### Parent Module

TreeWalker

## RightCurly

### Description

Checks the placement of right curly braces ('}') for else, try, and catch tokens. The policy to verify is specified using property option.

### Properties

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 | LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE
shouldStartLine | should we check if '}' starts line. | boolean | true

### Examples

To configure the check:

    <module name="RightCurly"/>

To configure the check with policy alone for else tokens:

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

### Package

com.puppycrawl.tools.checkstyle.checks.blocks

### Parent Module

TreeWalker

## 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 whichIsWich = 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"/>

### Package

com.puppycrawl.tools.checkstyle.checks.blocks

### Parent Module

TreeWalker

Copyright © 2001-2005, Oliver Burn