From ddcf77d9d4651bd117ff622180312141f900ac23 Mon Sep 17 00:00:00 2001 From: Amit Dash Date: Sat, 2 Jul 2022 01:59:08 +0530 Subject: [PATCH] Fix ReturnCount debt and refactor code (#5026) * Fix ReturnCount debt in UnnecessaryBackticks * Remove code duplication in NullableBooleanCheck * Fix MultilineIfElse debt --- .../rules/style/NullableBooleanCheck.kt | 25 +++++++------------ .../rules/style/UnnecessaryBackticks.kt | 14 +++++------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/NullableBooleanCheck.kt b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/NullableBooleanCheck.kt index 889644bad63..c70b7971250 100644 --- a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/NullableBooleanCheck.kt +++ b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/NullableBooleanCheck.kt @@ -46,23 +46,16 @@ class NullableBooleanCheck(config: Config = Config.empty) : Rule(config) { expression.right?.isBooleanConstant() == true && expression.left?.getType(bindingContext)?.isBooleanOrNullableBoolean() == true ) { - if (expression.right?.text == "true") { - report( - CodeSmell( - issue, - Entity.from(expression), - "The nullable boolean check `${expression.text}` should use `!= false` rather than `?: true`", - ) + val messageSuffix = + if (expression.right?.text == "true") "`!= false` rather than `?: true`" + else "`== true` rather than `?: false`" + report( + CodeSmell( + issue = issue, + entity = Entity.from(expression), + message = "The nullable boolean check `${expression.text}` should use $messageSuffix", ) - } else { - report( - CodeSmell( - issue, - Entity.from(expression), - "The nullable boolean check `${expression.text}` should use `== true` rather than `?: false`", - ) - ) - } + ) } super.visitBinaryExpression(expression) diff --git a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryBackticks.kt b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryBackticks.kt index 8a496aea3f4..b0463b9e074 100644 --- a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryBackticks.kt +++ b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryBackticks.kt @@ -42,16 +42,16 @@ class UnnecessaryBackticks(config: Config = Config.empty) : Rule(config) { super.visitKtElement(element) } - @Suppress("ReturnCount") private fun PsiElement.hasUnnecessaryBackticks(): Boolean { - val text = this.text - if (!text.startsWith("`") || !text.endsWith("`")) return false - val unquoted = text.drop(1).dropLast(1) - if (!unquoted.isIdentifier() || unquoted.isKeyword()) return false - val stringTemplateEntry = getStrictParentOfType() - return stringTemplateEntry == null || canPlaceAfterSimpleNameEntry(stringTemplateEntry.nextSibling) + return when { + (!text.startsWith("`") || !text.endsWith("`")) -> false + (!unquoted.isIdentifier() || unquoted.isKeyword()) -> false + else -> canPlaceAfterSimpleNameEntry( + getStrictParentOfType()?.nextSibling + ) + } } private fun String.isKeyword() = this in KEYWORDS || this.all { it == '_' }