Back to all reviewers

Use Option methods idiomatically

tokio-rs/tokio
Based on 6 comments
Rust

When dealing with Optional values, prefer Rust's built-in Option methods over manual null checks. This makes code more concise, self-documenting, and less error-prone by leveraging Rust's type system to enforce null safety at compile time.

Null Handling Rust

Reviewer Prompt

When dealing with Optional values, prefer Rust’s built-in Option methods over manual null checks. This makes code more concise, self-documenting, and less error-prone by leveraging Rust’s type system to enforce null safety at compile time.

For optional value checking:

// Instead of:
if self.processing_scheduled_tasks_started_at.is_some() {
    let busy_duration = self.processing_scheduled_tasks_started_at.unwrap().elapsed();
    // Use busy_duration...
}

// Use:
if let Some(processing_scheduled_tasks_started_at) = self.processing_scheduled_tasks_started_at {
    let busy_duration = processing_scheduled_tasks_started_at.elapsed();
    // Use busy_duration...
}

For lazy initialization:

// Instead of:
if me.entry.is_none() {
    let entry = TimerEntry::new(me.handle, deadline);
    me.entry.as_mut().set(Some(entry));
}

// Use:
let entry = me.entry.get_or_insert_with(|| TimerEntry::new(me.handle, deadline));

For error handling with Option:

// Instead of:
let n = match u32::try_from(n) {
    Ok(n) => n,
    Err(_) => return None,
};

// Use:
let n = u32::try_from(n).ok()?;
6
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions