{"record":{"id":"546f609a9b63a587","repo":"rayon-rs/rayon","slug":"range-end-bound-should-be-length-len","errorCode":null,"errorMessage":"range end {bound:?} should be <= length {len}","messagePattern":"range end (.+?) should be <= length (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math.rs","lineNumber":15,"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 end bound exceeds the collection length `len`. Valid ends are Unbounded, Excluded(i<=len), or Included(i<len); otherwise the slice end is out of range.","triggerScenarios":"Calling a drain-style API with an end bound past the collection length, e.g. `drain(..len+1)` or `drain(..=len)` on a collection of exactly `len` items.","commonSituations":"Hardcoding an end index computed from a stale/other collection's length; off-by-one when using inclusive ranges (`..=len` instead of `..len`).","solutions":["Clamp the end with `end.min(len)` before constructing the range","Use `..len` not `..=len` for a full-range drain","Fetch `len` from the same collection being drained, immediately before the call"],"exampleFix":"// before\nvec.par_drain(..=vec.len()); // Included(len) panics when len>0\n// after\nvec.par_drain(..vec.len()); // or vec.par_drain(..);","handlingStrategy":"validation","validationCode":"fn valid_drain_end(range: std::ops::Range<usize>, len: usize) -> bool {\n    range.end <= len\n}","typeGuard":"fn end_in_bounds(e: usize, len: usize) -> bool { e <= len }","tryCatchPattern":null,"preventionTips":["Prefer `..len` over `..=len`","Clamp end with .min(len)","Never reuse ranges from stale collections"],"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"}