Skip to content

Commit

Permalink
Simplify implementation of InvalidRange rule (#7267)
Browse files Browse the repository at this point in the history
* Simplify implementation of InvalidRange rule

* Remove getIntValueForPsiElement function
  • Loading branch information
3flex committed May 7, 2024
1 parent 38651f8 commit 92d31ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
1 change: 0 additions & 1 deletion detekt-psi-utils/api/detekt-psi-utils.api
Expand Up @@ -42,7 +42,6 @@ public final class io/gitlab/arturbosch/detekt/rules/IsPartOfUtilsKt {

public final class io/gitlab/arturbosch/detekt/rules/JunkKt {
public static final fun companionObject (Lorg/jetbrains/kotlin/psi/KtClass;)Lorg/jetbrains/kotlin/psi/KtObjectDeclaration;
public static final fun getIntValueForPsiElement (Lorg/jetbrains/kotlin/com/intellij/psi/PsiElement;)Ljava/lang/Integer;
public static final fun hasCommentInside (Lorg/jetbrains/kotlin/com/intellij/psi/PsiElement;)Z
public static final fun hasCommentInside (Lorg/jetbrains/kotlin/psi/KtClassOrObject;)Z
public static final fun isUsedForNesting (Lorg/jetbrains/kotlin/psi/KtCallExpression;)Z
Expand Down
Expand Up @@ -7,7 +7,6 @@ import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression
Expand All @@ -31,10 +30,6 @@ fun PsiElement.hasCommentInside(): Boolean {
return getUserData(commentKey) == true
}

fun getIntValueForPsiElement(element: PsiElement): Int? {
return (element as? KtConstantExpression)?.text?.toIntOrNull()
}

fun KtClass.companionObject() = this.companionObjects.singleOrNull { it.isCompanion() }

fun KtCallExpression.receiverIsUsed(context: BindingContext): Boolean =
Expand Down
Expand Up @@ -5,9 +5,8 @@ import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.rules.getIntValueForPsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtConstantExpression

/**
* Reports ranges which are empty.
Expand Down Expand Up @@ -35,38 +34,21 @@ class InvalidRange(config: Config) : Rule(
"If a for loops condition is false before the first iteration, the loop will never get executed."
) {

private val minimumSize = 3

override fun visitBinaryExpression(expression: KtBinaryExpression) {
val range = expression.children
if (range.size >= minimumSize && hasInvalidLoopRange(range)) {
report(
CodeSmell(
Entity.from(expression),
"This loop will never be executed due to its expression."
)
)
if (expression.isInvalidLoopRange()) {
report(CodeSmell(Entity.from(expression), "This loop will never be executed due to its expression."))
}
super.visitBinaryExpression(expression)
}

private fun hasInvalidLoopRange(range: Array<PsiElement>): Boolean {
val lowerValue = getIntValueForPsiElement(range[0])
val upperValue = getIntValueForPsiElement(range[2])
if (lowerValue == null || upperValue == null) {
return false
}
return when (range[1].text) {
".." -> checkRangeTo(lowerValue, upperValue)
"downTo" -> checkDownTo(lowerValue, upperValue)
"until", "..<" -> checkUntil(lowerValue, upperValue)
private fun KtBinaryExpression.isInvalidLoopRange(): Boolean {
val lower = (left as? KtConstantExpression)?.text?.toIntOrNull() ?: return false
val upper = (right as? KtConstantExpression)?.text?.toIntOrNull() ?: return false
return when (operationReference.text) {
".." -> lower > upper
"downTo" -> lower < upper
"until", "..<" -> lower >= upper
else -> false
}
}

private fun checkRangeTo(lower: Int, upper: Int) = lower > upper

private fun checkDownTo(lower: Int, upper: Int) = lower < upper

private fun checkUntil(lower: Int, upper: Int) = lower >= upper
}

0 comments on commit 92d31ed

Please sign in to comment.