about summary refs log tree commit diff
path: root/library/core/src/ops
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2020-10-19 10:02:51 -0700
committerJosh Stone <jistone@redhat.com>2020-10-19 10:02:51 -0700
commitb62b352f4779072c07c110a63fd83afa9508b9e9 (patch)
tree4a9c4181acb3ce6ee324bb9d4aa1aa0b21ee9c4c /library/core/src/ops
parentcb2462c53f2cc3f140c0f1ea0976261cab968a34 (diff)
Check for exhaustion in RangeInclusive::contains
When a range has finished iteration, `is_empty` returns true, so it
should also be the case that `contains` returns false.
Diffstat (limited to 'library/core/src/ops')
-rw-r--r--library/core/src/ops/range.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs
index 4423cfc27dd..1da186d9fbb 100644
--- a/library/core/src/ops/range.rs
+++ b/library/core/src/ops/range.rs
@@ -479,13 +479,23 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
     /// assert!(!(0.0..=f32::NAN).contains(&0.0));
     /// assert!(!(f32::NAN..=1.0).contains(&1.0));
     /// ```
+    ///
+    /// This method always returns `false` after iteration has finished:
+    ///
+    /// ```
+    /// let mut r = 3..=5;
+    /// assert!(r.contains(&3) && r.contains(&5));
+    /// for _ in r.by_ref() {}
+    /// // Precise field values are unspecified here
+    /// assert!(!r.contains(&3) && !r.contains(&5));
+    /// ```
     #[stable(feature = "range_contains", since = "1.35.0")]
     pub fn contains<U>(&self, item: &U) -> bool
     where
         Idx: PartialOrd<U>,
         U: ?Sized + PartialOrd<Idx>,
     {
-        <Self as RangeBounds<Idx>>::contains(self, item)
+        !self.exhausted && <Self as RangeBounds<Idx>>::contains(self, item)
     }
 
     /// Returns `true` if the range contains no items.