Skip to content

Commit ca97aaa

Browse files
authored
Rollup merge of rust-lang#89794 - jkugelman:must-use-to_value-conversions, r=joshtriplett
Add #[must_use] to to_value conversions `NonNull<T>::cast` snuck in when I wasn't looking. What a scamp! Parent issue: rust-lang#89692 r? ``@joshtriplett``
2 parents 3164052 + 0cf84c8 commit ca97aaa

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
@@ -242,6 +242,8 @@ impl<T: ?Sized> NonNull<T> {
242242
/// The pointer can be later reconstructed with [`NonNull::from_raw_parts`].
243243
#[unstable(feature = "ptr_metadata", issue = "81513")]
244244
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
245+
#[must_use = "this returns the result of the operation, \
246+
without modifying the original"]
245247
#[inline]
246248
pub const fn to_raw_parts(self) -> (NonNull<()>, <T as super::Pointee>::Metadata) {
247249
(self.cast(), super::metadata(self.as_ptr()))
@@ -386,6 +388,8 @@ impl<T: ?Sized> NonNull<T> {
386388
/// ```
387389
#[stable(feature = "nonnull_cast", since = "1.27.0")]
388390
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.36.0")]
391+
#[must_use = "this returns the result of the operation, \
392+
without modifying the original"]
389393
#[inline]
390394
pub const fn cast<U>(self) -> NonNull<U> {
391395
// 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
@@ -1342,6 +1342,8 @@ impl CStr {
13421342
/// assert_eq!(cstr.to_bytes(), b"foo");
13431343
/// ```
13441344
#[inline]
1345+
#[must_use = "this returns the result of the operation, \
1346+
without modifying the original"]
13451347
#[stable(feature = "rust1", since = "1.0.0")]
13461348
pub fn to_bytes(&self) -> &[u8] {
13471349
let bytes = self.to_bytes_with_nul();
@@ -1367,6 +1369,8 @@ impl CStr {
13671369
/// assert_eq!(cstr.to_bytes_with_nul(), b"foo\0");
13681370
/// ```
13691371
#[inline]
1372+
#[must_use = "this returns the result of the operation, \
1373+
without modifying the original"]
13701374
#[stable(feature = "rust1", since = "1.0.0")]
13711375
pub fn to_bytes_with_nul(&self) -> &[u8] {
13721376
unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
@@ -1437,6 +1441,8 @@ impl CStr {
14371441
/// Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
14381442
/// );
14391443
/// ```
1444+
#[must_use = "this returns the result of the operation, \
1445+
without modifying the original"]
14401446
#[stable(feature = "cstr_to_str", since = "1.4.0")]
14411447
pub fn to_string_lossy(&self) -> Cow<'_, str> {
14421448
String::from_utf8_lossy(self.to_bytes())

library/std/src/ffi/os_str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ impl OsStr {
576576
/// assert_eq!(os_str.to_str(), Some("foo"));
577577
/// ```
578578
#[stable(feature = "rust1", since = "1.0.0")]
579+
#[must_use = "this returns the result of the operation, \
580+
without modifying the original"]
579581
#[inline]
580582
pub fn to_str(&self) -> Option<&str> {
581583
self.inner.to_str()
@@ -627,6 +629,8 @@ impl OsStr {
627629
/// }
628630
/// ```
629631
#[stable(feature = "rust1", since = "1.0.0")]
632+
#[must_use = "this returns the result of the operation, \
633+
without modifying the original"]
630634
#[inline]
631635
pub fn to_string_lossy(&self) -> Cow<'_, str> {
632636
self.inner.to_string_lossy()
@@ -644,6 +648,8 @@ impl OsStr {
644648
/// assert_eq!(os_string, OsString::from("foo"));
645649
/// ```
646650
#[stable(feature = "rust1", since = "1.0.0")]
651+
#[must_use = "this returns the result of the operation, \
652+
without modifying the original"]
647653
#[inline]
648654
pub fn to_os_string(&self) -> OsString {
649655
OsString { inner: self.inner.to_owned() }

library/std/src/net/ip.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ impl IpAddr {
426426
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
427427
/// ```
428428
#[inline]
429+
#[must_use = "this returns the result of the operation, \
430+
without modifying the original"]
429431
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
430432
#[unstable(feature = "ip", issue = "27709")]
431433
pub const fn to_canonical(&self) -> IpAddr {
@@ -901,6 +903,8 @@ impl Ipv4Addr {
901903
/// ```
902904
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
903905
#[stable(feature = "rust1", since = "1.0.0")]
906+
#[must_use = "this returns the result of the operation, \
907+
without modifying the original"]
904908
#[inline]
905909
pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
906910
let [a, b, c, d] = self.octets();
@@ -926,6 +930,8 @@ impl Ipv4Addr {
926930
/// ```
927931
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
928932
#[stable(feature = "rust1", since = "1.0.0")]
933+
#[must_use = "this returns the result of the operation, \
934+
without modifying the original"]
929935
#[inline]
930936
pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
931937
let [a, b, c, d] = self.octets();
@@ -1648,6 +1654,8 @@ impl Ipv6Addr {
16481654
/// ```
16491655
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
16501656
#[unstable(feature = "ip", issue = "27709")]
1657+
#[must_use = "this returns the result of the operation, \
1658+
without modifying the original"]
16511659
#[inline]
16521660
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
16531661
match self.octets() {
@@ -1685,6 +1693,8 @@ impl Ipv6Addr {
16851693
/// ```
16861694
#[rustc_const_stable(feature = "const_ipv6", since = "1.50.0")]
16871695
#[stable(feature = "rust1", since = "1.0.0")]
1696+
#[must_use = "this returns the result of the operation, \
1697+
without modifying the original"]
16881698
#[inline]
16891699
pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
16901700
if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
@@ -1708,9 +1718,11 @@ impl Ipv6Addr {
17081718
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
17091719
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
17101720
/// ```
1711-
#[inline]
17121721
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
17131722
#[unstable(feature = "ip", issue = "27709")]
1723+
#[must_use = "this returns the result of the operation, \
1724+
without modifying the original"]
1725+
#[inline]
17141726
pub const fn to_canonical(&self) -> IpAddr {
17151727
if let Some(mapped) = self.to_ipv4_mapped() {
17161728
return IpAddr::V4(mapped);

library/std/src/path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,8 @@ impl Path {
19521952
/// assert_eq!(path.to_str(), Some("foo.txt"));
19531953
/// ```
19541954
#[stable(feature = "rust1", since = "1.0.0")]
1955+
#[must_use = "this returns the result of the operation, \
1956+
without modifying the original"]
19551957
#[inline]
19561958
pub fn to_str(&self) -> Option<&str> {
19571959
self.inner.to_str()
@@ -1978,6 +1980,8 @@ impl Path {
19781980
/// Had `path` contained invalid unicode, the `to_string_lossy` call might
19791981
/// have returned `"fo�.txt"`.
19801982
#[stable(feature = "rust1", since = "1.0.0")]
1983+
#[must_use = "this returns the result of the operation, \
1984+
without modifying the original"]
19811985
#[inline]
19821986
pub fn to_string_lossy(&self) -> Cow<'_, str> {
19831987
self.inner.to_string_lossy()
@@ -1994,6 +1998,8 @@ impl Path {
19941998
/// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
19951999
/// ```
19962000
#[rustc_conversion_suggestion]
2001+
#[must_use = "this returns the result of the operation, \
2002+
without modifying the original"]
19972003
#[stable(feature = "rust1", since = "1.0.0")]
19982004
pub fn to_path_buf(&self) -> PathBuf {
19992005
PathBuf::from(self.inner.to_os_string())

0 commit comments

Comments
 (0)