Back to all reviewers

Use Option combinators

tokio-rs/axum
Based on 4 comments
Rust

Instead of verbose conditional logic, prefer Rust's Option combinators like map, and_then, and filter for handling potentially null values. This creates more concise, readable code that expresses intent clearly and reduces the chance of null-related bugs.

Null Handling Rust

Reviewer Prompt

Instead of verbose conditional logic, prefer Rust’s Option combinators like map, and_then, and filter for handling potentially null values. This creates more concise, readable code that expresses intent clearly and reduces the chance of null-related bugs.

For example, replace:

let result = if let Some(value) = optional_value {
    Some(process(value))
} else {
    None
};

With:

let result = optional_value.map(process);

Similarly, when handling multiple optional values, use and_then instead of nested if-lets:

// Instead of this
let session_cookie =
    if let Ok(TypedHeader(cookie)) = TypedHeader::<Cookie>::from_request(req).await {
        cookie.get(AXUM_SESSION_COOKIE_NAME).map(|x| x.to_owned())
    } else {
        None
    };

// Prefer this
let session_cookie = Option::<TypedHeader<Cookie>>::from_request(req).await
    .and_then(|cookie| Some(cookie.get(AXUM_SESSION_COOKIE_NAME)?.to_owned()));

When validating values that might fail, explicitly set to None on failure:

self.filename = if let Ok(filename) = value.try_into() {
    Some(filename)
} else {
    trace!("Attachment filename contains invalid characters");
    None
};

Using Option combinators not only makes code more concise but also makes the intent clearer and reduces the chance of null-handling mistakes.

4
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions