Rust

Rust

Made by DeepSource

Found manual implementation of Result::is_ok RS-W1055

Anti-pattern
Minor

Option::is_some and Option::is_none are functions that tell us what is contained within an Option type without unwrapping or pattern-matching on it. To "peek" into a Result type in a similar manner, one could convert an Result into an Option by calling Result::ok or Result::err and then use Option::is_some or Option::is_none. However, Result already has functions that let us peek into the state of the enum: Result::is_ok and Result::is_err.

Consider using these functions instead of conversions:

  • .ok().is_some() is equivalent to .is_ok()
  • .ok().is_none() is equivalent to .is_err()
  • .err().is_some() is equivalent to .is_err()
  • .err().is_none() is equivalent to .is_ok()

Bad practice

let x = Ok(5);

if x.ok().is_some() {
    /* ... */ 
}

Recommended

let x = Ok(5);

if x.is_ok() {
    /* ... */ 
}