Skip to content

Commit 8fc6ea1

Browse files
committed
feat: {ensure_and,logged}_debug_assert: Don't evaluate condition twice
1 parent c5c947e commit 8fc6ea1

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

src/mimefactory.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,14 @@ impl MimeFactory {
368368
}
369369
}
370370

371-
ensure_and_debug_assert!(member_timestamps.len() >= to.len());
372-
ensure_and_debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
371+
ensure_and_debug_assert!(
372+
member_timestamps.len() >= to.len(),
373+
"member_timestamps.len() ({}) < to.len() ({})",
374+
member_timestamps.len(), to.len());
375+
ensure_and_debug_assert!(
376+
member_fingerprints.is_empty() || member_fingerprints.len() >= to.len(),
377+
"member_fingerprints.len() ({}) < to.len() ({})",
378+
member_fingerprints.len(), to.len());
373379

374380
if to.len() > 1 {
375381
if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
@@ -448,7 +454,11 @@ impl MimeFactory {
448454

449455
ensure_and_debug_assert!(
450456
member_timestamps.is_empty()
451-
|| to.len() + past_members.len() == member_timestamps.len()
457+
|| to.len() + past_members.len() == member_timestamps.len(),
458+
"to.len() ({}) + past_members.len() ({}) != member_timestamps.len() ({})",
459+
to.len(),
460+
past_members.len(),
461+
member_timestamps.len(),
452462
);
453463

454464
let factory = MimeFactory {
@@ -671,7 +681,11 @@ impl MimeFactory {
671681

672682
ensure_and_debug_assert!(
673683
self.member_timestamps.is_empty()
674-
|| to.len() + past_members.len() == self.member_timestamps.len()
684+
|| to.len() + past_members.len() == self.member_timestamps.len(),
685+
"to.len() ({}) + past_members.len() ({}) != self.member_timestamps.len() ({})",
686+
to.len(),
687+
past_members.len(),
688+
self.member_timestamps.len(),
675689
);
676690
if to.is_empty() {
677691
to.push(hidden_recipients());

src/receive_imf.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,10 @@ async fn do_chat_assignment(
14571457
false => None,
14581458
};
14591459
if let Some(chat) = chat {
1460-
ensure_and_debug_assert!(chat.typ == Chattype::Single);
1460+
ensure_and_debug_assert!(
1461+
chat.typ == Chattype::Single,
1462+
"Chat {chat_id} is not Single",
1463+
);
14611464
let mut new_protection = match verified_encryption {
14621465
VerifiedEncryption::Verified => ProtectionStatus::Protected,
14631466
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
@@ -2142,7 +2145,7 @@ RETURNING id
21422145
// afterwards insert additional parts.
21432146
replace_msg_id = None;
21442147

2145-
ensure_and_debug_assert!(!row_id.is_special());
2148+
ensure_and_debug_assert!(!row_id.is_special(), "Rowid {row_id} is special");
21462149
created_db_entries.push(row_id);
21472150
}
21482151

@@ -2406,7 +2409,9 @@ async fn lookup_chat_by_reply(
24062409
// as we can directly assign the message to the chat
24072410
// by its group ID.
24082411
ensure_and_debug_assert!(
2409-
mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted()
2412+
mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted(),
2413+
"Encrypted message has group ID {}",
2414+
mime_parser.get_chat_group_id().unwrap_or_default(),
24102415
);
24112416

24122417
// Try to assign message to the same chat as the parent message.

src/tools.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,10 @@ pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
767767
/// In non-optimized builds, panics instead if so.
768768
#[macro_export]
769769
macro_rules! ensure_and_debug_assert {
770-
($($arg:tt)*) => {
771-
debug_assert!($($arg)*);
772-
anyhow::ensure!($($arg)*);
770+
($cond:expr, $($arg:tt)*) => {
771+
let cond_val = $cond;
772+
debug_assert!(cond_val, $($arg)*);
773+
anyhow::ensure!(cond_val, $($arg)*);
773774
};
774775
}
775776

@@ -806,10 +807,11 @@ macro_rules! ensure_and_debug_assert_ne {
806807
#[macro_export]
807808
macro_rules! logged_debug_assert {
808809
($ctx:expr, $cond:expr, $($arg:tt)*) => {
809-
if !$cond {
810+
let cond_val = $cond;
811+
if !cond_val {
810812
warn!($ctx, $($arg)*);
811813
}
812-
debug_assert!($cond, $($arg)*);
814+
debug_assert!(cond_val, $($arg)*);
813815
};
814816
}
815817

0 commit comments

Comments
 (0)