Inspectopedia Help

Control flow statement without braces

Reports any if, while, do, or for statements without braces. Some code styles, e.g. the Google Java Style guide, require braces for all control statements.

When adding further statements to control statements without braces, it is important not to forget adding braces. When commenting out a line of code, it is also necessary to be more careful when not using braces, to not inadvertently make the next statement part of the control flow statement. Always using braces makes inserting or commenting out a line of code safer.

It's likely the goto fail vulnerability would not have happened, if an always use braces code style was used. Control statements with braces make the control flow easier to see, without relying on, possibly incorrect, indentation.

Example:

class Strange { void x(boolean one, boolean two) { if(one) if(two) foo(); else bar(); } void foo() {} void bar() {} }

The quick-fix wraps the statement body with braces:

class Strange { void x(boolean one, boolean two) { if(one) { if(two) { foo(); } else { bar(); } } } void foo() {} void bar() {} }

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023