{"record":{"id":"b45493772d9bde9e","repo":"rayon-rs/rayon","slug":"range-start-should-be-range-end","errorCode":null,"errorMessage":"range start {:?} should be <= range end {:?}","messagePattern":"range start (.+?) should be <= range end (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math.rs","lineNumber":18,"sourceCode":"use std::ops::{Bound, Range, RangeBounds};\n\n/// Normalize arbitrary `RangeBounds` to a `Range`\npub(super) fn simplify_range(range: impl RangeBounds<usize>, len: usize) -> Range<usize> {\n    let start = match range.start_bound() {\n        Bound::Unbounded => 0,\n        Bound::Included(&i) if i <= len => i,\n        Bound::Excluded(&i) if i < len => i + 1,\n        bound => panic!(\"range start {bound:?} should be <= length {len}\"),\n    };\n    let end = match range.end_bound() {\n        Bound::Unbounded => len,\n        Bound::Excluded(&i) if i <= len => i,\n        Bound::Included(&i) if i < len => i + 1,\n        bound => panic!(\"range end {bound:?} should be <= length {len}\"),\n    };\n    if start > end {\n        panic!(\n            \"range start {:?} should be <= range end {:?}\",\n            range.start_bound(),\n            range.end_bound()\n        );\n    }\n    start..end\n}\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/rayon-rs/rayon/blob/ee0a00bdb1ab039e178a215ad5712fb7fa58e58f/src/math.rs#L1-L26","documentation":"Panic raised by the range-normalization helper simplify_range when the caller supplies a range whose start bound exceeds its end bound (after resolution against the collection length len). The function converts any RangeBounds into a plain Range<usize>; the start match accepts start <= len and the end match accepts end <= len, but a resulting start > end has no valid interpretation (an empty range cannot be represented here), so it panics. This is a caller-input validation guard: the faulty input is a range like 3..1 or ..0 with start beyond end. Reached via collection operations built on it, e.g. Drain.","triggerScenarios":"Calling drain/splice-style APIs with `Range` where start > end, e.g. `drain(10..3)`, often because variables were swapped or the range was computed from sorted positions that changed.","commonSituations":"Swapped arguments to a helper producing a range; reusing a range captured before the collection shrank; min/max mixups.","solutions":["Validate `start <= end` before calling the API (use `range.clone().next()` / assert)","Use `Range::new(start.min(end), start.max(end))` style normalization","Check whether the intended API is `drain(start..)` vs `drain(..end)` with swapped args"],"exampleFix":"// before\nvec.par_drain(hi..lo); // panics if hi > lo\n// after\nassert!(lo <= hi, \"invalid range\");\nvec.par_drain(lo..hi);","handlingStrategy":"validation","validationCode":"fn range_ordered(r: &std::ops::Range<usize>) -> bool { r.start <= r.end }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert start <= end at range-construction sites","Use a range-builder function that orders bounds","Name parameters clearly to avoid swapped arguments"],"tags":["rayon","panic","range","inverted-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"ee0a00bdb1ab039e178a215ad5712fb7fa58e58f","analyzedAt":"2026-09-07T23:28:47.588Z","contentChangedAt":"2026-09-07T23:28:47.588Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}