Replace rangeTo with until: fix false negative with parenthesized expression
#KT-29153 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
c183f0285d
commit
b9d8466fc0
@@ -23,14 +23,11 @@ import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.idea.intentions.getArguments
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
class ReplaceRangeToWithUntilInspection : AbstractPrimitiveRangeToInspection() {
|
||||
override fun visitRangeToExpression(expression: KtExpression, holder: ProblemsHolder) {
|
||||
if (expression.getArguments()?.second?.isMinusOne() != true) return
|
||||
if (expression.getArguments()?.second?.deparenthesize()?.isMinusOne() != true) return
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
@@ -52,7 +49,7 @@ class ReplaceRangeToWithUntilInspection : AbstractPrimitiveRangeToInspection() {
|
||||
KtPsiFactory(element).createExpressionByPattern(
|
||||
"$0 until $1",
|
||||
args.first ?: return,
|
||||
(args.second as? KtBinaryExpression)?.left ?: return
|
||||
(args.second?.deparenthesize() as? KtBinaryExpression)?.left ?: return
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -66,4 +63,6 @@ class ReplaceRangeToWithUntilInspection : AbstractPrimitiveRangeToInspection() {
|
||||
val rightValue = (constantValue?.value as? Number)?.toInt() ?: return false
|
||||
return rightValue == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.deparenthesize() = KtPsiUtil.safeDeparenthesize(this)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun example() {
|
||||
val max = 5
|
||||
for (i in 0..<caret>(max - 1)) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun example() {
|
||||
val max = 5
|
||||
for (i in 0 until max) {
|
||||
}
|
||||
}
|
||||
+5
@@ -6255,6 +6255,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/replaceRangeToWithUntil/operatorLong.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("parentheses.kt")
|
||||
public void testParentheses() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("plusOne.kt")
|
||||
public void testPlusOne() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceRangeToWithUntil/plusOne.kt");
|
||||
|
||||
Reference in New Issue
Block a user