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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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]

0 commit comments

Comments
 (0)