Inspection checking that destructuring variable name matches the name of different component #KT-12004 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
af7de9a0c5
commit
2506bb6673
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports entries of destructuring declarations that match the name of a different property of the destructured data class.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2051,6 +2051,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.DestructuringWrongNameInspection"
|
||||
displayName="Variable in destructuring declaration uses name of a wrong data class property"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.codeInsight.daemon.impl.quickfix.RenameElementFix
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
|
||||
class DestructuringWrongNameInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitDestructuringDeclaration(destructuringDeclaration: KtDestructuringDeclaration) {
|
||||
super.visitDestructuringDeclaration(destructuringDeclaration)
|
||||
|
||||
val initializer = destructuringDeclaration.initializer ?: return
|
||||
val type = initializer.analyze().getType(initializer) ?: return
|
||||
|
||||
val classDescriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return
|
||||
|
||||
val primaryParameterNames = classDescriptor.constructors
|
||||
.firstOrNull { it.isPrimary }
|
||||
?.valueParameters
|
||||
?.map { it.name.asString() } ?: return
|
||||
|
||||
destructuringDeclaration.entries.forEachIndexed { entryIndex, entry ->
|
||||
val variableName = entry.name
|
||||
if (variableName != primaryParameterNames.getOrNull(entryIndex)) {
|
||||
for ((parameterIndex, parameterName) in primaryParameterNames.withIndex()) {
|
||||
if (parameterIndex == entryIndex) continue
|
||||
if (variableName == parameterName) {
|
||||
val fix = primaryParameterNames.getOrNull(entryIndex)?.let { RenameElementFix(entry, it) }
|
||||
holder.registerProblem(
|
||||
entry,
|
||||
"Variable name '$variableName' matches the name of a different component",
|
||||
*listOfNotNull(fix).toTypedArray())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>4</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt"/>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Variable in destructuring declaration uses name of a wrong data
|
||||
class property
|
||||
</problem_class>
|
||||
<description>Variable name 'c' matches the name of a different component</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+1
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.DestructuringWrongNameInspection
|
||||
@@ -0,0 +1,5 @@
|
||||
data class Foo(val a: String, val b: Int, val c: String)
|
||||
|
||||
fun bar(f: Foo) {
|
||||
val (a, c) = f
|
||||
}
|
||||
@@ -179,6 +179,13 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("destructuringWrongName/inspectionData/inspections.test")
|
||||
public void testDestructuringWrongName_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName =
|
||||
KotlinTestUtils.navigationMetadata("idea/testData/inspections/destructuringWrongName/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("dynamic/js/inspectionData/inspections.test")
|
||||
public void testDynamic_js_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/dynamic/js/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user