prefer-early-continue
WarningWarningWarningWarningWhen a loop body is a single if, invert the condition and continue early.
fn run(users: &[User]) { for user in users { if user.active { validate(user); store(user); } }}fn run(users: &[User]) { for user in users { if !user.active { continue; } validate(user); store(user); }}for (const user of users) { if (user.active) { validate(user); store(user); }}while (queue.length > 0) { if (ready) { process(queue.pop()); notify(); }}for (const user of users) { if (!user.active) { continue; } validate(user); store(user);}void run(List<User> users) { for (final user in users) { if (user.active) { validate(user); store(user); } }}void run(List<User> users) { for (final user in users) { if (!user.active) { continue; } validate(user); store(user); }}class A { void run(List<User> users) { for (User user : users) { if (user.active) { validate(user); process(user); } } }}class A { void run(List<User> users) { for (User user : users) { if (!user.active) continue; process(user); } }}How to fix
Invert the condition and continue, then write the rest of the body one level up.
How to fix
Invert the condition and continue, then write the rest of the body one level up.
How to fix
Invert the condition and continue, then write the rest of the body one level up.
How to fix
Invert the condition and continue, then write the rest of the loop body one level up.