if branch bodies should be formatted unambiguously

  • AMBIGUOUS_BRANCH_BODY
  • Code Quality
  • Low
  • cwe

This rule applies when if branch bodies are formatted ambiguously.

Without braces ({}), if branch body can have only one statement. This rule detects the following cases where the next statement not belonging to the branch is ambiguously formatted:

  1. The next statement is on the same line as the branch body.
  2. The next statement is aligned with the branch body.

This rule also applies to for and while loop bodies.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
if (a)
    foo(); bar(); // AMBIGUOUS_BRANCH_BODY alarm

// Example 2
for (var i = 0; i < len; i++)
    foo();
    bar(); // AMBIGUOUS_BRANCH_BODY alarm

Compliant Code Example

View with noncompliant examples side by side
// Example 1
if (a) {
    foo();
}
bar();

// Example 2
for (var i = 0; i < len; i++) {
    foo();
}
bar();

Version

This rule was introduced in DeepScan 1.28.0.

See

Was this documentation helpful?