Fix repeated input evaluation in bitflags_match! #499

Merged
christophacham merged 1 commit from fix/bitflags-match-evaluate-once into main 2026-09-18 01:59:49 -05:00
christophacham commented 2026-09-16 03:57:09 -05:00 (Migrated from github.com)

Evaluate the input once without moving it. Add regression tests for later-arm matches, fallback matches, default-only invocations, and non-Copy inputs. Verified with cargo test --all-features and cargo fmt --all -- --check.

bitflags_match! could evaluate its input repeatedly—once per attempted match—instead of once overall.

So bitflags_match!(next_flags(), …) could call next_flags() several times and match against different values. With only a default arm, it never called it.

The fix evaluates the input once and reuses it without moving it. Four regression tests cover this.

Evaluate the input once without moving it. Add regression tests for later-arm matches, fallback matches, default-only invocations, and non-Copy inputs. Verified with cargo test --all-features and cargo fmt --all -- --check. `bitflags_match!` could evaluate its input repeatedly—once per attempted match—instead of once overall. So `bitflags_match!(next_flags(), …)` could call `next_flags()` several times and match against different values. With only a default arm, it never called it. The fix evaluates the input once and reuses it without moving it. Four regression tests cover this.
KodrAus (Migrated from github.com) approved these changes 2026-09-16 15:29:40 -05:00
KodrAus (Migrated from github.com) left a comment

Thanks @christophacham!

Thanks @christophacham!
Sign in to join this conversation.
No reviewers
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags!499
No description provided.