Skip to content

Commit d6087b9

Browse files
authored
Rollup merge of #68946 - mjbshaw:must_use, r=mjbshaw
Mark several functions and methods in core::cmp as #[must_use] These functions and methods aren't mutating functions and ignoring the result of them is likely a bug in the user's code.
2 parents 2be062a + 4ac468c commit d6087b9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/libcore/cmp.rs

+13
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl Ordering {
361361
/// assert!(data == b);
362362
/// ```
363363
#[inline]
364+
#[must_use]
364365
#[stable(feature = "rust1", since = "1.0.0")]
365366
pub fn reverse(self) -> Ordering {
366367
match self {
@@ -398,6 +399,7 @@ impl Ordering {
398399
/// assert_eq!(result, Ordering::Less);
399400
/// ```
400401
#[inline]
402+
#[must_use]
401403
#[stable(feature = "ordering_chaining", since = "1.17.0")]
402404
pub fn then(self, other: Ordering) -> Ordering {
403405
match self {
@@ -435,6 +437,7 @@ impl Ordering {
435437
/// assert_eq!(result, Ordering::Less);
436438
/// ```
437439
#[inline]
440+
#[must_use]
438441
#[stable(feature = "ordering_chaining", since = "1.17.0")]
439442
pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
440443
match self {
@@ -576,6 +579,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
576579
/// assert_eq!(10.cmp(&5), Ordering::Greater);
577580
/// assert_eq!(5.cmp(&5), Ordering::Equal);
578581
/// ```
582+
#[must_use]
579583
#[stable(feature = "rust1", since = "1.0.0")]
580584
fn cmp(&self, other: &Self) -> Ordering;
581585

@@ -591,6 +595,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
591595
/// ```
592596
#[stable(feature = "ord_max_min", since = "1.21.0")]
593597
#[inline]
598+
#[must_use]
594599
fn max(self, other: Self) -> Self
595600
where
596601
Self: Sized,
@@ -610,6 +615,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
610615
/// ```
611616
#[stable(feature = "ord_max_min", since = "1.21.0")]
612617
#[inline]
618+
#[must_use]
613619
fn min(self, other: Self) -> Self
614620
where
615621
Self: Sized,
@@ -635,6 +641,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
635641
/// assert!(0.clamp(-2, 1) == 0);
636642
/// assert!(2.clamp(-2, 1) == 1);
637643
/// ```
644+
#[must_use]
638645
#[unstable(feature = "clamp", issue = "44095")]
639646
fn clamp(self, min: Self, max: Self) -> Self
640647
where
@@ -915,6 +922,7 @@ pub macro PartialOrd($item:item) {
915922
/// assert_eq!(2, cmp::min(2, 2));
916923
/// ```
917924
#[inline]
925+
#[must_use]
918926
#[stable(feature = "rust1", since = "1.0.0")]
919927
pub fn min<T: Ord>(v1: T, v2: T) -> T {
920928
v1.min(v2)
@@ -935,6 +943,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
935943
/// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
936944
/// ```
937945
#[inline]
946+
#[must_use]
938947
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
939948
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
940949
match compare(&v1, &v2) {
@@ -958,6 +967,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
958967
/// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
959968
/// ```
960969
#[inline]
970+
#[must_use]
961971
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
962972
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
963973
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
@@ -978,6 +988,7 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
978988
/// assert_eq!(2, cmp::max(2, 2));
979989
/// ```
980990
#[inline]
991+
#[must_use]
981992
#[stable(feature = "rust1", since = "1.0.0")]
982993
pub fn max<T: Ord>(v1: T, v2: T) -> T {
983994
v1.max(v2)
@@ -998,6 +1009,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
9981009
/// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
9991010
/// ```
10001011
#[inline]
1012+
#[must_use]
10011013
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
10021014
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
10031015
match compare(&v1, &v2) {
@@ -1021,6 +1033,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
10211033
/// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
10221034
/// ```
10231035
#[inline]
1036+
#[must_use]
10241037
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
10251038
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
10261039
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))

0 commit comments

Comments
 (0)