From 387568cd564317ca7491e6960ddcbe13beecae13 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Mon, 9 Nov 2020 22:34:31 +0100 Subject: [PATCH] Added SAFETY comment as request --- library/alloc/src/collections/binary_heap.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 17f0668c0ea35..97ebc12175f73 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -495,6 +495,10 @@ impl BinaryHeap { let mut end = self.len(); while end > 1 { end -= 1; + // SAFETY: `end` goes from `self.len() - 1` to 1 (both included), + // so it's always a valid index to access. + // It is safe to access index 0 (i.e. `ptr`), because + // 1 <= end < self.len(), which means self.len() >= 2. unsafe { let ptr = self.data.as_mut_ptr(); ptr::swap(ptr, ptr.add(end));