From 68a4460b614bc925a12231d5bba04dd66cb78c6e Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 27 Oct 2021 15:45:31 +0200 Subject: [PATCH] replace `&` with `&&` in {integer}::checked_rem Using short-circuit operators makes it easier to perform some kinds of source code analysis, like MC/DC code coverage (a requirement in safety-critical environments). The optimized x86 assembly is the same between the old and new versions: ``` xor eax, eax test esi, esi je .LBB0_1 cmp edi, -2147483648 jne .LBB0_4 cmp esi, -1 jne .LBB0_4 ret .LBB0_1: ret .LBB0_4: mov eax, edi cdq idiv esi mov eax, 1 ret ``` --- library/core/src/num/int_macros.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 22d36d4eba3..052e1a21b32 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -661,8 +661,7 @@ macro_rules! int_impl { without modifying the original"] #[inline] pub const fn checked_rem(self, rhs: Self) -> Option { - // Using `&` helps LLVM see that it is the same check made in division. - if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) { + if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) { None } else { // SAFETY: div by zero and by INT_MIN have been checked above