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

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.16690

Last modified: 29 April 2024