Replace .unwrap() calls and sentinel values with safe null handling patterns to prevent runtime panics and improve code robustness.
Key Issues:
.unwrap() on operations that could fail, especially downcasts and IPC operationsInvalid or None = -1 instead of proper Option typesSafe Patterns:
if let Some(value) = optional_value instead of .unwrap().expect("meaningful message") when unwrapping is truly safeOption<T> types? operator for early returns with OptionsOption<T> instead of default values when absence is meaningfulExample:
// ❌ Unsafe - could panic
let video_elem = self.downcast::<HTMLVideoElement>().unwrap();
video_elem.resize(width, height);
// ✅ Safe pattern
if let Some(video_elem) = self.downcast::<HTMLVideoElement>() {
video_elem.resize(width, height);
}
// ❌ Sentinel value antipattern
pub enum LargestContentfulPaintType {
None = -1,
// ...
}
// ✅ Proper Option usage
pub fn get_lcp_candidate() -> Option<LCPCandidate> {
// ...
}
This prevents runtime crashes and makes null states explicit in the type system, improving both safety and code clarity.
Enter the URL of a public GitHub repository