Skip to content

Commit

Permalink
Add #[must_use] to remaining alloc functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkugelman authored and Kugelman, John R committed Oct 15, 2021
1 parent e1e9319 commit fb2d0f5
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 23 deletions.
4 changes: 2 additions & 2 deletions library/alloc/benches/btree/map.rs
Expand Up @@ -290,7 +290,7 @@ where
let mut c = 0;
for i in 0..BENCH_RANGE_SIZE {
for j in i + 1..BENCH_RANGE_SIZE {
black_box(map.range(f(i, j)));
let _ = black_box(map.range(f(i, j)));
c += 1;
}
}
Expand Down Expand Up @@ -322,7 +322,7 @@ fn bench_iter(b: &mut Bencher, repeats: i32, size: i32) {
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
b.iter(|| {
for _ in 0..repeats {
black_box(map.iter());
let _ = black_box(map.iter());
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion library/alloc/src/collections/binary_heap.rs
Expand Up @@ -512,6 +512,7 @@ impl<T: Ord> BinaryHeap<T> {
/// let vec = heap.into_sorted_vec();
/// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
pub fn into_sorted_vec(mut self) -> Vec<T> {
let mut end = self.len();
Expand Down Expand Up @@ -850,7 +851,6 @@ impl<T> BinaryHeap<T> {
///
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
pub fn into_iter_sorted(self) -> IntoIterSorted<T> {
IntoIterSorted { inner: self }
Expand All @@ -877,6 +877,7 @@ impl<T> BinaryHeap<T> {
/// # Time complexity
///
/// Cost is *O*(1) in the worst case.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn peek(&self) -> Option<&T> {
self.data.get(0)
Expand All @@ -894,6 +895,7 @@ impl<T> BinaryHeap<T> {
/// assert!(heap.capacity() >= 100);
/// heap.push(4);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize {
self.data.capacity()
Expand Down Expand Up @@ -1203,6 +1205,7 @@ impl<T> Drop for Hole<'_, T> {
/// documentation for more.
///
/// [`iter`]: BinaryHeap::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> {
iter: slice::Iter<'a, T>,
Expand Down Expand Up @@ -1337,6 +1340,7 @@ impl<I> AsIntoIter for IntoIter<I> {
}
}

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
#[derive(Clone, Debug)]
pub struct IntoIterSorted<T> {
Expand Down
11 changes: 9 additions & 2 deletions library/alloc/src/collections/btree/map.rs
Expand Up @@ -288,6 +288,7 @@ where
/// documentation for more.
///
/// [`iter`]: BTreeMap::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, K: 'a, V: 'a> {
range: LazyLeafRange<marker::Immut<'a>, K, V>,
Expand Down Expand Up @@ -316,6 +317,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
_marker: PhantomData<&'a mut (K, V)>,
}

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "collection_debug", since = "1.17.0")]
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -359,6 +361,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
/// documentation for more.
///
/// [`keys`]: BTreeMap::keys
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
Expand All @@ -377,6 +380,7 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
/// documentation for more.
///
/// [`values`]: BTreeMap::values
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
Expand All @@ -395,6 +399,7 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
/// documentation for more.
///
/// [`values_mut`]: BTreeMap::values_mut
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_values_mut", since = "1.10.0")]
pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
Expand All @@ -413,6 +418,7 @@ impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
/// See its documentation for more.
///
/// [`into_keys`]: BTreeMap::into_keys
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoKeys<K, V> {
inner: IntoIter<K, V>,
Expand All @@ -431,6 +437,7 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
/// See its documentation for more.
///
/// [`into_values`]: BTreeMap::into_values
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoValues<K, V> {
inner: IntoIter<K, V>,
Expand All @@ -449,6 +456,7 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
/// documentation for more.
///
/// [`range`]: BTreeMap::range
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "btree_range", since = "1.17.0")]
pub struct Range<'a, K: 'a, V: 'a> {
inner: LeafRange<marker::Immut<'a>, K, V>,
Expand All @@ -467,6 +475,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Range<'_, K, V> {
/// documentation for more.
///
/// [`range_mut`]: BTreeMap::range_mut
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "btree_range", since = "1.17.0")]
pub struct RangeMut<'a, K: 'a, V: 'a> {
inner: LeafRange<marker::ValMut<'a>, K, V>,
Expand Down Expand Up @@ -1265,7 +1274,6 @@ impl<K, V> BTreeMap<K, V> {
/// assert_eq!(keys, [1, 2]);
/// ```
#[inline]
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub fn into_keys(self) -> IntoKeys<K, V> {
IntoKeys { inner: self.into_iter() }
Expand All @@ -1288,7 +1296,6 @@ impl<K, V> BTreeMap<K, V> {
/// assert_eq!(values, ["hello", "goodbye"]);
/// ```
#[inline]
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub fn into_values(self) -> IntoValues<K, V> {
IntoValues { inner: self.into_iter() }
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/collections/btree/map/entry.rs
Expand Up @@ -347,6 +347,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
/// map.entry("poneyland").or_insert(12);
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ```
#[must_use]
#[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K {
self.handle.reborrow().into_kv().0
Expand Down Expand Up @@ -391,6 +392,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
/// assert_eq!(o.get(), &12);
/// }
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> &V {
self.handle.reborrow().into_kv().1
Expand Down
26 changes: 13 additions & 13 deletions library/alloc/src/collections/btree/map/tests.rs
Expand Up @@ -744,35 +744,35 @@ fn test_range_equal_empty_cases() {
#[should_panic]
fn test_range_equal_excluded() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(2), Excluded(2)));
let _ = map.range((Excluded(2), Excluded(2)));
}

#[test]
#[should_panic]
fn test_range_backwards_1() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Included(3), Included(2)));
let _ = map.range((Included(3), Included(2)));
}

#[test]
#[should_panic]
fn test_range_backwards_2() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Included(3), Excluded(2)));
let _ = map.range((Included(3), Excluded(2)));
}

#[test]
#[should_panic]
fn test_range_backwards_3() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(3), Included(2)));
let _ = map.range((Excluded(3), Included(2)));
}

#[test]
#[should_panic]
fn test_range_backwards_4() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(3), Excluded(2)));
let _ = map.range((Excluded(3), Excluded(2)));
}

#[test]
Expand All @@ -783,7 +783,7 @@ fn test_range_finding_ill_order_in_map() {
// we cause a different panic than `test_range_backwards_1` does.
// A more refined `should_panic` would be welcome.
if Cyclic3::C < Cyclic3::A {
map.range(Cyclic3::C..=Cyclic3::A);
let _ = map.range(Cyclic3::C..=Cyclic3::A);
}
}

Expand Down Expand Up @@ -824,7 +824,7 @@ fn test_range_finding_ill_order_in_range_ord() {
}

let map = (0..12).map(|i| (CompositeKey(i, EvilTwin(i)), ())).collect::<BTreeMap<_, _>>();
map.range(EvilTwin(5)..=EvilTwin(7));
let _ = map.range(EvilTwin(5)..=EvilTwin(7));
}

#[test]
Expand Down Expand Up @@ -1239,32 +1239,32 @@ fn test_borrow() {

#[allow(dead_code)]
fn get<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get(t);
let _ = v.get(t);
}

#[allow(dead_code)]
fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.get_mut(t);
let _ = v.get_mut(t);
}

#[allow(dead_code)]
fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get_key_value(t);
let _ = v.get_key_value(t);
}

#[allow(dead_code)]
fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.contains_key(t);
let _ = v.contains_key(t);
}

#[allow(dead_code)]
fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) {
v.range(t..);
let _ = v.range(t..);
}

#[allow(dead_code)]
fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: T) {
v.range_mut(t..);
let _ = v.range_mut(t..);
}

#[allow(dead_code)]
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/collections/btree/set.rs
Expand Up @@ -92,6 +92,7 @@ impl<T: Clone> Clone for BTreeSet<T> {
/// See its documentation for more.
///
/// [`iter`]: BTreeSet::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> {
iter: Keys<'a, T, ()>,
Expand Down Expand Up @@ -123,6 +124,7 @@ pub struct IntoIter<T> {
/// See its documentation for more.
///
/// [`range`]: BTreeSet::range
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Debug)]
#[stable(feature = "btree_range", since = "1.17.0")]
pub struct Range<'a, T: 'a> {
Expand Down Expand Up @@ -668,6 +670,7 @@ impl<T> BTreeSet<T> {
/// set.insert(2);
/// assert_eq!(set.first(), Some(&1));
/// ```
#[must_use]
#[unstable(feature = "map_first_last", issue = "62924")]
pub fn first(&self) -> Option<&T>
where
Expand All @@ -694,6 +697,7 @@ impl<T> BTreeSet<T> {
/// set.insert(2);
/// assert_eq!(set.last(), Some(&2));
/// ```
#[must_use]
#[unstable(feature = "map_first_last", issue = "62924")]
pub fn last(&self) -> Option<&T>
where
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/btree/set/tests.rs
Expand Up @@ -613,8 +613,8 @@ fn test_ord_absence() {
set.is_empty();
set.len();
set.clear();
set.iter();
set.into_iter();
let _ = set.iter();
let _ = set.into_iter();
}

fn set_debug<K: Debug>(set: BTreeSet<K>) {
Expand Down

0 comments on commit fb2d0f5

Please sign in to comment.