Skip to content

Commit 96ffc74

Browse files
Rollup merge of #89753 - jkugelman:must-use-from_value-conversions, r=joshtriplett
Add #[must_use] to from_value conversions I added two methods to the list myself. Clippy did not flag them because they take `mut` args, but neither modifies their argument. ```rust core::str const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str; std::ffi::CString unsafe fn from_raw(ptr: *mut c_char) -> CString; ``` I put a custom note on `from_raw`: ```rust #[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"] pub unsafe fn from_raw(ptr: *mut c_char) -> CString { ``` Parent issue: #89692 r? ``@joshtriplett``
2 parents 9183942 + cf2bcd1 commit 96ffc74

File tree

17 files changed

+50
-1
lines changed

17 files changed

+50
-1
lines changed

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) }

library/alloc/src/string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ impl String {
764764
/// assert_eq!("💖", sparkle_heart);
765765
/// ```
766766
#[inline]
767+
#[must_use]
767768
#[stable(feature = "rust1", since = "1.0.0")]
768769
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
769770
String { vec: bytes }

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.

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 {

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)

library/core/src/num/f32.rs

+4
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ impl f32 {
801801
/// ```
802802
#[stable(feature = "float_bits_conv", since = "1.20.0")]
803803
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
804+
#[must_use]
804805
#[inline]
805806
pub const fn from_bits(v: u32) -> Self {
806807
// SAFETY: `u32` is a plain old datatype so we can always transmute from it
@@ -885,6 +886,7 @@ impl f32 {
885886
/// ```
886887
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
887888
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
889+
#[must_use]
888890
#[inline]
889891
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
890892
Self::from_bits(u32::from_be_bytes(bytes))
@@ -900,6 +902,7 @@ impl f32 {
900902
/// ```
901903
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
902904
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
905+
#[must_use]
903906
#[inline]
904907
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
905908
Self::from_bits(u32::from_le_bytes(bytes))
@@ -926,6 +929,7 @@ impl f32 {
926929
/// ```
927930
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
928931
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
932+
#[must_use]
929933
#[inline]
930934
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
931935
Self::from_bits(u32::from_ne_bytes(bytes))

library/core/src/num/f64.rs

+4
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ impl f64 {
817817
/// ```
818818
#[stable(feature = "float_bits_conv", since = "1.20.0")]
819819
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
820+
#[must_use]
820821
#[inline]
821822
pub const fn from_bits(v: u64) -> Self {
822823
// SAFETY: `u64` is a plain old datatype so we can always transmute from it
@@ -901,6 +902,7 @@ impl f64 {
901902
/// ```
902903
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
903904
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
905+
#[must_use]
904906
#[inline]
905907
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
906908
Self::from_bits(u64::from_be_bytes(bytes))
@@ -916,6 +918,7 @@ impl f64 {
916918
/// ```
917919
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
918920
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
921+
#[must_use]
919922
#[inline]
920923
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
921924
Self::from_bits(u64::from_le_bytes(bytes))
@@ -942,6 +945,7 @@ impl f64 {
942945
/// ```
943946
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
944947
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
948+
#[must_use]
945949
#[inline]
946950
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
947951
Self::from_bits(u64::from_ne_bytes(bytes))

library/core/src/num/int_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ macro_rules! int_impl {
297297
/// ```
298298
#[stable(feature = "rust1", since = "1.0.0")]
299299
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
300+
#[must_use]
300301
#[inline]
301302
pub const fn from_be(x: Self) -> Self {
302303
#[cfg(target_endian = "big")]
@@ -328,6 +329,7 @@ macro_rules! int_impl {
328329
/// ```
329330
#[stable(feature = "rust1", since = "1.0.0")]
330331
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
332+
#[must_use]
331333
#[inline]
332334
pub const fn from_le(x: Self) -> Self {
333335
#[cfg(target_endian = "little")]
@@ -2671,6 +2673,7 @@ macro_rules! int_impl {
26712673
/// ```
26722674
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
26732675
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2676+
#[must_use]
26742677
#[inline]
26752678
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
26762679
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2701,6 +2704,7 @@ macro_rules! int_impl {
27012704
/// ```
27022705
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
27032706
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2707+
#[must_use]
27042708
#[inline]
27052709
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
27062710
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2742,6 +2746,7 @@ macro_rules! int_impl {
27422746
/// ```
27432747
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
27442748
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2749+
#[must_use]
27452750
// SAFETY: const sound because integers are plain old datatypes so we can always
27462751
// transmute to them
27472752
#[inline]

library/core/src/num/saturating.rs

+2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ macro_rules! saturating_int_impl {
657657
/// }
658658
/// ```
659659
#[inline]
660+
#[must_use]
660661
#[unstable(feature = "saturating_int_impl", issue = "87920")]
661662
pub const fn from_be(x: Self) -> Self {
662663
Saturating(<$t>::from_be(x.0))
@@ -684,6 +685,7 @@ macro_rules! saturating_int_impl {
684685
/// }
685686
/// ```
686687
#[inline]
688+
#[must_use]
687689
#[unstable(feature = "saturating_int_impl", issue = "87920")]
688690
pub const fn from_le(x: Self) -> Self {
689691
Saturating(<$t>::from_le(x.0))

library/core/src/num/uint_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ macro_rules! uint_impl {
300300
/// ```
301301
#[stable(feature = "rust1", since = "1.0.0")]
302302
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
303+
#[must_use]
303304
#[inline(always)]
304305
pub const fn from_be(x: Self) -> Self {
305306
#[cfg(target_endian = "big")]
@@ -332,6 +333,7 @@ macro_rules! uint_impl {
332333
/// ```
333334
#[stable(feature = "rust1", since = "1.0.0")]
334335
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
336+
#[must_use]
335337
#[inline(always)]
336338
pub const fn from_le(x: Self) -> Self {
337339
#[cfg(target_endian = "little")]
@@ -2321,6 +2323,7 @@ macro_rules! uint_impl {
23212323
/// ```
23222324
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23232325
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2326+
#[must_use]
23242327
#[inline]
23252328
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
23262329
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2351,6 +2354,7 @@ macro_rules! uint_impl {
23512354
/// ```
23522355
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23532356
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2357+
#[must_use]
23542358
#[inline]
23552359
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
23562360
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2392,6 +2396,7 @@ macro_rules! uint_impl {
23922396
/// ```
23932397
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23942398
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2399+
#[must_use]
23952400
// SAFETY: const sound because integers are plain old datatypes so we can always
23962401
// transmute to them
23972402
#[inline]

library/core/src/num/wrapping.rs

+2
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ macro_rules! wrapping_int_impl {
651651
/// }
652652
/// ```
653653
#[inline]
654+
#[must_use]
654655
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
655656
pub const fn from_be(x: Self) -> Self {
656657
Wrapping(<$t>::from_be(x.0))
@@ -678,6 +679,7 @@ macro_rules! wrapping_int_impl {
678679
/// }
679680
/// ```
680681
#[inline]
682+
#[must_use]
681683
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
682684
pub const fn from_le(x: Self) -> Self {
683685
Wrapping(<$t>::from_le(x.0))

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`

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) }

library/core/src/task/wake.rs

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

library/core/src/time.rs

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl Duration {
204204
/// assert_eq!(0, duration.subsec_nanos());
205205
/// ```
206206
#[stable(feature = "duration", since = "1.3.0")]
207+
#[must_use]
207208
#[inline]
208209
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
209210
pub const fn from_secs(secs: u64) -> Duration {
@@ -223,6 +224,7 @@ impl Duration {
223224
/// assert_eq!(569_000_000, duration.subsec_nanos());
224225
/// ```
225226
#[stable(feature = "duration", since = "1.3.0")]
227+
#[must_use]
226228
#[inline]
227229
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
228230
pub const fn from_millis(millis: u64) -> Duration {
@@ -245,6 +247,7 @@ impl Duration {
245247
/// assert_eq!(2000, duration.subsec_nanos());
246248
/// ```
247249
#[stable(feature = "duration_from_micros", since = "1.27.0")]
250+
#[must_use]
248251
#[inline]
249252
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
250253
pub const fn from_micros(micros: u64) -> Duration {
@@ -267,6 +270,7 @@ impl Duration {
267270
/// assert_eq!(123, duration.subsec_nanos());
268271
/// ```
269272
#[stable(feature = "duration_extras", since = "1.27.0")]
273+
#[must_use]
270274
#[inline]
271275
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
272276
pub const fn from_nanos(nanos: u64) -> Duration {
@@ -708,6 +712,7 @@ impl Duration {
708712
/// assert_eq!(dur, Duration::new(2, 700_000_000));
709713
/// ```
710714
#[stable(feature = "duration_float", since = "1.38.0")]
715+
#[must_use]
711716
#[inline]
712717
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
713718
pub const fn from_secs_f64(secs: f64) -> Duration {
@@ -769,6 +774,7 @@ impl Duration {
769774
/// assert_eq!(dur, Duration::new(2, 700_000_000));
770775
/// ```
771776
#[stable(feature = "duration_float", since = "1.38.0")]
777+
#[must_use]
772778
#[inline]
773779
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
774780
pub const fn from_secs_f32(secs: f32) -> Duration {

library/std/src/ffi/c_str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ impl CString {
426426
/// let c_string = CString::from_vec_unchecked(raw);
427427
/// }
428428
/// ```
429+
#[must_use]
429430
#[stable(feature = "rust1", since = "1.0.0")]
430431
pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
431432
v.reserve_exact(1);
@@ -477,6 +478,7 @@ impl CString {
477478
/// let c_string = CString::from_raw(raw);
478479
/// }
479480
/// ```
481+
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
480482
#[stable(feature = "cstr_memory", since = "1.4.0")]
481483
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
482484
// SAFETY: This is called with a pointer that was obtained from a call
@@ -705,6 +707,7 @@ impl CString {
705707
/// unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
706708
/// );
707709
/// ```
710+
#[must_use]
708711
#[unstable(feature = "cstring_from_vec_with_nul", issue = "73179")]
709712
pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
710713
Self { inner: v.into_boxed_slice() }
@@ -1168,6 +1171,7 @@ impl CStr {
11681171
/// }
11691172
/// # }
11701173
/// ```
1174+
#[must_use]
11711175
#[stable(feature = "rust1", since = "1.0.0")]
11721176
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
11731177
// SAFETY: The caller has provided a pointer that points to a valid C
@@ -1250,6 +1254,7 @@ impl CStr {
12501254
/// }
12511255
/// ```
12521256
#[inline]
1257+
#[must_use]
12531258
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
12541259
#[rustc_const_unstable(feature = "const_cstr_unchecked", issue = "none")]
12551260
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {

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)