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.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.calleeName
|
||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
|
||||
class ReplaceArraysCopyOfWithCopyOfInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
|
||||
super.visitDotQualifiedExpression(expression)
|
||||
|
||||
if (expression.isArraysCopyOf()) {
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"Replace 'Arrays.copyOf' with 'copyOf'",
|
||||
ProblemHighlightType.WEAK_WARNING,
|
||||
ReplaceArraysCopyOfWithCopyOfQuickfix()
|
||||
)
|
||||
}
|
||||
return simpleNameExpressionVisitor { simpleNameExpression ->
|
||||
if (simpleNameExpression.isArraysCopyOf()) {
|
||||
holder.registerProblem(
|
||||
simpleNameExpression,
|
||||
"Replace 'Arrays.copyOf' with 'copyOf'",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
ReplaceArraysCopyOfWithCopyOfQuickfix()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,15 +36,21 @@ class ReplaceArraysCopyOfWithCopyOfQuickfix : LocalQuickFix {
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.psiElement as KtDotQualifiedExpression
|
||||
val args = element.callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.toTypedArray() ?: return
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0.copyOf($1)", *args))
|
||||
val element = descriptor.psiElement as? KtSimpleNameExpression ?: return
|
||||
val callExpression = (element.parent as? KtCallExpression) ?: return
|
||||
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 {
|
||||
if (callExpression?.valueArguments?.size != 2) return false
|
||||
if (callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.size != 2) return false
|
||||
if (calleeName != "copyOf") return false
|
||||
return getCallableDescriptor()?.containingDeclaration?.fqNameSafe == FqName("java.util.Arrays")
|
||||
private fun KtSimpleNameExpression.isArraysCopyOf(): Boolean {
|
||||
val callExpression = (parent as? KtCallExpression) ?: return false
|
||||
if (callExpression.valueArguments.size != 2) return false
|
||||
if (callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }.size != 2) return false
|
||||
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@TestMetadata("qualified.kt")
|
||||
public void testQualified() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt");
|
||||
|
||||
Reference in New Issue
Block a user