Skip to content

Commit 0cf84c8

Browse files
committed
Add #[must_use] to to_value conversions
1 parent 5b21064 commit 0cf84c8

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

library/core/src/char/methods.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,11 @@ impl char {
328328
///
329329
/// ```should_panic
330330
/// // this panics
331-
/// '1'.to_digit(37);
331+
/// let _ = '1'.to_digit(37);
332332
/// ```
333333
#[stable(feature = "rust1", since = "1.0.0")]
334+
#[must_use = "this returns the result of the operation, \
335+
without modifying the original"]
334336
#[inline]
335337
pub fn to_digit(self, radix: u32) -> Option<u32> {
336338
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");

library/core/src/ptr/non_null.rs

+4
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ impl<T: ?Sized> NonNull<T> {
240240
/// The pointer can be later reconstructed with [`NonNull::from_raw_parts`].
241241
#[unstable(feature = "ptr_metadata", issue = "81513")]
242242
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
243+
#[must_use = "this returns the result of the operation, \
244+
without modifying the original"]
243245
#[inline]
244246
pub const fn to_raw_parts(self) -> (NonNull<()>, <T as super::Pointee>::Metadata) {
245247
(self.cast(), super::metadata(self.as_ptr()))
@@ -381,6 +383,8 @@ impl<T: ?Sized> NonNull<T> {
381383
/// ```
382384
#[stable(feature = "nonnull_cast", since = "1.27.0")]
383385
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.36.0")]
386+
#[must_use = "this returns the result of the operation, \
387+
without modifying the original"]
384388
#[inline]
385389
pub const fn cast<U>(self) -> NonNull<U> {
386390
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null

library/std/src/ffi/c_str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,8 @@ impl CStr {
13371337
/// assert_eq!(cstr.to_bytes(), b"foo");
13381338
/// ```
13391339
#[inline]
1340+
#[must_use = "this returns the result of the operation, \
1341+
without modifying the original"]
13401342
#[stable(feature = "rust1", since = "1.0.0")]
13411343
pub fn to_bytes(&self) -> &[u8] {
13421344
let bytes = self.to_bytes_with_nul();
@@ -1362,6 +1364,8 @@ impl CStr {
13621364
/// assert_eq!(cstr.to_bytes_with_nul(), b"foo\0");
13631365
/// ```
13641366
#[inline]
1367+
#[must_use = "this returns the result of the operation, \
1368+
without modifying the original"]
13651369
#[stable(feature = "rust1", since = "1.0.0")]
13661370
pub fn to_bytes_with_nul(&self) -> &[u8] {
13671371
unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
@@ -1432,6 +1436,8 @@ impl CStr {
14321436
/// Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
14331437
/// );
14341438
/// ```
1439+
#[must_use = "this returns the result of the operation, \
1440+
without modifying the original"]
14351441
#[stable(feature = "cstr_to_str", since = "1.4.0")]
14361442
pub fn to_string_lossy(&self) -> Cow<'_, str> {
14371443
String::from_utf8_lossy(self.to_bytes())

library/std/src/ffi/os_str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ impl OsStr {
575575
/// assert_eq!(os_str.to_str(), Some("foo"));
576576
/// ```
577577
#[stable(feature = "rust1", since = "1.0.0")]
578+
#[must_use = "this returns the result of the operation, \
579+
without modifying the original"]
578580
#[inline]
579581
pub fn to_str(&self) -> Option<&str> {
580582
self.inner.to_str()
@@ -626,6 +628,8 @@ impl OsStr {
626628
/// }
627629
/// ```
628630
#[stable(feature = "rust1", since = "1.0.0")]
631+
#[must_use = "this returns the result of the operation, \
632+
without modifying the original"]
629633
#[inline]
630634
pub fn to_string_lossy(&self) -> Cow<'_, str> {
631635
self.inner.to_string_lossy()
@@ -643,6 +647,8 @@ impl OsStr {
643647
/// assert_eq!(os_string, OsString::from("foo"));
644648
/// ```
645649
#[stable(feature = "rust1", since = "1.0.0")]
650+
#[must_use = "this returns the result of the operation, \
651+
without modifying the original"]
646652
#[inline]
647653
pub fn to_os_string(&self) -> OsString {
648654
OsString { inner: self.inner.to_owned() }

library/std/src/net/ip.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ impl IpAddr {
418418
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
419419
/// ```
420420
#[inline]
421+
#[must_use = "this returns the result of the operation, \
422+
without modifying the original"]
421423
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
422424
#[unstable(feature = "ip", issue = "27709")]
423425
pub const fn to_canonical(&self) -> IpAddr {
@@ -882,6 +884,8 @@ impl Ipv4Addr {
882884
/// ```
883885
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
884886
#[stable(feature = "rust1", since = "1.0.0")]
887+
#[must_use = "this returns the result of the operation, \
888+
without modifying the original"]
885889
#[inline]
886890
pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
887891
let [a, b, c, d] = self.octets();
@@ -907,6 +911,8 @@ impl Ipv4Addr {
907911
/// ```
908912
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
909913
#[stable(feature = "rust1", since = "1.0.0")]
914+
#[must_use = "this returns the result of the operation, \
915+
without modifying the original"]
910916
#[inline]
911917
pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
912918
let [a, b, c, d] = self.octets();
@@ -1619,6 +1625,8 @@ impl Ipv6Addr {
16191625
/// ```
16201626
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
16211627
#[unstable(feature = "ip", issue = "27709")]
1628+
#[must_use = "this returns the result of the operation, \
1629+
without modifying the original"]
16221630
#[inline]
16231631
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
16241632
match self.octets() {
@@ -1656,6 +1664,8 @@ impl Ipv6Addr {
16561664
/// ```
16571665
#[rustc_const_stable(feature = "const_ipv6", since = "1.50.0")]
16581666
#[stable(feature = "rust1", since = "1.0.0")]
1667+
#[must_use = "this returns the result of the operation, \
1668+
without modifying the original"]
16591669
#[inline]
16601670
pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
16611671
if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
@@ -1679,9 +1689,11 @@ impl Ipv6Addr {
16791689
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
16801690
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
16811691
/// ```
1682-
#[inline]
16831692
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
16841693
#[unstable(feature = "ip", issue = "27709")]
1694+
#[must_use = "this returns the result of the operation, \
1695+
without modifying the original"]
1696+
#[inline]
16851697
pub const fn to_canonical(&self) -> IpAddr {
16861698
if let Some(mapped) = self.to_ipv4_mapped() {
16871699
return IpAddr::V4(mapped);

library/std/src/path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,8 @@ impl Path {
19451945
/// assert_eq!(path.to_str(), Some("foo.txt"));
19461946
/// ```
19471947
#[stable(feature = "rust1", since = "1.0.0")]
1948+
#[must_use = "this returns the result of the operation, \
1949+
without modifying the original"]
19481950
#[inline]
19491951
pub fn to_str(&self) -> Option<&str> {
19501952
self.inner.to_str()
@@ -1971,6 +1973,8 @@ impl Path {
19711973
/// Had `path` contained invalid unicode, the `to_string_lossy` call might
19721974
/// have returned `"fo�.txt"`.
19731975
#[stable(feature = "rust1", since = "1.0.0")]
1976+
#[must_use = "this returns the result of the operation, \
1977+
without modifying the original"]
19741978
#[inline]
19751979
pub fn to_string_lossy(&self) -> Cow<'_, str> {
19761980
self.inner.to_string_lossy()
@@ -1987,6 +1991,8 @@ impl Path {
19871991
/// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
19881992
/// ```
19891993
#[rustc_conversion_suggestion]
1994+
#[must_use = "this returns the result of the operation, \
1995+
without modifying the original"]
19901996
#[stable(feature = "rust1", since = "1.0.0")]
19911997
pub fn to_path_buf(&self) -> PathBuf {
19921998
PathBuf::from(self.inner.to_os_string())

0 commit comments

Comments
 (0)