Skip to content

Commit

Permalink
Fix ReturnCount debt and refactor code (#5026)
Browse files Browse the repository at this point in the history
* Fix ReturnCount debt in UnnecessaryBackticks

* Remove code duplication in NullableBooleanCheck

* Fix MultilineIfElse debt
  • Loading branch information
amitdash291 committed Jul 1, 2022
1 parent 1322deb commit ddcf77d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
Expand Up @@ -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)
Expand Down
Expand Up @@ -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<KtSimpleNameStringTemplateEntry>()
return stringTemplateEntry == null || canPlaceAfterSimpleNameEntry(stringTemplateEntry.nextSibling)
return when {
(!text.startsWith("`") || !text.endsWith("`")) -> false
(!unquoted.isIdentifier() || unquoted.isKeyword()) -> false
else -> canPlaceAfterSimpleNameEntry(
getStrictParentOfType<KtSimpleNameStringTemplateEntry>()?.nextSibling
)
}
}

private fun String.isKeyword() = this in KEYWORDS || this.all { it == '_' }
Expand Down

0 comments on commit ddcf77d

Please sign in to comment.