Add inspection for potentially wrongly placed unary operators
So #KT-18236 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
eaea160f0e
commit
647558c98a
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This inspection reports an unary operator followed by a dot qualifier (such as <code>-1.inc()</code>) that is potentially wrong.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2214,6 +2214,14 @@
|
|||||||
language="kotlin"
|
language="kotlin"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.WrapUnaryOperatorInspection"
|
||||||
|
displayName="Ambiguous unary operator use with number constant"
|
||||||
|
groupName="Kotlin"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WEAK WARNING"
|
||||||
|
language="kotlin"
|
||||||
|
/>
|
||||||
|
|
||||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.intellij.psi.PsiElementVisitor
|
||||||
|
import com.intellij.psi.tree.IElementType
|
||||||
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
|
class WrapUnaryOperatorInspection : AbstractKotlinInspection() {
|
||||||
|
|
||||||
|
val numberTypes = listOf(KtNodeTypes.INTEGER_CONSTANT, KtNodeTypes.FLOAT_CONSTANT)
|
||||||
|
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
|
return object : KtVisitorVoid() {
|
||||||
|
override fun visitPrefixExpression(expression: KtPrefixExpression) {
|
||||||
|
super.visitPrefixExpression(expression)
|
||||||
|
if (expression.operationToken.isUnaryMinusOrPlus()) {
|
||||||
|
val baseExpression = expression.baseExpression
|
||||||
|
if (baseExpression is KtDotQualifiedExpression) {
|
||||||
|
val receiverExpression = baseExpression.receiverExpression
|
||||||
|
if (receiverExpression is KtConstantExpression &&
|
||||||
|
receiverExpression.node.elementType in numberTypes) {
|
||||||
|
holder.registerProblem(expression,
|
||||||
|
"Wrap unary operator and value with ()",
|
||||||
|
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
|
WrapUnaryOperatorQuickfix())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun IElementType.isUnaryMinusOrPlus() = this == KtTokens.MINUS || this == KtTokens.PLUS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class WrapUnaryOperatorQuickfix : LocalQuickFix {
|
||||||
|
override fun getFamilyName() = "Wrap unary operator and value with ()"
|
||||||
|
|
||||||
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
val expression = descriptor.psiElement as? KtPrefixExpression ?: return
|
||||||
|
val dotQualifiedExpression = expression.baseExpression as? KtDotQualifiedExpression ?: return
|
||||||
|
val factory = KtPsiFactory(project)
|
||||||
|
val newReceiver = factory.createExpressionByPattern("($0$1)", expression.operationReference.text, dotQualifiedExpression.getLeftMostReceiverExpression())
|
||||||
|
val newExpression = dotQualifiedExpression.replaceFirstReceiver(factory, newReceiver)
|
||||||
|
expression.replace(newExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>6</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>9</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>10</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>12</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>14</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>22</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>simple.kt</file>
|
||||||
|
<line>23</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Ambiguous unary operator use with number constant</problem_class>
|
||||||
|
<description>Wrap unary operator and value with ()</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.WrapUnaryOperatorInspection
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
object Constants {
|
||||||
|
val ONE = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
println(-1.inc())
|
||||||
|
val i = 100
|
||||||
|
println(-i.inc())
|
||||||
|
println(-1.1.dec())
|
||||||
|
println(-1.1f.dec())
|
||||||
|
println(1 - -1.dec().dec())
|
||||||
|
println(1 - -1.javaClass)
|
||||||
|
println(1 - - 1.dec().dec())
|
||||||
|
println(1 - - 1.javaClass)
|
||||||
|
println(1-1.inc()) // NG
|
||||||
|
println(1 - 1.dec().dec()) // NG
|
||||||
|
println(1 - 1.javaClass) // NG
|
||||||
|
println(-1) //NG
|
||||||
|
println(!true) // NG
|
||||||
|
println(!true.not()) // NG
|
||||||
|
println(+1) // NG
|
||||||
|
println(+1.inc())
|
||||||
|
println(+3.14.inc())
|
||||||
|
|
||||||
|
println(-Constants.ONE) // NG
|
||||||
|
println(-"2".toInt()) // NG
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.inspections.WrapUnaryOperatorInspection
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun Int.inc() = this + 1
|
||||||
|
|
||||||
|
val x = <caret>-1.inc()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun Int.inc() = this + 1
|
||||||
|
|
||||||
|
val x = (-1).inc()
|
||||||
@@ -400,5 +400,11 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/inspections.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/inspections.test");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("wrapUnaryOperator/inspectionData/inspections.test")
|
||||||
|
public void testWrapUnaryOperator_inspectionData_Inspections_test() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/wrapUnaryOperator/inspectionData/inspections.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -320,4 +320,19 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/inspectionsLocal/wrapUnaryOperator")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class WrapUnaryOperator extends AbstractLocalInspectionTest {
|
||||||
|
public void testAllFilesPresentInWrapUnaryOperator() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/wrapUnaryOperator"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/wrapUnaryOperator/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user