Skip to content

Commit 58cc18c

Browse files
committed
Add #[must_use] to alloc constructors
1 parent 6928faf commit 58cc18c

File tree

12 files changed

+40
-2
lines changed

12 files changed

+40
-2
lines changed

library/alloc/src/boxed.rs

+12
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<T> Box<T> {
187187
#[cfg(not(no_global_oom_handling))]
188188
#[inline(always)]
189189
#[stable(feature = "rust1", since = "1.0.0")]
190+
#[must_use]
190191
pub fn new(x: T) -> Self {
191192
box x
192193
}
@@ -211,6 +212,7 @@ impl<T> Box<T> {
211212
/// ```
212213
#[cfg(not(no_global_oom_handling))]
213214
#[unstable(feature = "new_uninit", issue = "63291")]
215+
#[must_use]
214216
#[inline]
215217
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
216218
Self::new_uninit_in(Global)
@@ -237,6 +239,7 @@ impl<T> Box<T> {
237239
#[cfg(not(no_global_oom_handling))]
238240
#[inline]
239241
#[unstable(feature = "new_uninit", issue = "63291")]
242+
#[must_use]
240243
pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
241244
Self::new_zeroed_in(Global)
242245
}
@@ -245,6 +248,7 @@ impl<T> Box<T> {
245248
/// `x` will be pinned in memory and unable to be moved.
246249
#[cfg(not(no_global_oom_handling))]
247250
#[stable(feature = "pin", since = "1.33.0")]
251+
#[must_use]
248252
#[inline(always)]
249253
pub fn pin(x: T) -> Pin<Box<T>> {
250254
(box x).into()
@@ -339,6 +343,7 @@ impl<T, A: Allocator> Box<T, A> {
339343
/// ```
340344
#[cfg(not(no_global_oom_handling))]
341345
#[unstable(feature = "allocator_api", issue = "32838")]
346+
#[must_use]
342347
#[inline]
343348
pub fn new_in(x: T, alloc: A) -> Self {
344349
let mut boxed = Self::new_uninit_in(alloc);
@@ -395,6 +400,7 @@ impl<T, A: Allocator> Box<T, A> {
395400
/// ```
396401
#[unstable(feature = "allocator_api", issue = "32838")]
397402
#[cfg(not(no_global_oom_handling))]
403+
#[must_use]
398404
// #[unstable(feature = "new_uninit", issue = "63291")]
399405
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
400406
let layout = Layout::new::<mem::MaybeUninit<T>>();
@@ -459,6 +465,7 @@ impl<T, A: Allocator> Box<T, A> {
459465
#[unstable(feature = "allocator_api", issue = "32838")]
460466
#[cfg(not(no_global_oom_handling))]
461467
// #[unstable(feature = "new_uninit", issue = "63291")]
468+
#[must_use]
462469
pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
463470
let layout = Layout::new::<mem::MaybeUninit<T>>();
464471
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -503,6 +510,7 @@ impl<T, A: Allocator> Box<T, A> {
503510
/// `x` will be pinned in memory and unable to be moved.
504511
#[cfg(not(no_global_oom_handling))]
505512
#[unstable(feature = "allocator_api", issue = "32838")]
513+
#[must_use]
506514
#[inline(always)]
507515
pub fn pin_in(x: T, alloc: A) -> Pin<Self>
508516
where
@@ -561,6 +569,7 @@ impl<T> Box<[T]> {
561569
/// ```
562570
#[cfg(not(no_global_oom_handling))]
563571
#[unstable(feature = "new_uninit", issue = "63291")]
572+
#[must_use]
564573
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
565574
unsafe { RawVec::with_capacity(len).into_box(len) }
566575
}
@@ -585,6 +594,7 @@ impl<T> Box<[T]> {
585594
/// [zeroed]: mem::MaybeUninit::zeroed
586595
#[cfg(not(no_global_oom_handling))]
587596
#[unstable(feature = "new_uninit", issue = "63291")]
597+
#[must_use]
588598
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
589599
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
590600
}
@@ -681,6 +691,7 @@ impl<T, A: Allocator> Box<[T], A> {
681691
#[cfg(not(no_global_oom_handling))]
682692
#[unstable(feature = "allocator_api", issue = "32838")]
683693
// #[unstable(feature = "new_uninit", issue = "63291")]
694+
#[must_use]
684695
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
685696
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
686697
}
@@ -708,6 +719,7 @@ impl<T, A: Allocator> Box<[T], A> {
708719
#[cfg(not(no_global_oom_handling))]
709720
#[unstable(feature = "allocator_api", issue = "32838")]
710721
// #[unstable(feature = "new_uninit", issue = "63291")]
722+
#[must_use]
711723
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
712724
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
713725
}

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ impl<T: Ord> BinaryHeap<T> {
364364
/// heap.push(4);
365365
/// ```
366366
#[stable(feature = "rust1", since = "1.0.0")]
367+
#[must_use]
367368
pub fn new() -> BinaryHeap<T> {
368369
BinaryHeap { data: vec![] }
369370
}
@@ -383,6 +384,7 @@ impl<T: Ord> BinaryHeap<T> {
383384
/// heap.push(4);
384385
/// ```
385386
#[stable(feature = "rust1", since = "1.0.0")]
387+
#[must_use]
386388
pub fn with_capacity(capacity: usize) -> BinaryHeap<T> {
387389
BinaryHeap { data: Vec::with_capacity(capacity) }
388390
}

library/alloc/src/collections/btree/map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl<K, V> BTreeMap<K, V> {
502502
/// ```
503503
#[stable(feature = "rust1", since = "1.0.0")]
504504
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
505+
#[must_use]
505506
pub const fn new() -> BTreeMap<K, V> {
506507
BTreeMap { root: None, length: 0 }
507508
}

library/alloc/src/collections/btree/set.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<T> BTreeSet<T> {
248248
/// ```
249249
#[stable(feature = "rust1", since = "1.0.0")]
250250
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
251+
#[must_use]
251252
pub const fn new() -> BTreeSet<T> {
252253
BTreeSet { map: BTreeMap::new() }
253254
}

library/alloc/src/collections/linked_list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ impl<T> LinkedList<T> {
417417
#[inline]
418418
#[rustc_const_stable(feature = "const_linked_list_new", since = "1.32.0")]
419419
#[stable(feature = "rust1", since = "1.0.0")]
420+
#[must_use]
420421
pub const fn new() -> Self {
421422
LinkedList { head: None, tail: None, len: 0, marker: PhantomData }
422423
}

library/alloc/src/collections/vec_deque/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ impl<T> VecDeque<T> {
475475
/// ```
476476
#[inline]
477477
#[stable(feature = "rust1", since = "1.0.0")]
478+
#[must_use]
478479
pub fn new() -> VecDeque<T> {
479480
VecDeque::new_in(Global)
480481
}
@@ -490,6 +491,7 @@ impl<T> VecDeque<T> {
490491
/// ```
491492
#[inline]
492493
#[stable(feature = "rust1", since = "1.0.0")]
494+
#[must_use]
493495
pub fn with_capacity(capacity: usize) -> VecDeque<T> {
494496
Self::with_capacity_in(capacity, Global)
495497
}

library/alloc/src/raw_vec.rs

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl<T> RawVec<T, Global> {
6969
/// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
7070
/// `RawVec` with capacity `usize::MAX`. Useful for implementing
7171
/// delayed allocation.
72+
#[must_use]
7273
pub const fn new() -> Self {
7374
Self::new_in(Global)
7475
}
@@ -87,13 +88,15 @@ impl<T> RawVec<T, Global> {
8788
///
8889
/// Aborts on OOM.
8990
#[cfg(not(no_global_oom_handling))]
91+
#[must_use]
9092
#[inline]
9193
pub fn with_capacity(capacity: usize) -> Self {
9294
Self::with_capacity_in(capacity, Global)
9395
}
9496

9597
/// Like `with_capacity`, but guarantees the buffer is zeroed.
9698
#[cfg(not(no_global_oom_handling))]
99+
#[must_use]
97100
#[inline]
98101
pub fn with_capacity_zeroed(capacity: usize) -> Self {
99102
Self::with_capacity_zeroed_in(capacity, Global)

library/alloc/src/rc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ impl<T> Rc<T> {
452452
/// ```
453453
#[cfg(not(no_global_oom_handling))]
454454
#[unstable(feature = "new_uninit", issue = "63291")]
455+
#[must_use]
455456
pub fn new_uninit() -> Rc<mem::MaybeUninit<T>> {
456457
unsafe {
457458
Rc::from_ptr(Rc::allocate_for_layout(
@@ -484,6 +485,7 @@ impl<T> Rc<T> {
484485
/// [zeroed]: mem::MaybeUninit::zeroed
485486
#[cfg(not(no_global_oom_handling))]
486487
#[unstable(feature = "new_uninit", issue = "63291")]
488+
#[must_use]
487489
pub fn new_zeroed() -> Rc<mem::MaybeUninit<T>> {
488490
unsafe {
489491
Rc::from_ptr(Rc::allocate_for_layout(
@@ -587,6 +589,7 @@ impl<T> Rc<T> {
587589
/// `value` will be pinned in memory and unable to be moved.
588590
#[cfg(not(no_global_oom_handling))]
589591
#[stable(feature = "pin", since = "1.33.0")]
592+
#[must_use]
590593
pub fn pin(value: T) -> Pin<Rc<T>> {
591594
unsafe { Pin::new_unchecked(Rc::new(value)) }
592595
}
@@ -658,6 +661,7 @@ impl<T> Rc<[T]> {
658661
/// ```
659662
#[cfg(not(no_global_oom_handling))]
660663
#[unstable(feature = "new_uninit", issue = "63291")]
664+
#[must_use]
661665
pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
662666
unsafe { Rc::from_ptr(Rc::allocate_for_slice(len)) }
663667
}
@@ -684,6 +688,7 @@ impl<T> Rc<[T]> {
684688
/// [zeroed]: mem::MaybeUninit::zeroed
685689
#[cfg(not(no_global_oom_handling))]
686690
#[unstable(feature = "new_uninit", issue = "63291")]
691+
#[must_use]
687692
pub fn new_zeroed_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
688693
unsafe {
689694
Rc::from_ptr(Rc::allocate_for_layout(
@@ -2044,6 +2049,7 @@ impl<T> Weak<T> {
20442049
/// assert!(empty.upgrade().is_none());
20452050
/// ```
20462051
#[stable(feature = "downgraded_weak", since = "1.10.0")]
2052+
#[must_use]
20472053
pub fn new() -> Weak<T> {
20482054
Weak { ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0") }
20492055
}

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl String {
378378
#[inline]
379379
#[rustc_const_stable(feature = "const_string_new", since = "1.39.0")]
380380
#[stable(feature = "rust1", since = "1.0.0")]
381+
#[must_use]
381382
pub const fn new() -> String {
382383
String { vec: Vec::new() }
383384
}
@@ -422,6 +423,7 @@ impl String {
422423
#[cfg(not(no_global_oom_handling))]
423424
#[inline]
424425
#[stable(feature = "rust1", since = "1.0.0")]
426+
#[must_use]
425427
pub fn with_capacity(capacity: usize) -> String {
426428
String { vec: Vec::with_capacity(capacity) }
427429
}

library/alloc/src/sync.rs

+6
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ impl<T> Arc<T> {
448448
/// ```
449449
#[cfg(not(no_global_oom_handling))]
450450
#[unstable(feature = "new_uninit", issue = "63291")]
451+
#[must_use]
451452
pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
452453
unsafe {
453454
Arc::from_ptr(Arc::allocate_for_layout(
@@ -480,6 +481,7 @@ impl<T> Arc<T> {
480481
/// [zeroed]: mem::MaybeUninit::zeroed
481482
#[cfg(not(no_global_oom_handling))]
482483
#[unstable(feature = "new_uninit", issue = "63291")]
484+
#[must_use]
483485
pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
484486
unsafe {
485487
Arc::from_ptr(Arc::allocate_for_layout(
@@ -494,6 +496,7 @@ impl<T> Arc<T> {
494496
/// `data` will be pinned in memory and unable to be moved.
495497
#[cfg(not(no_global_oom_handling))]
496498
#[stable(feature = "pin", since = "1.33.0")]
499+
#[must_use]
497500
pub fn pin(data: T) -> Pin<Arc<T>> {
498501
unsafe { Pin::new_unchecked(Arc::new(data)) }
499502
}
@@ -662,6 +665,7 @@ impl<T> Arc<[T]> {
662665
/// ```
663666
#[cfg(not(no_global_oom_handling))]
664667
#[unstable(feature = "new_uninit", issue = "63291")]
668+
#[must_use]
665669
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
666670
unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
667671
}
@@ -688,6 +692,7 @@ impl<T> Arc<[T]> {
688692
/// [zeroed]: mem::MaybeUninit::zeroed
689693
#[cfg(not(no_global_oom_handling))]
690694
#[unstable(feature = "new_uninit", issue = "63291")]
695+
#[must_use]
691696
pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
692697
unsafe {
693698
Arc::from_ptr(Arc::allocate_for_layout(
@@ -1678,6 +1683,7 @@ impl<T> Weak<T> {
16781683
/// assert!(empty.upgrade().is_none());
16791684
/// ```
16801685
#[stable(feature = "downgraded_weak", since = "1.10.0")]
1686+
#[must_use]
16811687
pub fn new() -> Weak<T> {
16821688
Weak { ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0") }
16831689
}

library/alloc/src/vec/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ impl<T> Vec<T> {
420420
#[inline]
421421
#[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")]
422422
#[stable(feature = "rust1", since = "1.0.0")]
423+
#[must_use]
423424
pub const fn new() -> Self {
424425
Vec { buf: RawVec::NEW, len: 0 }
425426
}
@@ -464,6 +465,7 @@ impl<T> Vec<T> {
464465
#[cfg(not(no_global_oom_handling))]
465466
#[inline]
466467
#[stable(feature = "rust1", since = "1.0.0")]
468+
#[must_use]
467469
pub fn with_capacity(capacity: usize) -> Self {
468470
Self::with_capacity_in(capacity, Global)
469471
}

src/test/ui/weak-new-uninhabited-issue-48493.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
fn main() {
44
enum Void {}
5-
std::rc::Weak::<Void>::new();
6-
std::sync::Weak::<Void>::new();
5+
let _ = std::rc::Weak::<Void>::new();
6+
let _ = std::sync::Weak::<Void>::new();
77
}

0 commit comments

Comments
 (0)