diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c466871..02258af49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +- Replaced 5 additional infallible-by-construction `.expect()` sites in + `lading_payload` with `.unwrap_or_else(|_| unreachable!("..."))` / + `.unwrap_or_else(|| unreachable!("..."))`. Covered: 4 sites in + `string_list_pool::validate_range` (parse-and-chars-iter calls guarded by + flags set immediately above) and 1 site in + `templated_json::resolver::resolve_def` (take-from-`Option`-Vec inside a + three-color visit state machine). No runtime behavior change. - Replaced 26 infallible-by-construction `.expect()` sites in `lading_payload` with `.unwrap_or_else(|| unreachable!("..."))`, making the impossibility structural and explicit. Sites covered: 20 `CONST_ARR.choose(rng)` against diff --git a/lading_payload/src/common/strings/string_list_pool.rs b/lading_payload/src/common/strings/string_list_pool.rs index c2e4fa80d..798a60484 100644 --- a/lading_payload/src/common/strings/string_list_pool.rs +++ b/lading_payload/src/common/strings/string_list_pool.rs @@ -174,12 +174,12 @@ impl StringListPool { // Validate that ranges are not backwards if start_is_num && end_is_num { - let start_num = start - .parse::() - .expect("start_is_num guarantees this parses"); - let end_num = end - .parse::() - .expect("end_is_num guarantees this parses"); + let start_num = start.parse::().unwrap_or_else(|_| { + unreachable!("start_is_num was set from start.parse::().is_ok()") + }); + let end_num = end.parse::().unwrap_or_else(|_| { + unreachable!("end_is_num was set from end.parse::().is_ok()") + }); if start_num > end_num { return Err(Error::MalformedPattern { pattern: format!("{{{{{start}-{end}}}}}"), @@ -189,11 +189,11 @@ impl StringListPool { let start_char = start .chars() .next() - .expect("start_is_char guarantees non-empty"); + .unwrap_or_else(|| unreachable!("start_is_char requires start.len() == 1")); let end_char = end .chars() .next() - .expect("end_is_char guarantees non-empty"); + .unwrap_or_else(|| unreachable!("end_is_char requires end.len() == 1")); if start_char > end_char { return Err(Error::MalformedPattern { pattern: format!("{{{{{start}-{end}}}}}"), diff --git a/lading_payload/src/templated_json/resolver.rs b/lading_payload/src/templated_json/resolver.rs index 30252653d..5108cc651 100644 --- a/lading_payload/src/templated_json/resolver.rs +++ b/lading_payload/src/templated_json/resolver.rs @@ -152,7 +152,7 @@ impl Resolver { self.grey[idx] = true; let vgen = self.raw_defs[idx] .take() - .expect("raw definition should exist"); + .unwrap_or_else(|| unreachable!("raw_defs[idx] is Some when grey[idx] is first set")); let resolved = vgen.resolve(self)?; self.resolved_defs[idx] = Some(resolved); self.grey[idx] = false;