Currently, Ord::max is defined as
fn max(self, other: Self) -> Self {
if other >= self { other } else { self }
}
I noticed, that the compiler is not able to optimize away the division with zero check in this scenario:
fn div_test(val_1: usize, val_2: usize) -> usize {
let val = core::cmp::max(val_2, 1);
val_1 / val
}
But if we change the implementation of Ord::max to
fn max(self, other: Self) -> Self {
if self < other { other } else { self }
}
the compiler is able to omit the checks for division with zero.
Link to GodBolt (Link is with -C opt-level=3, same result with -O)