Skip to content

refactor(payload): mark infallible .expect() sites as unreachable!()#1884

Draft
goxberry wants to merge 1 commit into
goxberry/expect-cleanup-lading-throttlefrom
goxberry/expect-cleanup-payload-infallible
Draft

refactor(payload): mark infallible .expect() sites as unreachable!()#1884
goxberry wants to merge 1 commit into
goxberry/expect-cleanup-lading-throttlefrom
goxberry/expect-cleanup-payload-infallible

Conversation

@goxberry
Copy link
Copy Markdown
Contributor

@goxberry goxberry commented May 22, 2026

What does this PR do?

Converts 26 infallible-by-construction .expect() sites in lading_payload to .unwrap_or_else(|| unreachable!("...")). The runtime behavior is identical at every site (both forms panic if the impossible happens); the new form documents why the panic is unreachable at the call site, and tags the branch with the standard unreachable!() macro that Rust optimizers and humans both recognize.

This PR does not remove the lading_payload quarantine — that lands later in the sub-stack after Cat-1/Cat-2/Cat-4 sites are addressed.

Sites covered

Pattern Count Treatment
CONST_ARR.choose(rng).expect(...) against non-empty const arrays 20 .choose(rng).unwrap_or_else(|| unreachable!("CONST_ARR is a non-empty const array"))
write!(&mut Vec<u8>, ...).expect("formatting to Vec<u8> cannot fail") 5 .unwrap_or_else(|_| unreachable!("io::Write on Vec<u8> never errors"))
Option-after-guard (as_mut/take immediately after a function that populates the value) 3 .unwrap_or_else(|| unreachable!("<guard> populates <field> on Ok"))

Files touched: apache_common.rs, block.rs, datadog_logs.rs, dogstatsd.rs, json.rs, splunk_hec.rs, static_chunks.rs, static_timestamped.rs, syslog.rs. +69 −31.

Why this shape, not the alternatives

The first attempt at this PR replaced the const-array sites with CONST_ARR[rng.random_range(0..CONST_ARR.len())] — eliminating the Option entirely. While that approach has zero runtime panic surface, it (a) repeats the const name at the call site, (b) loses the "choose" reading, and (c) requires dropping the IndexedRandom import in some files. Reviewer feedback on the draft preferred the more drop-in form. Both forms are equally vulnerable to a refactor leaving CONST_ARR empty — neither has compile-time protection against that. Adding const _: () = assert!(!CONST.is_empty()); next to each declaration is a reasonable follow-up but is out of scope here.

Motivation

Second per-crate cleanup in the stack started by #1882. Strategy:

  • This PR (PR A): Category-3 infallible sites — the only ones where the conversion is mechanical and risk-free.
  • Next PR (PR B): Category-1 (?-propagation) + Category-4 (genuine panic intent, where the .expect() is the contract).
  • Final PR (PR C): Category-2 (function-signature changes) + drop the lading_payload quarantine.

Related issues

Stacked on #1883.

Additional Notes

  • cargo build --all-targets --all-features
  • cargo clippy --all-targets --all-features ✓ (full ci/clippy)
  • cargo test -p lading-payload --lib ✓ (244 passed, 0 failed)
  • A full inventory pass classified ~78 production .expect() sites in lading_payload into four categories; the 26 sites here are the strict Category-3 subset. Categories 1, 2, 4 are not touched by this PR.

Converts 26 infallible-by-construction `.expect()` sites in lading_payload
to `.unwrap_or_else(|| unreachable!("..."))`. The runtime behavior is
unchanged at every site — both `.expect()` and `unreachable!()` panic —
but the new form documents at the call site *why* the panic cannot fire,
not just *what message would be printed if it did*.

Sites covered:
- 20 `CONST_ARR.choose(rng)` against non-empty `const` arrays in
  `apache_common.rs`, `datadog_logs.rs`, `json.rs`, `splunk_hec.rs`,
  and `syslog.rs`.
- 5 `write!(&mut Vec<u8>, ...)` calls (`apache_common.rs`, `dogstatsd.rs`
  ×2, `splunk_hec.rs`, `syslog.rs`). `io::Write` on `Vec<u8>` cannot fail.
- 3 `Option`-after-guard sites: `as_mut().expect(...)` post-`ensure_reader()`
  in `static_chunks.rs`, `take().expect(...)` post-`fill_next_block()` in
  `static_timestamped.rs`, and `Byte::from_u64_with_unit(1, MiB)` in
  `block.rs` (also drops the now-stale `# Panics` doc since the function
  no longer documents an `.expect()`).

This is the second per-crate cleanup in the stack started by
"chore(clippy): forbid .expect() in production code". The crate-root
`#![allow(clippy::expect_used)]` quarantine on `lading_payload` is NOT
removed here — Cat-1 (?-prop), Cat-2 (signature changes), and Cat-4
(genuine panic intent) sites remain. The quarantine drop lands in the
final PR of this sub-stack after those layers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor Author

goxberry commented May 22, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant