Loop with an empty body busy-waits and burns CPU
ID |
rust.correctness.empty_loop_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Infinite Loop |
Language |
Rust |
Description
This loop has an empty body, so it spins at full speed without doing any work and without yielding the core. Wait on a channel, condition variable or JoinHandle instead; if a spin is genuinely wanted, call std::hint::spin_loop() inside the body.
Rationale
This loop has an empty body, so it spins at full speed without doing any work and without yielding the core. Wait on a channel, condition variable or JoinHandle instead; if a spin is genuinely wanted, call std::hint::spin_loop() inside the body.
The following code illustrates the pattern detected by this rule:
use std::sync::atomic::{AtomicBool, Ordering};
fn spin_forever() {
// FLAGGED: Loop with an empty body busy-waits and burns CPU
loop {}
}