Add inspection for equals which is called recursively within itself

So #KT-13702 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-11-09 17:24:30 +03:00
committed by Mikhail Glukhikh
parent bd348911af
commit b8fcdea673
7 changed files with 132 additions and 0 deletions
@@ -0,0 +1,20 @@
<html>
<body>
Reports recursive equals calls.
<p>
For example:
<code>
<pre>
class X {
override fun equals(other: Any?): Boolean {
if (this == other) return true // recursive equals call
return false
}
}
</pre>
</code>
</p>
</body>
</html>
+9
View File
@@ -2672,6 +2672,15 @@
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RecursiveEqualsCallInspection"
displayName="Recursive equals call"
groupPath="Kotlin"
groupName="Probable bugs"
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,75 @@
/*
* 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.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isBoolean
import org.jetbrains.kotlin.types.typeUtil.isNullableAny
class RecursiveEqualsCallInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitBinaryExpression(expr: KtBinaryExpression) {
super.visitBinaryExpression(expr)
if (expr.operationToken != KtTokens.EQEQ) return
val classDescriptor = expr.containingClass()?.descriptor ?: return
val context = expr.analyze(BodyResolveMode.PARTIAL)
if (classDescriptor != expr.getResolvedCall(context)?.dispatchReceiver?.type?.constructor?.declarationDescriptor) return
val functionDescriptor = expr.getNonStrictParentOfType<KtNamedFunction>()?.descriptor as? FunctionDescriptor ?: return
if (functionDescriptor.name.asString() != "equals" ||
!DescriptorUtils.isOverride(functionDescriptor) ||
functionDescriptor.valueParameters.singleOrNull()?.type?.isNullableAny() == false ||
functionDescriptor.returnType?.isBoolean() == false) return
holder.registerProblem(expr,
"Recursive equals call",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
ReplaceWithReferentialEqualityFix())
}
}
}
}
private class ReplaceWithReferentialEqualityFix : LocalQuickFix {
override fun getName() = "Replace with '==='"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val expr = descriptor.psiElement as? KtBinaryExpression ?: return
val left = expr.left ?: return
val right = expr.right ?: return
expr.replace(KtPsiFactory(project).createExpressionByPattern("$0 $1 $2", left, "===", right))
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RecursiveEqualsCallInspection
@@ -0,0 +1,6 @@
class Test {
override fun equals(other: Any?): Boolean {
if (<caret>this == other) return true
return false
}
}
@@ -0,0 +1,6 @@
class Test {
override fun equals(other: Any?): Boolean {
if (this === other) return true
return false
}
}
@@ -1500,6 +1500,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/recursiveEqualsCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RecursiveEqualsCall extends AbstractLocalInspectionTest {
public void testAllFilesPresentInRecursiveEqualsCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/recursiveEqualsCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("recursive.kt")
public void testRecursive() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/recursiveEqualsCall/recursive.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantExplicitType")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)