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() {}
}
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
ControlFlowStatementWithoutBraces
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Java, 243.23126 |
Thanks for your feedback!