Skip to content

📝 Add style rule S.3.7: Leave an infinite loop's body empty - #103

Merged
kammce merged 1 commit into
mainfrom
style-empty-infinite-loop
Nov 14, 2026
Merged

kammce merged 1 commit into
mainfrom
style-empty-infinite-loop

Conversation

@kammce

@kammce kammce commented Nov 13, 2026

Copy link
Copy Markdown
Member

Adds style rule S.3.7 Leave an infinite loop's body empty, and fixes the two pages whose examples broke it.

Clang deletes while (true) { continue; } in every standard mode. The compiler may assume that a loop without side effects ends, and only an empty body with a constant condition is exempt (P2809). So a halt loop written that way compiles to nothing, and execution falls into whatever code follows it. GCC keeps the loop.

S.3.7

  • A loop with a constant condition that has nothing to do gets an empty body, with a comment inside the braces saying why. No continue;.
  • A loop whose condition reads volatile or atomic memory, such as polling a register or a spin lock, can keep continue;. The read is a side effect, so the loop stays. This matches the S.5.4 polling example.
  • Google's C++ style guide allows {} or continue for an empty loop body. The rule notes that only {} is safe with a constant condition.

Pages fixed

  • index.md: the catch (...) loop in the example main().
  • education/adc.md: the loop in the example terminate handler.

Both keep their comment, now inside the empty body. Apart from the rule's own ❌ example, these were the only matches on the site for the pattern in libhal/ci#134.

Clang deletes `while (true) { continue; }`: the compiler may assume a
loop without side effects ends, and only an empty body with a constant
condition is exempt (P2809). Polling loops on volatile or atomic memory
can keep `continue;`.

The examples on the home page and in the ADC guide now use an empty
body with the comment inside.
@kammce
kammce merged commit 160ff1e into main Nov 14, 2026
1 check passed
@kammce
kammce deleted the style-empty-infinite-loop branch November 14, 2026 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant