{"record":{"id":"7309904ef9adc33e","repo":"rayon-rs/rayon","slug":"range-start-bound-should-be-length-len","errorCode":null,"errorMessage":"range start {bound:?} should be <= length {len}","messagePattern":"range start (.+?) should be <= length (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math.rs","lineNumber":9,"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":"`simplify_range` normalizes any RangeBounds to a Range and panics when the start bound exceeds the collection length `len`. The library requires start <= len (Included) or start < len (Excluded); anything else is an out-of-range slice argument.","triggerScenarios":"Calling a draining/splice-style API (e.g. `drain(n..)` on a collection of length < n, or `drain(m..)` with m > len), or an Excluded start equal to len (e.g. `drain((len-0..).start_bound_excluded)` patterns like `range start (len+1)`).","commonSituations":"Computing a drain start from user input or a computed offset that wasn't clamped to the collection length; off-by-one after removing items before draining.","solutions":["Clamp the range start to the collection length before the call","Use `start.min(len)` when constructing the range","Check `len` first and skip the drain when start >= len","Guard Excluded starts so start+1 <= len"],"exampleFix":"// before\nvec.par_drain(start..end);\n// after\nlet start = start.min(vec.len());\nlet end = end.min(vec.len());\nvec.par_drain(start..end);","handlingStrategy":"validation","validationCode":"fn valid_drain_start(range: std::ops::Range<usize>, len: usize) -> bool {\n    range.start <= len\n}","typeGuard":"fn in_bounds(i: usize, len: usize) -> bool { i <= len }","tryCatchPattern":null,"preventionTips":["Clamp range bounds to len before drain","Recompute len immediately before slicing","Write a helper that sanitizes Range<usize> inputs"],"tags":["rayon","panic","range","out-of-bounds"],"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"}