Back to all reviewers

Use safe optional defaults

jj-vcs/jj
Based on 3 comments
Rust

When working with optional values, prefer safe extraction methods that provide defaults rather than methods that can panic. Use `unwrap_or()`, `unwrap_or_default()`, or `unwrap_or_else()` instead of `unwrap()` to handle `None` cases gracefully.

Null Handling Rust

Reviewer Prompt

When working with optional values, prefer safe extraction methods that provide defaults rather than methods that can panic. Use unwrap_or(), unwrap_or_default(), or unwrap_or_else() instead of unwrap() to handle None cases gracefully.

This pattern prevents runtime panics and makes code more robust by explicitly handling the absence of values. It’s particularly important when dealing with configuration values, map lookups, or any operation that might not return a result.

Examples of good patterns:

// Instead of potentially panicking
let executable = file.executable.unwrap();

// Use safe defaults
let executable = file.executable.unwrap_or(false);

// For common defaults
let config = maybe_read_to_string(&path)?.unwrap_or_default();

// For map lookups
let parent = old_to_new.get(p.id()).unwrap_or(&p);

This approach makes the code’s behavior predictable and prevents unexpected crashes when optional values are absent, while clearly documenting the intended fallback behavior.

3
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions