Add inspection for ranges with start > endInclusive #KT-18438 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
647558c98a
commit
13a2612e20
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports ranges that are empty because the 'start' value is greater than the 'endInclusive' value.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2222,6 +2222,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.EmptyRangeInspection"
|
||||
displayName="Range with start greater than endInclusive is empty"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
abstract class AbstractPrimitiveRangeToInspection : AbstractKotlinInspection() {
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitExpression(expression: KtExpression) {
|
||||
super.visitExpression(expression)
|
||||
|
||||
if (expression !is KtBinaryExpression && expression !is KtDotQualifiedExpression) return
|
||||
|
||||
val fqName = expression.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return
|
||||
if (!fqName.matches(REGEX_RANGE_TO)) return
|
||||
|
||||
visitRangeToExpression(expression, holder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun visitRangeToExpression(expression: KtExpression, holder: ProblemsHolder)
|
||||
|
||||
companion object {
|
||||
private val REGEX_RANGE_TO = """kotlin.(Char|Byte|Short|Int|Long).rangeTo""".toRegex()
|
||||
|
||||
fun KtExpression.constantValueOrNull(context: BindingContext? = null): ConstantValue<Any?>? {
|
||||
val c = context ?: this.analyze(BodyResolveMode.PARTIAL)
|
||||
|
||||
val constant = ConstantExpressionEvaluator.getConstant(this, c) ?: return null
|
||||
|
||||
return constant.toConstantValue(getType(c) ?: return null)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.getArguments
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class EmptyRangeInspection : AbstractPrimitiveRangeToInspection() {
|
||||
override fun visitRangeToExpression(expression: KtExpression, holder: ProblemsHolder) {
|
||||
val (left, right) = expression.getArguments() ?: return
|
||||
|
||||
val context = expression.analyze(BodyResolveMode.PARTIAL)
|
||||
val startValue = left?.longValueOrNull(context) ?: return
|
||||
val endValue = right?.longValueOrNull(context) ?: return
|
||||
|
||||
if (startValue <= endValue) return
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"This range is empty. Did you mean to use 'downTo'?",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
ReplaceWithDownToFix())
|
||||
}
|
||||
|
||||
class ReplaceWithDownToFix : LocalQuickFix {
|
||||
override fun getFamilyName() = "Replace with 'downTo'"
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.psiElement as? KtExpression ?: return
|
||||
val (left, right) = element.getArguments() ?: return
|
||||
if (left == null || right == null) return
|
||||
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0 downTo $1", left, right))
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.longValueOrNull(context: BindingContext): Long? {
|
||||
return (constantValueOrNull(context)?.value as? Number)?.toLong()
|
||||
}
|
||||
}
|
||||
+14
-33
@@ -21,41 +21,24 @@ import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.getArguments
|
||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
|
||||
private val REGEX_RANGE_TO = """kotlin.(Char|Byte|Short|Int|Long).rangeTo""".toRegex()
|
||||
class ReplaceRangeToWithUntilInspection : AbstractPrimitiveRangeToInspection() {
|
||||
|
||||
class ReplaceRangeToWithUntilInspection : AbstractKotlinInspection() {
|
||||
override fun visitRangeToExpression(expression: KtExpression, holder: ProblemsHolder) {
|
||||
if (expression.getArguments()?.second?.isMinusOne() != true) return
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitExpression(expression: KtExpression) {
|
||||
super.visitExpression(expression)
|
||||
|
||||
if (expression !is KtBinaryExpression && expression !is KtDotQualifiedExpression) return
|
||||
|
||||
val fqName = expression.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return
|
||||
if (!fqName.matches(REGEX_RANGE_TO)) return
|
||||
|
||||
if (expression.getArguments()?.second?.isMinusOne() != true) return
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"'rangeTo' or the '..' call can be replaced with 'until'",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
ReplaceWithUntilQuickFix()
|
||||
)
|
||||
}
|
||||
}
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"'rangeTo' or the '..' call can be replaced with 'until'",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
ReplaceWithUntilQuickFix()
|
||||
)
|
||||
}
|
||||
|
||||
class ReplaceWithUntilQuickFix : LocalQuickFix {
|
||||
@@ -80,9 +63,7 @@ class ReplaceRangeToWithUntilInspection : AbstractKotlinInspection() {
|
||||
if (this !is KtBinaryExpression) return false
|
||||
if (operationToken != KtTokens.MINUS) return false
|
||||
|
||||
val right = right as? KtConstantExpression ?: return false
|
||||
val context = right.analyze(BodyResolveMode.PARTIAL)
|
||||
val constantValue = ConstantExpressionEvaluator.getConstant(right, context)?.toConstantValue(right.getType(context) ?: return false)
|
||||
val constantValue = right?.constantValueOrNull()
|
||||
val rightValue = (constantValue?.value as? Number)?.toInt() ?: return false
|
||||
return rightValue == 1
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>6</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>7</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>8</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>9</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>10</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Range with start greater than endInclusive is empty
|
||||
</problem_class>
|
||||
<description>This range is empty. Did you mean to use 'downTo'?</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.EmptyRangeInspection
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// WITH_RUNTIME
|
||||
const val ONE = 1
|
||||
|
||||
fun foo() {
|
||||
2..1
|
||||
2.rangeTo(1)
|
||||
2..1L
|
||||
10L..-10L
|
||||
5..ONE
|
||||
10.toShort()..1.toShort()
|
||||
|
||||
//valid
|
||||
1..1
|
||||
1..10L
|
||||
1..2
|
||||
1.rangeTo(2)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.EmptyRangeInspection
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
for (i in 9<caret>..0) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
for (i in 9 downTo 0) {}
|
||||
}
|
||||
@@ -197,6 +197,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyRange/inspectionData/inspections.test")
|
||||
public void testEmptyRange_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/emptyRange/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("equalsAndHashCode/inspectionData/inspections.test")
|
||||
public void testEqualsAndHashCode_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/equalsAndHashCode/inspectionData/inspections.test");
|
||||
|
||||
@@ -63,6 +63,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/emptyRange")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class EmptyRange extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInEmptyRange() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/emptyRange"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/emptyRange/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/leakingThis")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user