For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/dart-code-quality/curly-braces-in-flow-control-structures.md. A documentation index is available at /llms.txt.

Use curly braces for all flow control structures

This product is not supported for your selected Datadog site. ().

Metadata

ID: dart-code-quality/curly-braces-in-flow-control-structures

Language: Dart

Severity: Notice

Category: Code Style

Description

Omitting curly braces around single-statement bodies in if, else, for, and while blocks is a common source of bugs. When a second statement is added later, it may appear to be inside the block but is not.

Always wrap control flow bodies in curly braces, even when there is only one statement, to prevent accidental logic errors.

Non-Compliant Code Examples

void main() {
  var x = 0;

  while (true)
    x++;

  for (var i = 0; i < 42; i++)
    x++;

  for (var item in [1, 2, 3])
    x += item;

  if (true)
    x++;
  else
    x--;

  if (x < 1)
    x = 1;
  else if (x > 10)
    x = 10;

  do
    x++;
  while (x < 10);
}

Compliant Code Examples

void main() {
  var x = 0;

  while (true) {
    x++;
  }

  for (var i = 0; i < 42; i++) {
    x++;
  }

  for (var item in [1, 2, 3]) {
    x += item;
  }

  if (true) {
    x++;
  } else {
    x--;
  }

  if (true) {
    x++;
  } else if(true) {
    x--;
  }

  do {
    x++;
  } while (x < 10);
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security