Skip to content

Commit dfea61d

Browse files
goxberryclaude
andcommitted
refactor(payload): annotate intentional-panic .expect() sites
Eleven functions in `lading_payload` contain `.expect()` calls that are the function's documented contract: violating the precondition is a programming error, and panicking is the intended response. Each such function gets `#[expect(clippy::expect_used, reason = "...")]` so the workspace-level lint is bypassed at the source with an explicit reason. Covered functions and the invariant each preserves: - `block::Cache::read_at` — documents in its doc comment that it panics if reads exceed machine-word bytes; the `usize::try_from(u64)` calls inside are the documented contract. - `RandomStringPool::using_handle` and `StringListPool::using_handle` — the `Handle` enum has two variants, one per pool; a handle of the wrong variant is a programming error (cross-pool misuse). - `Display::fmt` impls for `Event` (in `dogstatsd/event.rs`), `ServiceCheck` (in `dogstatsd/service_check.rs`), and `Count`, `Gauge`, `Timer`, `Dist`, `Set`, `Histogram` (all in `dogstatsd/metric.rs`) — each formats tag keys/values by handle-table lookup against `self.pools`, which issued those handles at construction time; a miss indicates an internal invariant violation. The `.expect()` calls themselves are unchanged. No runtime behavior change. Same stack as #1882#1890. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8215c40 commit dfea61d

7 files changed

Lines changed: 52 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8+
- Annotated 11 `lading_payload` functions that intentionally panic on
9+
invariant violations with `#[expect(clippy::expect_used, reason = "...")]`.
10+
Covered: `block::Cache::read_at` (documented `usize`/`u64` overflow
11+
panic), `RandomStringPool::using_handle` and
12+
`StringListPool::using_handle` (handle-type contract), and the
13+
`Display::fmt` impls for `Event`, `ServiceCheck`, `Count`, `Gauge`,
14+
`Timer`, `Dist`, `Set`, `Histogram` (tag-pool handle lookups). No
15+
runtime behavior change; the `.expect()` calls remain in place.
816
- Replaced 6 in-function-invariant `.expect()` sites in `lading_payload`
917
with `.unwrap_or_else(|_| unreachable!("..."))` /
1018
`.unwrap_or_else(|| unreachable!("..."))`. Covered: `usize → u32`

lading_payload/src/block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ impl Cache {
506506
/// # Panics
507507
///
508508
/// Function will panic if reads are larger than machine word bytes wide.
509+
#[expect(
510+
clippy::expect_used,
511+
reason = "u64-to-usize panic on overflow is the documented contract of this function"
512+
)]
509513
pub fn read_at(&self, offset: u64, size: usize) -> Bytes {
510514
let mut data = BytesMut::with_capacity(size);
511515

lading_payload/src/common/strings/random_string_pool.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ impl Pool for RandomStringPool {
131131
}
132132

133133
#[inline]
134+
#[expect(
135+
clippy::expect_used,
136+
reason = "handle was issued by this pool earlier in the call chain; a non-PosAndLength handle indicates a cross-pool programming error"
137+
)]
134138
fn using_handle(&self, handle: Handle) -> Option<&str> {
135139
let (offset, length) = handle
136140
.as_pos_and_length()

lading_payload/src/common/strings/string_list_pool.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ impl Pool for StringListPool {
417417
Some((&self.metric_names[idx], Handle::Index(idx)))
418418
}
419419

420+
#[expect(
421+
clippy::expect_used,
422+
reason = "handle was issued by this pool earlier in the call chain; a non-Index handle indicates a cross-pool programming error"
423+
)]
420424
fn using_handle(&self, handle: Handle) -> Option<&str> {
421425
let idx = handle
422426
.as_index()

lading_payload/src/dogstatsd/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ pub struct Event<'a> {
102102
}
103103

104104
impl fmt::Display for Event<'_> {
105+
#[expect(
106+
clippy::expect_used,
107+
reason = "tag handles were issued by self.pools at event construction; mismatched lookup indicates an internal invariant violation"
108+
)]
105109
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106110
// _e{<TITLE_UTF8_LENGTH>,<TEXT_UTF8_LENGTH>}:<TITLE>|<TEXT>|d:<TIMESTAMP>|h:<HOSTNAME>|p:<PRIORITY>|t:<ALERT_TYPE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
107111
write!(

lading_payload/src/dogstatsd/metric.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ pub struct Count<'a> {
339339
}
340340

341341
impl fmt::Display for Count<'_> {
342+
#[expect(
343+
clippy::expect_used,
344+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
345+
)]
342346
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
343347
// <METRIC_NAME>:<VALUE>|d|#<TAG_KEY_1>:<TAGVALUE_1>,<TAG_2>|c:<CONTAINER_ID>
344348
// <METRIC_NAME>:<VALUE1>:<VALUE2>:<VALUE3>|d|@<SAMPLE_RATE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
@@ -411,6 +415,10 @@ pub struct Gauge<'a> {
411415
}
412416

413417
impl fmt::Display for Gauge<'_> {
418+
#[expect(
419+
clippy::expect_used,
420+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
421+
)]
414422
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
415423
// <METRIC_NAME>:<VALUE>|d|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|c:<CONTAINER_ID>
416424
// <METRIC_NAME>:<VALUE1>:<VALUE2>:<VALUE3>|d|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
@@ -479,6 +487,10 @@ pub struct Timer<'a> {
479487
}
480488

481489
impl fmt::Display for Timer<'_> {
490+
#[expect(
491+
clippy::expect_used,
492+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
493+
)]
482494
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483495
// <METRIC_NAME>:<VALUE>|d|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|c:<CONTAINER_ID>
484496
// <METRIC_NAME>:<VALUE1>:<VALUE2>:<VALUE3>|d|@<SAMPLE_RATE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
@@ -547,6 +559,10 @@ pub struct Dist<'a> {
547559
}
548560

549561
impl fmt::Display for Dist<'_> {
562+
#[expect(
563+
clippy::expect_used,
564+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
565+
)]
550566
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
551567
// <METRIC_NAME>:<VALUE>|d|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|c:<CONTAINER_ID>
552568
// <METRIC_NAME>:<VALUE1>:<VALUE2>:<VALUE3>|d|@<SAMPLE_RATE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>
@@ -613,6 +629,10 @@ pub struct Set<'a> {
613629
}
614630

615631
impl fmt::Display for Set<'_> {
632+
#[expect(
633+
clippy::expect_used,
634+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
635+
)]
616636
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
617637
// <METRIC_NAME>:<VALUE>|s|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|c:<CONTAINER_ID>
618638
let name = &self.name;
@@ -674,6 +694,10 @@ pub struct Histogram<'a> {
674694
}
675695

676696
impl fmt::Display for Histogram<'_> {
697+
#[expect(
698+
clippy::expect_used,
699+
reason = "tag handles were issued by self.pools at metric construction; mismatched lookup indicates an internal invariant violation"
700+
)]
677701
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
678702
// <METRIC_NAME>:<VALUE>|h|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|c:<CONTAINER_ID>
679703
// <METRIC_NAME>:<VALUE1>:<VALUE2>:<VALUE3>|h|@<SAMPLE_RATE>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>

lading_payload/src/dogstatsd/service_check.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ pub struct ServiceCheck<'a> {
7171
}
7272

7373
impl fmt::Display for ServiceCheck<'_> {
74+
#[expect(
75+
clippy::expect_used,
76+
reason = "tag handles were issued by self.pools at service-check construction; mismatched lookup indicates an internal invariant violation"
77+
)]
7478
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7579
// _sc|<NAME>|<STATUS>|d:<TIMESTAMP>|h:<HOSTNAME>|#<TAG_KEY_1>:<TAG_VALUE_1>,<TAG_2>|m:<SERVICE_CHECK_MESSAGE>
7680
write!(

0 commit comments

Comments
 (0)