From 81a3b7ea347b681c1fdb2a9e63dd3553a95035b3 Mon Sep 17 00:00:00 2001 From: Jason Ross Date: Sun, 5 Jul 2026 20:36:38 -0500 Subject: [PATCH] refactor(mail): early-return guard in ImapConnectionCache (#357 review) Restructure isConnectionDrop as leading guard clauses (definite-drop types, then a not-MessagingException early return) instead of a when expression, per maintainer review feedback on PR #368. Behavior is unchanged; verified by the existing ImapConnectionCacheTest suite (all 8 cases still pass), including the FolderClosedException / StoreClosedException cases that depend on the check running before the MessagingException .cause guard. Co-Authored-By: Claude Opus 4.8 --- .../kotlin/org/libremail/mail/ImapConnectionCache.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/org/libremail/mail/ImapConnectionCache.kt b/app/src/main/kotlin/org/libremail/mail/ImapConnectionCache.kt index 40e1c8b..3b2446a 100644 --- a/app/src/main/kotlin/org/libremail/mail/ImapConnectionCache.kt +++ b/app/src/main/kotlin/org/libremail/mail/ImapConnectionCache.kt @@ -207,10 +207,14 @@ internal class ImapConnectionCache( * while idle, detected on the next op's first command before any mutation is issued — is safe. A * copy-then-expunge move interrupted between its two halves is the rare exception left to that review. */ - private fun isConnectionDrop(error: Throwable): Boolean = when (error) { - is FolderClosedException, is StoreClosedException, is IOException, is ConnectionException -> true - is MessagingException -> error.cause is IOException || error.cause is ConnectionException - else -> false + private fun isConnectionDrop(error: Throwable): Boolean { + // FolderClosedException/StoreClosedException are themselves MessagingException subtypes, so these + // definite-drop checks must run before the MessagingException guard below — otherwise they'd fall + // into it and get gated on a `.cause` they don't carry, instead of the unconditional `true` below. + if (error is FolderClosedException || error is StoreClosedException) return true + if (error is IOException || error is ConnectionException) return true + if (error !is MessagingException) return false + return error.cause is IOException || error.cause is ConnectionException } private companion object {