Back to all reviewers

Default over unsafe initialization

rust-lang/rust
Based on 3 comments
Rust

Prefer using safe initialization methods like `Default::default()` over unsafe alternatives like `MaybeUninit::uninit()` or null values when initializing variables. This reduces the risk of undefined behavior and eliminates unnecessary unsafe blocks.

Null Handling Rust

Reviewer Prompt

Prefer using safe initialization methods like Default::default() over unsafe alternatives like MaybeUninit::uninit() or null values when initializing variables. This reduces the risk of undefined behavior and eliminates unnecessary unsafe blocks.

Example - Instead of:

let mut config_data: MaybeUninit<ConfigData> = MaybeUninit::zeroed();
// ... later ...
unsafe { config_data.assume_init() }

Prefer:

let config_data = ConfigData::default();

This approach:

  • Reduces unsafe code blocks
  • Provides better guarantees about initialization state
  • Makes code more maintainable and robust against future type changes
  • Prevents potential undefined behavior from uninitialized memory

Only use MaybeUninit or manual null handling when there are specific performance requirements or when dealing with FFI boundaries where default initialization is not possible.

3
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions