]> git.lizzy.rs Git - rust.git/commitdiff
Fix off-by-one in String::remove
authordiwic <diwic@users.noreply.github.com>
Sat, 2 Jan 2016 21:36:50 +0000 (22:36 +0100)
committerdiwic <diwic@users.noreply.github.com>
Sat, 2 Jan 2016 21:36:50 +0000 (22:36 +0100)
Obviously we can't remove the character one past the end of the String. And we can't today either - we'll just panic at char_at() instead - but if we're going to keep that assertion, we should at least have a correct assertion.

src/libcollections/string.rs

index 931092f69242e00b91e399f4b260b33f111ef3a8..d2cbcad875f347a5249f547829ca463c424bb431 100644 (file)
@@ -1029,8 +1029,8 @@ pub fn pop(&mut self) -> Option<char> {
     ///
     /// # Panics
     ///
-    /// Panics if `idx` is larger than the `String`'s length, or if it does not
-    /// lie on a [`char`] boundary.
+    /// Panics if `idx` is larger than or equal to the `String`'s length,
+    /// or if it does not lie on a [`char`] boundary.
     ///
     /// [`char`]: ../primitive.char.html
     ///
@@ -1049,7 +1049,7 @@ pub fn pop(&mut self) -> Option<char> {
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn remove(&mut self, idx: usize) -> char {
         let len = self.len();
-        assert!(idx <= len);
+        assert!(idx < len);
 
         let ch = self.char_at(idx);
         let next = idx + ch.len_utf8();