Skip to contentSlop Block

prefer-guard-clause

Warning

When a function body is a single if, invert the condition and return early.

fn save(user: &User) {	if user.active {The whole function body sits inside one if		validate(user);		store(user);	}}
fn save(user: Option<User>) {	if let Some(user) = user {The whole function body sits inside one if		validate(&user);		store(&user);	}}
// ast-grep-ignore: prefer-guard-clause-rust

How to fix

Invert the condition and return early, then write the rest of the body one level up. For if let, use let-else.