Fixes for ReplaceArraysCopyOfWithCopyOfInspection
- Reduce applicability range - Don't fix reporting level, otherwise it won't be possible to change it in settings - Using isCalling utility
This commit is contained in:
+23
-24
@@ -11,27 +11,20 @@ import com.intellij.codeInspection.ProblemHighlightType
|
|||||||
import com.intellij.codeInspection.ProblemsHolder
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||||
import org.jetbrains.kotlin.idea.intentions.calleeName
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
|
||||||
|
|
||||||
class ReplaceArraysCopyOfWithCopyOfInspection : AbstractKotlinInspection() {
|
class ReplaceArraysCopyOfWithCopyOfInspection : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
return object : KtVisitorVoid() {
|
return simpleNameExpressionVisitor { simpleNameExpression ->
|
||||||
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
|
if (simpleNameExpression.isArraysCopyOf()) {
|
||||||
super.visitDotQualifiedExpression(expression)
|
holder.registerProblem(
|
||||||
|
simpleNameExpression,
|
||||||
if (expression.isArraysCopyOf()) {
|
"Replace 'Arrays.copyOf' with 'copyOf'",
|
||||||
holder.registerProblem(
|
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
expression,
|
ReplaceArraysCopyOfWithCopyOfQuickfix()
|
||||||
"Replace 'Arrays.copyOf' with 'copyOf'",
|
)
|
||||||
ProblemHighlightType.WEAK_WARNING,
|
|
||||||
ReplaceArraysCopyOfWithCopyOfQuickfix()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,15 +36,21 @@ class ReplaceArraysCopyOfWithCopyOfQuickfix : LocalQuickFix {
|
|||||||
override fun getFamilyName() = name
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
val element = descriptor.psiElement as KtDotQualifiedExpression
|
val element = descriptor.psiElement as? KtSimpleNameExpression ?: return
|
||||||
val args = element.callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.toTypedArray() ?: return
|
val callExpression = (element.parent as? KtCallExpression) ?: return
|
||||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0.copyOf($1)", *args))
|
val qualifiedExpression = (callExpression.parent as? KtDotQualifiedExpression) ?: return
|
||||||
|
|
||||||
|
val args = callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }.toTypedArray() ?: return
|
||||||
|
if (args.size != 2) return
|
||||||
|
|
||||||
|
qualifiedExpression.replace(KtPsiFactory(element).createExpressionByPattern("$0.copyOf($1)", *args))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtDotQualifiedExpression.isArraysCopyOf(): Boolean {
|
private fun KtSimpleNameExpression.isArraysCopyOf(): Boolean {
|
||||||
if (callExpression?.valueArguments?.size != 2) return false
|
val callExpression = (parent as? KtCallExpression) ?: return false
|
||||||
if (callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.size != 2) return false
|
if (callExpression.valueArguments.size != 2) return false
|
||||||
if (calleeName != "copyOf") return false
|
if (callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }.size != 2) return false
|
||||||
return getCallableDescriptor()?.containingDeclaration?.fqNameSafe == FqName("java.util.Arrays")
|
|
||||||
|
return callExpression.isCalling(FqName("java.util.Arrays.copyOf"))
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val array = intArrayOf(1, 2, 3)
|
||||||
|
val result = java.util.Arrays.<caret>copyOf(array, 3)
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val array = intArrayOf(1, 2, 3)
|
||||||
|
val result = array.copyOf(3)
|
||||||
|
}
|
||||||
@@ -3,5 +3,5 @@ import java.util.Arrays
|
|||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
val array = intArrayOf(1, 2, 3)
|
val array = intArrayOf(1, 2, 3)
|
||||||
val result = <caret>Arrays.copyOf(array, 3)
|
val result = Arrays.<caret>copyOf(array, 3)
|
||||||
}
|
}
|
||||||
+7
@@ -5131,6 +5131,13 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@TestMetadata("qualified.kt")
|
||||||
|
public void testQualified() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simple.kt")
|
@TestMetadata("simple.kt")
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user