From 5e179326d4c3bc96ab1b95c3569a651ad6b42ecc Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 13 May 2026 10:17:13 +0200 Subject: [PATCH] refactor(sonar): replace if/throw with require/error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sonar — replace hand-rolled 'throw IllegalArgumentException' / 'throw IllegalStateException' with the idiomatic 'require { msg }' / 'error(msg)'. Exception types preserved exactly (require throws IAE, error throws ISE). --- .../model/nip30CustomEmojis/OwnedEmojiPacksState.kt | 4 +--- .../com/vitorpamplona/amethyst/ui/components/WindowUtils.kt | 2 +- .../amethyst/cli/secrets/PassphraseProvider.kt | 6 +++--- .../com/vitorpamplona/amethyst/cli/secrets/SecretStore.kt | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/OwnedEmojiPacksState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/OwnedEmojiPacksState.kt index ad85b687e..720e05e7c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/OwnedEmojiPacksState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/OwnedEmojiPacksState.kt @@ -200,9 +200,7 @@ class OwnedEmojiPacksState( isPrivate: Boolean, account: Account, ) { - if (!EmojiUrlTag.isValidShortcode(emojiTag.code)) { - throw IllegalArgumentException("Invalid emoji shortcode: ${emojiTag.code}") - } + require(EmojiUrlTag.isValidShortcode(emojiTag.code)) { "Invalid emoji shortcode: ${emojiTag.code}" } val packEvent = getOwnedEmojiPackEvent(dTag) ?: return diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/WindowUtils.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/WindowUtils.kt index 07522149c..e9e27deb5 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/WindowUtils.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/WindowUtils.kt @@ -52,7 +52,7 @@ tailrec fun Context.getActivity(): ComponentActivity = when (this) { is ComponentActivity -> this is ContextWrapper -> baseContext.getActivity() - else -> throw IllegalStateException("Requires a ComponentActivity to run") + else -> error("Requires a ComponentActivity to run") } fun Context.getActivityOrNull(): ComponentActivity? = diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/PassphraseProvider.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/PassphraseProvider.kt index 81aa61439..90de4d714 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/PassphraseProvider.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/PassphraseProvider.kt @@ -42,7 +42,7 @@ class PassphraseProvider( ): String { fileFlag?.let { path -> val text = File(path).readText().trimEnd('\n', '\r') - if (text.isEmpty()) throw IllegalArgumentException("--passphrase-file $path is empty") + require(text.isNotEmpty()) { "--passphrase-file $path is empty" } return text } System.getenv(envName)?.takeIf { it.isNotEmpty() }?.let { return it } @@ -52,10 +52,10 @@ class PassphraseProvider( "No TTY and no passphrase source: set $envName or pass --passphrase-file PATH.", ) val first = console.readPassword("$prompt: ").concatToString() - if (first.isEmpty()) throw IllegalArgumentException("empty passphrase") + require(first.isNotEmpty()) { "empty passphrase" } if (confirm) { val second = console.readPassword("Confirm passphrase: ").concatToString() - if (first != second) throw IllegalArgumentException("passphrases do not match") + require(first == second) { "passphrases do not match" } } return first } diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/SecretStore.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/SecretStore.kt index 12a830daf..47bbcf942 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/SecretStore.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/secrets/SecretStore.kt @@ -91,7 +91,7 @@ class SecretStore internal constructor( when (secret.backend) { MacosKeychainBackend.BACKEND_ID -> MacosKeychainBackend SecretServiceBackend.BACKEND_ID -> SecretServiceBackend - else -> throw IllegalStateException("unknown keychain backend: ${secret.backend}") + else -> error("unknown keychain backend: ${secret.backend}") } }