Back to all reviewers

Proper Option type usage

neondatabase/neon
Based on 4 comments
Rust

Use Option only for truly optional values that can meaningfully be None. Avoid using Option when a value is always required or when defaulting to empty/invalid values.

Null Handling Rust

Reviewer Prompt

Use Option only for truly optional values that can meaningfully be None. Avoid using Option when a value is always required or when defaulting to empty/invalid values.

Key guidelines:

  1. Use Option when a value may legitimately be absent
  2. Avoid Option if a field is never None in practice
  3. Don’t use empty/default values as substitutes for Option

Example:

// Good: Truly optional value
struct Config {
    refresh_interval: Option<Duration>,  // May not be configured
    error_message: Option<String>        // Only present on error
}

// Bad: Using Option when value is never None
struct AuthInterceptor {
    tenant_id: AsciiMetadataValue,
    shard_id: Option<AsciiMetadataValue>  // If always present, remove Option
}

// Bad: Using empty string instead of Option
struct Settings {
    base_url: String  // Empty string is meaningless, use Option<String>
}
4
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions