Redundant object type check: use referential equality, forbid for when
So #KT-22538 Fixed
This commit is contained in:
+1
-24
@@ -41,14 +41,6 @@ class RedundantObjectTypeCheckInspection : AbstractKotlinInspection() {
|
||||
registerProblem(expression.operationReference, ReplaceWithEqualityFix(expression.isNegated))
|
||||
}
|
||||
|
||||
override fun visitWhenConditionIsPattern(condition: KtWhenConditionIsPattern) {
|
||||
super.visitWhenConditionIsPattern(condition)
|
||||
val typeReference = condition.typeReference ?: return
|
||||
if (!typeReference.isObject()) return
|
||||
if (condition.isNegated) return
|
||||
registerProblem(condition, RemoveIsOperator())
|
||||
}
|
||||
|
||||
private fun registerProblem(element: KtElement, quickFix: LocalQuickFix) {
|
||||
holder.registerProblem(
|
||||
element,
|
||||
@@ -69,7 +61,7 @@ private fun KtTypeReference.isObject(): Boolean {
|
||||
private class ReplaceWithEqualityFix(isNegated: Boolean) : LocalQuickFix {
|
||||
private val isOperator = if (isNegated) "!is" else "is"
|
||||
|
||||
private val equality = if (isNegated) "!=" else "=="
|
||||
private val equality = if (isNegated) "!==" else "==="
|
||||
|
||||
override fun getName() = "Replace '$isOperator' with '$equality'"
|
||||
|
||||
@@ -83,18 +75,3 @@ private class ReplaceWithEqualityFix(isNegated: Boolean) : LocalQuickFix {
|
||||
element.replace(newElement)
|
||||
}
|
||||
}
|
||||
|
||||
private class RemoveIsOperator : LocalQuickFix {
|
||||
override fun getName() = "Remove 'is' operator"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.psiElement.getParentOfType<KtWhenConditionIsPattern>(strict = false) ?: return
|
||||
val typeReference = element.typeReference ?: return
|
||||
val factory = KtPsiFactory(project)
|
||||
val newElement = factory.createWhenCondition(typeReference.text)
|
||||
element.replace(newElement)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
object O
|
||||
|
||||
fun foo(arg: Any) {
|
||||
if (arg != O) {
|
||||
if (arg !== O) {
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
object O
|
||||
|
||||
fun foo(arg: Any) {
|
||||
if (arg == O) {
|
||||
if (arg === O) {
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
object O
|
||||
|
||||
fun foo(arg: Any) {
|
||||
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
object O
|
||||
|
||||
fun foo(arg: Any) {
|
||||
when (arg) {
|
||||
O -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user