feat: {ensure_and,logged}_debug_assert: Don't evaluate condition twice

This commit is contained in:
iequidoo
2025-07-15 09:47:41 -03:00
committed by iequidoo
parent c5c947e175
commit 8fc6ea19b4
3 changed files with 33 additions and 12 deletions

View File

@@ -368,8 +368,14 @@ impl MimeFactory {
} }
} }
ensure_and_debug_assert!(member_timestamps.len() >= to.len()); ensure_and_debug_assert!(
ensure_and_debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len()); member_timestamps.len() >= to.len(),
"member_timestamps.len() ({}) < to.len() ({})",
member_timestamps.len(), to.len());
ensure_and_debug_assert!(
member_fingerprints.is_empty() || member_fingerprints.len() >= to.len(),
"member_fingerprints.len() ({}) < to.len() ({})",
member_fingerprints.len(), to.len());
if to.len() > 1 { if to.len() > 1 {
if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) { if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
@@ -448,7 +454,11 @@ impl MimeFactory {
ensure_and_debug_assert!( ensure_and_debug_assert!(
member_timestamps.is_empty() member_timestamps.is_empty()
|| to.len() + past_members.len() == member_timestamps.len() || to.len() + past_members.len() == member_timestamps.len(),
"to.len() ({}) + past_members.len() ({}) != member_timestamps.len() ({})",
to.len(),
past_members.len(),
member_timestamps.len(),
); );
let factory = MimeFactory { let factory = MimeFactory {
@@ -671,7 +681,11 @@ impl MimeFactory {
ensure_and_debug_assert!( ensure_and_debug_assert!(
self.member_timestamps.is_empty() self.member_timestamps.is_empty()
|| to.len() + past_members.len() == self.member_timestamps.len() || to.len() + past_members.len() == self.member_timestamps.len(),
"to.len() ({}) + past_members.len() ({}) != self.member_timestamps.len() ({})",
to.len(),
past_members.len(),
self.member_timestamps.len(),
); );
if to.is_empty() { if to.is_empty() {
to.push(hidden_recipients()); to.push(hidden_recipients());

View File

@@ -1457,7 +1457,10 @@ async fn do_chat_assignment(
false => None, false => None,
}; };
if let Some(chat) = chat { if let Some(chat) = chat {
ensure_and_debug_assert!(chat.typ == Chattype::Single); ensure_and_debug_assert!(
chat.typ == Chattype::Single,
"Chat {chat_id} is not Single",
);
let mut new_protection = match verified_encryption { let mut new_protection = match verified_encryption {
VerifiedEncryption::Verified => ProtectionStatus::Protected, VerifiedEncryption::Verified => ProtectionStatus::Protected,
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected, VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
@@ -2142,7 +2145,7 @@ RETURNING id
// afterwards insert additional parts. // afterwards insert additional parts.
replace_msg_id = None; replace_msg_id = None;
ensure_and_debug_assert!(!row_id.is_special()); ensure_and_debug_assert!(!row_id.is_special(), "Rowid {row_id} is special");
created_db_entries.push(row_id); created_db_entries.push(row_id);
} }
@@ -2406,7 +2409,9 @@ async fn lookup_chat_by_reply(
// as we can directly assign the message to the chat // as we can directly assign the message to the chat
// by its group ID. // by its group ID.
ensure_and_debug_assert!( ensure_and_debug_assert!(
mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted() mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted(),
"Encrypted message has group ID {}",
mime_parser.get_chat_group_id().unwrap_or_default(),
); );
// Try to assign message to the same chat as the parent message. // Try to assign message to the same chat as the parent message.

View File

@@ -767,9 +767,10 @@ pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
/// In non-optimized builds, panics instead if so. /// In non-optimized builds, panics instead if so.
#[macro_export] #[macro_export]
macro_rules! ensure_and_debug_assert { macro_rules! ensure_and_debug_assert {
($($arg:tt)*) => { ($cond:expr, $($arg:tt)*) => {
debug_assert!($($arg)*); let cond_val = $cond;
anyhow::ensure!($($arg)*); debug_assert!(cond_val, $($arg)*);
anyhow::ensure!(cond_val, $($arg)*);
}; };
} }
@@ -806,10 +807,11 @@ macro_rules! ensure_and_debug_assert_ne {
#[macro_export] #[macro_export]
macro_rules! logged_debug_assert { macro_rules! logged_debug_assert {
($ctx:expr, $cond:expr, $($arg:tt)*) => { ($ctx:expr, $cond:expr, $($arg:tt)*) => {
if !$cond { let cond_val = $cond;
if !cond_val {
warn!($ctx, $($arg)*); warn!($ctx, $($arg)*);
} }
debug_assert!($cond, $($arg)*); debug_assert!(cond_val, $($arg)*);
}; };
} }