refactor(sonar): replace if/throw with require/error

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).
This commit is contained in:
davotoula
2026-05-13 10:17:13 +02:00
parent 6960bdf497
commit 5e179326d4
4 changed files with 6 additions and 8 deletions
@@ -200,9 +200,7 @@ class OwnedEmojiPacksState(
isPrivate: Boolean, isPrivate: Boolean,
account: Account, account: Account,
) { ) {
if (!EmojiUrlTag.isValidShortcode(emojiTag.code)) { require(EmojiUrlTag.isValidShortcode(emojiTag.code)) { "Invalid emoji shortcode: ${emojiTag.code}" }
throw IllegalArgumentException("Invalid emoji shortcode: ${emojiTag.code}")
}
val packEvent = getOwnedEmojiPackEvent(dTag) ?: return val packEvent = getOwnedEmojiPackEvent(dTag) ?: return
@@ -52,7 +52,7 @@ tailrec fun Context.getActivity(): ComponentActivity =
when (this) { when (this) {
is ComponentActivity -> this is ComponentActivity -> this
is ContextWrapper -> baseContext.getActivity() is ContextWrapper -> baseContext.getActivity()
else -> throw IllegalStateException("Requires a ComponentActivity to run") else -> error("Requires a ComponentActivity to run")
} }
fun Context.getActivityOrNull(): ComponentActivity? = fun Context.getActivityOrNull(): ComponentActivity? =
@@ -42,7 +42,7 @@ class PassphraseProvider(
): String { ): String {
fileFlag?.let { path -> fileFlag?.let { path ->
val text = File(path).readText().trimEnd('\n', '\r') 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 return text
} }
System.getenv(envName)?.takeIf { it.isNotEmpty() }?.let { return it } 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.", "No TTY and no passphrase source: set $envName or pass --passphrase-file PATH.",
) )
val first = console.readPassword("$prompt: ").concatToString() val first = console.readPassword("$prompt: ").concatToString()
if (first.isEmpty()) throw IllegalArgumentException("empty passphrase") require(first.isNotEmpty()) { "empty passphrase" }
if (confirm) { if (confirm) {
val second = console.readPassword("Confirm passphrase: ").concatToString() 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 return first
} }
@@ -91,7 +91,7 @@ class SecretStore internal constructor(
when (secret.backend) { when (secret.backend) {
MacosKeychainBackend.BACKEND_ID -> MacosKeychainBackend MacosKeychainBackend.BACKEND_ID -> MacosKeychainBackend
SecretServiceBackend.BACKEND_ID -> SecretServiceBackend SecretServiceBackend.BACKEND_ID -> SecretServiceBackend
else -> throw IllegalStateException("unknown keychain backend: ${secret.backend}") else -> error("unknown keychain backend: ${secret.backend}")
} }
} }