Show warning also if arrays are compared by '!=' #KT-20259 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
93252926ba
commit
f08e9832a6
@@ -1,5 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection detects '==' operator for arrays that should be replaced with 'Arrays.equals'
|
||||
This inspection detects '==' or '!=' operator for arrays that should be replaced with 'Arrays.equals'
|
||||
</body>
|
||||
</html>
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention replace '==' operator for arrays with 'Arrays.equals'
|
||||
This intention replace '==' or '!=' operator for arrays with 'Arrays.equals'
|
||||
</body>
|
||||
</html>
|
||||
+9
-2
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
|
||||
class ReplaceArrayEqualityOpWithArraysEqualsInspection :
|
||||
@@ -44,12 +45,18 @@ class ReplaceArrayEqualityOpWithArraysEqualsIntention : SelfTargetingOffsetIndep
|
||||
ktFile.resolveImportReference(FqName("java.util.Arrays")).firstOrNull()?.let {
|
||||
ImportInsertHelper.getInstance(project).importDescriptor(ktFile, it)
|
||||
}
|
||||
val expression = factory.createExpression("Arrays.equals(${left.text}, ${right.text})")
|
||||
val template = buildString {
|
||||
if (element.operationToken == KtTokens.EXCLEQ) append("!")
|
||||
append("Arrays.equals($0, $1)")
|
||||
}
|
||||
val expression = factory.createExpressionByPattern(template, left, right)
|
||||
element.replace(expression)
|
||||
}
|
||||
|
||||
override fun isApplicableTo(element: KtBinaryExpression): Boolean {
|
||||
if (element.operationToken != KtTokens.EQEQ) return false
|
||||
val operationToken = element.operationToken
|
||||
if (operationToken != KtTokens.EQEQ && operationToken != KtTokens.EXCLEQ) return false
|
||||
if (operationToken == KtTokens.EXCLEQ) text = "Replace '!=' with 'Arrays.equals'"
|
||||
val right = element.right ?: return false
|
||||
val left = element.left ?: return false
|
||||
val rightResolvedCall = right.getResolvedCall(right.analyze()) ?: return false
|
||||
|
||||
Vendored
+8
@@ -7,4 +7,12 @@
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Replace '==' with 'Arrays.equals'</problem_class>
|
||||
<description>Replace '==' with 'Arrays.equals'</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="NOT_USED_ELEMENT_ATTRIBUTES">Replace '!=' with 'Arrays.equals'</problem_class>
|
||||
<description>Replace '!=' with 'Arrays.equals'</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -4,4 +4,5 @@ fun foo() {
|
||||
val c: Any? = null
|
||||
a == b // YES
|
||||
a == c // NO
|
||||
a != b
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace '!=' with 'Arrays.equals'
|
||||
fun foo() {
|
||||
val a = arrayOf("a", "b", "c")
|
||||
val b = arrayOf("a", "b", "c")
|
||||
if (a <caret>!= b) {
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import java.util.Arrays
|
||||
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace '!=' with 'Arrays.equals'
|
||||
fun foo() {
|
||||
val a = arrayOf("a", "b", "c")
|
||||
val b = arrayOf("a", "b", "c")
|
||||
if (!Arrays.equals(a, b)) {
|
||||
}
|
||||
}
|
||||
@@ -13562,6 +13562,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("arrayEXCLEQ.kt")
|
||||
public void testArrayEXCLEQ() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceArrayEqualityOpWithArraysEquals/arrayEXCLEQ.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("primitiveArrayEQEQ.kt")
|
||||
public void testPrimitiveArrayEQEQ() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceArrayEqualityOpWithArraysEquals/primitiveArrayEQEQ.kt");
|
||||
|
||||
Reference in New Issue
Block a user