Skip to content

Commit d50c54a

Browse files
authored
Rollup merge of rust-lang#130819 - bjoernager:char-must-use-len-utf, r=Noratrieb
Add `must_use` attribute to `len_utf8` and `len_utf16`. The `len_utf8` and `len_utf16` methods in `char` should have the `must_use` attribute. The somewhat similar method `<[T]>::len` has had this attribute since rust-lang#95274. Considering that these two methods would most likely be used to test the size of a buffer (before a call to `encode_utf8` or `encode_utf16`), *not* using their return values could indicate a bug. According to ["When to add `#[must_use]`](https://std-dev-guide.rust-lang.org/policy/must-use.html), this is **not** considered a breaking change (and could be reverted again at a later time).
2 parents 811ce77 + e4f89ec commit d50c54a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

core/src/char/methods.rs

+4
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ impl char {
606606
#[stable(feature = "rust1", since = "1.0.0")]
607607
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
608608
#[inline]
609+
#[must_use]
609610
pub const fn len_utf8(self) -> usize {
610611
len_utf8(self as u32)
611612
}
@@ -637,6 +638,7 @@ impl char {
637638
#[stable(feature = "rust1", since = "1.0.0")]
638639
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
639640
#[inline]
641+
#[must_use]
640642
pub const fn len_utf16(self) -> usize {
641643
len_utf16(self as u32)
642644
}
@@ -1738,6 +1740,7 @@ impl EscapeDebugExtArgs {
17381740
}
17391741

17401742
#[inline]
1743+
#[must_use]
17411744
const fn len_utf8(code: u32) -> usize {
17421745
match code {
17431746
..MAX_ONE_B => 1,
@@ -1748,6 +1751,7 @@ const fn len_utf8(code: u32) -> usize {
17481751
}
17491752

17501753
#[inline]
1754+
#[must_use]
17511755
const fn len_utf16(code: u32) -> usize {
17521756
if (code & 0xFFFF) == code { 1 } else { 2 }
17531757
}

0 commit comments

Comments
 (0)