Skip to content

Commit cf2bcd1

Browse files
committed
Add #[must_use] to from_value conversions
1 parent 6928faf commit cf2bcd1

File tree

17 files changed

+50
-1
lines changed

17 files changed

+50
-1
lines changed

Diff for: library/alloc/src/str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ impl str {
595595
/// assert_eq!("☺", &*smile);
596596
/// ```
597597
#[stable(feature = "str_box_extras", since = "1.20.0")]
598+
#[must_use]
598599
#[inline]
599600
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
600601
unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }

Diff for: library/alloc/src/string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ impl String {
761761
/// assert_eq!("💖", sparkle_heart);
762762
/// ```
763763
#[inline]
764+
#[must_use]
764765
#[stable(feature = "rust1", since = "1.0.0")]
765766
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
766767
String { vec: bytes }

Diff for: library/core/src/alloc/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl Layout {
9494
/// [`Layout::from_size_align`].
9595
#[stable(feature = "alloc_layout", since = "1.28.0")]
9696
#[rustc_const_stable(feature = "alloc_layout", since = "1.36.0")]
97+
#[must_use]
9798
#[inline]
9899
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
99100
// SAFETY: the caller must ensure that `align` is greater than zero.

Diff for: library/core/src/char/convert.rs

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use super::MAX;
4848
/// assert_eq!(None, c);
4949
/// ```
5050
#[doc(alias = "chr")]
51+
#[must_use]
5152
#[inline]
5253
#[stable(feature = "rust1", since = "1.0.0")]
5354
pub fn from_u32(i: u32) -> Option<char> {
@@ -88,6 +89,7 @@ pub fn from_u32(i: u32) -> Option<char> {
8889
/// assert_eq!('❤', c);
8990
/// ```
9091
#[inline]
92+
#[must_use]
9193
#[stable(feature = "char_from_unchecked", since = "1.5.0")]
9294
pub unsafe fn from_u32_unchecked(i: u32) -> char {
9395
// SAFETY: the caller must guarantee that `i` is a valid char value.
@@ -319,6 +321,7 @@ impl fmt::Display for CharTryFromError {
319321
/// let c = char::from_digit(1, 37);
320322
/// ```
321323
#[inline]
324+
#[must_use]
322325
#[stable(feature = "rust1", since = "1.0.0")]
323326
pub fn from_digit(num: u32, radix: u32) -> Option<char> {
324327
if radix > 36 {

Diff for: library/core/src/char/methods.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl char {
136136
/// assert_eq!(None, c);
137137
/// ```
138138
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
139+
#[must_use]
139140
#[inline]
140141
pub fn from_u32(i: u32) -> Option<char> {
141142
super::convert::from_u32(i)
@@ -177,6 +178,7 @@ impl char {
177178
/// assert_eq!('❤', c);
178179
/// ```
179180
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
181+
#[must_use]
180182
#[inline]
181183
pub unsafe fn from_u32_unchecked(i: u32) -> char {
182184
// SAFETY: the safety contract must be upheld by the caller.
@@ -230,9 +232,10 @@ impl char {
230232
/// use std::char;
231233
///
232234
/// // this panics
233-
/// char::from_digit(1, 37);
235+
/// let _c = char::from_digit(1, 37);
234236
/// ```
235237
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
238+
#[must_use]
236239
#[inline]
237240
pub fn from_digit(num: u32, radix: u32) -> Option<char> {
238241
super::convert::from_digit(num, radix)

Diff for: library/core/src/num/f32.rs

+4
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ impl f32 {
786786
/// ```
787787
#[stable(feature = "float_bits_conv", since = "1.20.0")]
788788
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
789+
#[must_use]
789790
#[inline]
790791
pub const fn from_bits(v: u32) -> Self {
791792
// SAFETY: `u32` is a plain old datatype so we can always transmute from it
@@ -864,6 +865,7 @@ impl f32 {
864865
/// ```
865866
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
866867
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
868+
#[must_use]
867869
#[inline]
868870
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
869871
Self::from_bits(u32::from_be_bytes(bytes))
@@ -879,6 +881,7 @@ impl f32 {
879881
/// ```
880882
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
881883
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
884+
#[must_use]
882885
#[inline]
883886
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
884887
Self::from_bits(u32::from_le_bytes(bytes))
@@ -905,6 +908,7 @@ impl f32 {
905908
/// ```
906909
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
907910
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
911+
#[must_use]
908912
#[inline]
909913
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
910914
Self::from_bits(u32::from_ne_bytes(bytes))

Diff for: library/core/src/num/f64.rs

+4
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ impl f64 {
800800
/// ```
801801
#[stable(feature = "float_bits_conv", since = "1.20.0")]
802802
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
803+
#[must_use]
803804
#[inline]
804805
pub const fn from_bits(v: u64) -> Self {
805806
// SAFETY: `u64` is a plain old datatype so we can always transmute from it
@@ -878,6 +879,7 @@ impl f64 {
878879
/// ```
879880
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
880881
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
882+
#[must_use]
881883
#[inline]
882884
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
883885
Self::from_bits(u64::from_be_bytes(bytes))
@@ -893,6 +895,7 @@ impl f64 {
893895
/// ```
894896
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
895897
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
898+
#[must_use]
896899
#[inline]
897900
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
898901
Self::from_bits(u64::from_le_bytes(bytes))
@@ -919,6 +922,7 @@ impl f64 {
919922
/// ```
920923
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
921924
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
925+
#[must_use]
922926
#[inline]
923927
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
924928
Self::from_bits(u64::from_ne_bytes(bytes))

Diff for: library/core/src/num/int_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ macro_rules! int_impl {
282282
/// ```
283283
#[stable(feature = "rust1", since = "1.0.0")]
284284
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
285+
#[must_use]
285286
#[inline]
286287
pub const fn from_be(x: Self) -> Self {
287288
#[cfg(target_endian = "big")]
@@ -313,6 +314,7 @@ macro_rules! int_impl {
313314
/// ```
314315
#[stable(feature = "rust1", since = "1.0.0")]
315316
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
317+
#[must_use]
316318
#[inline]
317319
pub const fn from_le(x: Self) -> Self {
318320
#[cfg(target_endian = "little")]
@@ -2620,6 +2622,7 @@ macro_rules! int_impl {
26202622
/// ```
26212623
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
26222624
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2625+
#[must_use]
26232626
#[inline]
26242627
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
26252628
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2650,6 +2653,7 @@ macro_rules! int_impl {
26502653
/// ```
26512654
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
26522655
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2656+
#[must_use]
26532657
#[inline]
26542658
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
26552659
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2691,6 +2695,7 @@ macro_rules! int_impl {
26912695
/// ```
26922696
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
26932697
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2698+
#[must_use]
26942699
// SAFETY: const sound because integers are plain old datatypes so we can always
26952700
// transmute to them
26962701
#[inline]

Diff for: library/core/src/num/saturating.rs

+2
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ macro_rules! saturating_int_impl {
644644
/// }
645645
/// ```
646646
#[inline]
647+
#[must_use]
647648
#[unstable(feature = "saturating_int_impl", issue = "87920")]
648649
pub const fn from_be(x: Self) -> Self {
649650
Saturating(<$t>::from_be(x.0))
@@ -671,6 +672,7 @@ macro_rules! saturating_int_impl {
671672
/// }
672673
/// ```
673674
#[inline]
675+
#[must_use]
674676
#[unstable(feature = "saturating_int_impl", issue = "87920")]
675677
pub const fn from_le(x: Self) -> Self {
676678
Saturating(<$t>::from_le(x.0))

Diff for: library/core/src/num/uint_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ macro_rules! uint_impl {
285285
/// ```
286286
#[stable(feature = "rust1", since = "1.0.0")]
287287
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
288+
#[must_use]
288289
#[inline(always)]
289290
pub const fn from_be(x: Self) -> Self {
290291
#[cfg(target_endian = "big")]
@@ -317,6 +318,7 @@ macro_rules! uint_impl {
317318
/// ```
318319
#[stable(feature = "rust1", since = "1.0.0")]
319320
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
321+
#[must_use]
320322
#[inline(always)]
321323
pub const fn from_le(x: Self) -> Self {
322324
#[cfg(target_endian = "little")]
@@ -2278,6 +2280,7 @@ macro_rules! uint_impl {
22782280
/// ```
22792281
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
22802282
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2283+
#[must_use]
22812284
#[inline]
22822285
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
22832286
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2308,6 +2311,7 @@ macro_rules! uint_impl {
23082311
/// ```
23092312
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23102313
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2314+
#[must_use]
23112315
#[inline]
23122316
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
23132317
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2349,6 +2353,7 @@ macro_rules! uint_impl {
23492353
/// ```
23502354
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23512355
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2356+
#[must_use]
23522357
// SAFETY: const sound because integers are plain old datatypes so we can always
23532358
// transmute to them
23542359
#[inline]

Diff for: library/core/src/num/wrapping.rs

+2
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ macro_rules! wrapping_int_impl {
638638
/// }
639639
/// ```
640640
#[inline]
641+
#[must_use]
641642
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
642643
pub const fn from_be(x: Self) -> Self {
643644
Wrapping(<$t>::from_be(x.0))
@@ -665,6 +666,7 @@ macro_rules! wrapping_int_impl {
665666
/// }
666667
/// ```
667668
#[inline]
669+
#[must_use]
668670
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
669671
pub const fn from_le(x: Self) -> Self {
670672
Wrapping(<$t>::from_le(x.0))

Diff for: library/core/src/str/converts.rs

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
155155
/// assert_eq!("💖", sparkle_heart);
156156
/// ```
157157
#[inline]
158+
#[must_use]
158159
#[stable(feature = "rust1", since = "1.0.0")]
159160
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")]
160161
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
@@ -181,6 +182,7 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
181182
/// assert_eq!("💖", heart);
182183
/// ```
183184
#[inline]
185+
#[must_use]
184186
#[stable(feature = "str_mut_extras", since = "1.20.0")]
185187
pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
186188
// SAFETY: the caller must guarantee that the bytes `v`

Diff for: library/core/src/str/lossy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ pub struct Utf8Lossy {
1212
}
1313

1414
impl Utf8Lossy {
15+
#[must_use]
1516
pub fn from_str(s: &str) -> &Utf8Lossy {
1617
Utf8Lossy::from_bytes(s.as_bytes())
1718
}
1819

20+
#[must_use]
1921
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
2022
// SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
2123
unsafe { mem::transmute(bytes) }

Diff for: library/core/src/task/wake.rs

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub struct Context<'a> {
158158
impl<'a> Context<'a> {
159159
/// Create a new `Context` from a `&Waker`.
160160
#[stable(feature = "futures_api", since = "1.36.0")]
161+
#[must_use]
161162
#[inline]
162163
pub fn from_waker(waker: &'a Waker) -> Self {
163164
Context { waker, _marker: PhantomData }
@@ -251,6 +252,7 @@ impl Waker {
251252
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
252253
/// Therefore this method is unsafe.
253254
#[inline]
255+
#[must_use]
254256
#[stable(feature = "futures_api", since = "1.36.0")]
255257
pub unsafe fn from_raw(waker: RawWaker) -> Waker {
256258
Waker { waker }

Diff for: library/core/src/time.rs

+6
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ impl Duration {
203203
/// assert_eq!(0, duration.subsec_nanos());
204204
/// ```
205205
#[stable(feature = "duration", since = "1.3.0")]
206+
#[must_use]
206207
#[inline]
207208
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
208209
pub const fn from_secs(secs: u64) -> Duration {
@@ -222,6 +223,7 @@ impl Duration {
222223
/// assert_eq!(569_000_000, duration.subsec_nanos());
223224
/// ```
224225
#[stable(feature = "duration", since = "1.3.0")]
226+
#[must_use]
225227
#[inline]
226228
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
227229
pub const fn from_millis(millis: u64) -> Duration {
@@ -244,6 +246,7 @@ impl Duration {
244246
/// assert_eq!(2000, duration.subsec_nanos());
245247
/// ```
246248
#[stable(feature = "duration_from_micros", since = "1.27.0")]
249+
#[must_use]
247250
#[inline]
248251
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
249252
pub const fn from_micros(micros: u64) -> Duration {
@@ -266,6 +269,7 @@ impl Duration {
266269
/// assert_eq!(123, duration.subsec_nanos());
267270
/// ```
268271
#[stable(feature = "duration_extras", since = "1.27.0")]
272+
#[must_use]
269273
#[inline]
270274
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
271275
pub const fn from_nanos(nanos: u64) -> Duration {
@@ -692,6 +696,7 @@ impl Duration {
692696
/// assert_eq!(dur, Duration::new(2, 700_000_000));
693697
/// ```
694698
#[stable(feature = "duration_float", since = "1.38.0")]
699+
#[must_use]
695700
#[inline]
696701
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
697702
pub const fn from_secs_f64(secs: f64) -> Duration {
@@ -753,6 +758,7 @@ impl Duration {
753758
/// assert_eq!(dur, Duration::new(2, 700_000_000));
754759
/// ```
755760
#[stable(feature = "duration_float", since = "1.38.0")]
761+
#[must_use]
756762
#[inline]
757763
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
758764
pub const fn from_secs_f32(secs: f32) -> Duration {

Diff for: library/std/src/ffi/c_str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ impl CString {
425425
/// let c_string = CString::from_vec_unchecked(raw);
426426
/// }
427427
/// ```
428+
#[must_use]
428429
#[stable(feature = "rust1", since = "1.0.0")]
429430
pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
430431
v.reserve_exact(1);
@@ -476,6 +477,7 @@ impl CString {
476477
/// let c_string = CString::from_raw(raw);
477478
/// }
478479
/// ```
480+
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
479481
#[stable(feature = "cstr_memory", since = "1.4.0")]
480482
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
481483
// SAFETY: This is called with a pointer that was obtained from a call
@@ -701,6 +703,7 @@ impl CString {
701703
/// unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
702704
/// );
703705
/// ```
706+
#[must_use]
704707
#[unstable(feature = "cstring_from_vec_with_nul", issue = "73179")]
705708
pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
706709
Self { inner: v.into_boxed_slice() }
@@ -1162,6 +1165,7 @@ impl CStr {
11621165
/// }
11631166
/// # }
11641167
/// ```
1168+
#[must_use]
11651169
#[stable(feature = "rust1", since = "1.0.0")]
11661170
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
11671171
// SAFETY: The caller has provided a pointer that points to a valid C
@@ -1244,6 +1248,7 @@ impl CStr {
12441248
/// }
12451249
/// ```
12461250
#[inline]
1251+
#[must_use]
12471252
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
12481253
#[rustc_const_unstable(feature = "const_cstr_unchecked", issue = "none")]
12491254
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {

Diff for: library/std/src/io/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ impl Error {
473473
/// # }
474474
/// ```
475475
#[stable(feature = "rust1", since = "1.0.0")]
476+
#[must_use]
476477
#[inline]
477478
pub fn from_raw_os_error(code: i32) -> Error {
478479
Error { repr: Repr::Os(code) }

0 commit comments

Comments
 (0)