Back to all reviewers

Enforce strong optional types

astral-sh/uv
Based on 3 comments
Rust

Use strong typing and early validation to handle optional values and prevent null-related issues. Prefer enums and specific types over strings when dealing with optional data, and validate optional values as early as possible in the data flow.

Null Handling Rust

Reviewer Prompt

Use strong typing and early validation to handle optional values and prevent null-related issues. Prefer enums and specific types over strings when dealing with optional data, and validate optional values as early as possible in the data flow.

Key practices:

  1. Use enums instead of strings for known value sets
  2. Leverage bool::then for cleaner optional handling
  3. Validate optional data at structure boundaries

Example: ```rust // Instead of struct Config { kind: String, // Could be “realm” or “index” value: Option }

// Prefer enum ConfigKind { Realm(String), Index(String) } struct Config { kind: ConfigKind, value: Option }

// Use bool::then for cleaner optional handling let value = condition.then(|| compute_value());

// Validate optional data early fn get_project_version(doc: &mut Document) -> Result<Version, Error> { let project = doc.get(“project”) .ok_or(Error::MissingProject)?;

let version = project
    .get("version")
    .and_then(Item::as_value)
    .and_then(Value::as_str)
    .ok_or(Error::MissingVersion)?;

Version::from_str(version) }
3
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions