From b9d8466fc0dd2b0afcb3aa4422ebb92ab2ddb1db Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sat, 2 Feb 2019 21:24:56 +0900 Subject: [PATCH] Replace rangeTo with until: fix false negative with parenthesized expression #KT-29153 Fixed --- .../ReplaceRangeToWithUntilInspection.kt | 13 ++++++------- .../replaceRangeToWithUntil/parentheses.kt | 7 +++++++ .../replaceRangeToWithUntil/parentheses.kt.after | 7 +++++++ .../inspections/LocalInspectionTestGenerated.java | 5 +++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt create mode 100644 idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceRangeToWithUntilInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceRangeToWithUntilInspection.kt index 729718118e5..df697eaf0e5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceRangeToWithUntilInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceRangeToWithUntilInspection.kt @@ -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 } -} \ No newline at end of file +} + +private fun KtExpression.deparenthesize() = KtPsiUtil.safeDeparenthesize(this) diff --git a/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt b/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt new file mode 100644 index 00000000000..dc74607e33d --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +fun example() { + val max = 5 + for (i in 0..(max - 1)) { + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt.after b/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt.after new file mode 100644 index 00000000000..c3a5fe245b8 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceRangeToWithUntil/parentheses.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +fun example() { + val max = 5 + for (i in 0 until max) { + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 86a64622ac1..87259ca957b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -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");