Skip to content

fix: remove #[must_use] from Drain - #486

Merged
ParkMyCar merged 2 commits into
mainfrom
fix/drain-must-use
Jul 12, 2026
Merged

ParkMyCar merged 2 commits into
mainfrom
fix/drain-must-use

Conversation

@ParkMyCar

@ParkMyCar ParkMyCar commented Jul 12, 2026 •

Copy link
Copy Markdown
Owner

Closes #435.

Drain was marked #[must_use = "iterators are lazy and do nothing unless consumed"], but that's misleading here: dropping a Drain still removes the selected range from the source CompactString (that's what its Drop impl does), so drain(range) purely to delete a range — without consuming the iterator — is a valid and common use. alloc::string::Drain isn't #[must_use] for exactly this reason.

This removes the attribute and documents why it's deliberately absent. Adds tests covering drain-without-consuming (inline and heap), partial consumption still removing the full range, the yielded characters, empty/full ranges, a multibyte boundary, and a proptest comparing against String::drain.

`CompactString::drain` removes the selected range in `Drain`'s `Drop` impl, so
`drain(range)` is a valid and common way to just delete a range without consuming
the returned iterator. Marking `Drain` as `#[must_use]` wrongly warned on that
pattern; `alloc::string::Drain` isn't `must_use` for the same reason.

Also adds the previously-missing drain test coverage, including the
drop-without-consuming path (inline + heap), partial-consume-then-drop removing
the full range, multi-byte boundaries, and a proptest asserting parity with
`String::drain` for both the consumed and dropped cases.

Closes #435.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ParkMyCar
ParkMyCar enabled auto-merge (squash) July 12, 2026 03:15
test_drain_without_consuming_removes_range_inline used a 13-byte string and
asserted it wasn't heap allocated, but MAX_SIZE is only 12 on 32-bit targets, so
the string was heap allocated there and the assertion failed. Use a 9-byte
string that stays inline on both 32- and 64-bit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VGTA38KYXfd6EgVAiUYJ1e
@ParkMyCar
ParkMyCar merged commit f464095 into main Jul 12, 2026
31 checks passed
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.

Drain should not be marked as must_use

1 participant