Skip to content

Add checks for OpenJDK Style §3.6 - Braces #19670

@Adam-Gamil

Description

@Adam-Gamil

Parent issue: #19604

Description

Add Checkstyle checks for all rules under §3.6 - Braces of the OpenJDK Java Style Guidelines v6.
https://checkstyle.org/styleguides/openjdk-java-style-v6/openjdk-styleguide.html#s3.6-braces

Image

Rules covered

From §3.6 - Braces:

  • Opening braces should be put on the end of the current line rather than on a line by its own. - LeftCurly Check
  • There should be a new line in front of a closing brace unless the block is empty (empty block exception is covered by §3.6.1). - RightCurly Check (option="alone")
  • Braces are recommended even where the language makes them optional. If a block spans more than one line (including comments), if one block in an if/else has braces, or if it comes last in an enclosing block, it must have braces. - NeedBraces Check
  • The else, catch and the while keyword in do...while loops go on the same line as the closing brace of the preceding block. - RightCurly Check (with option set to same)

Checks to add to openjdk_checks.xml

TreeWalker-level:

<module name="NeedBraces"/>

<module name="LeftCurly"/>

<module name="RightCurly">
    <property name="id" value="RightCurlyAlone"/>
    <property name="option" value="alone"/>
    <property name="tokens"
        value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR,
               LITERAL_WHILE, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF"/>
</module>

<module name="RightCurly">
    <property name="id" value="RightCurlySame"/>
    <property name="option" value="same"/>
    <property name="tokens"
        value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY,
               LITERAL_IF, LITERAL_ELSE, LITERAL_DO"/>
</module>
Check Rule PR
LeftCurly Opening brace placement
RightCurly (alone) Closing brace on new line
NeedBraces Enforce braces for optional blocks
RightCurly (same) else/catch/while on same line as closing brace

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions