Unnecessary variable: do not suggest in case of name duplication
So #KT-20300 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
844dd1c43c
commit
b09465ca0b
@@ -27,12 +27,13 @@ import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
|
||||
import org.jetbrains.kotlin.idea.refactoring.inline.KotlinInlineValHandler
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
|
||||
class UnnecessaryVariableInspection : AbstractKotlinInspection() {
|
||||
|
||||
@@ -73,7 +74,23 @@ class UnnecessaryVariableInspection : AbstractKotlinInspection() {
|
||||
val initializerDescriptor = context[REFERENCE_TARGET, initializer] as? VariableDescriptor ?: return false
|
||||
if (initializerDescriptor.isVar) return false
|
||||
if (initializerDescriptor.containingDeclaration !is FunctionDescriptor) return false
|
||||
return ReferencesSearch.search(property, LocalSearchScope(enclosingElement)).findFirst() != null
|
||||
|
||||
val copyName = initializerDescriptor.name.asString()
|
||||
if (ReferencesSearch.search(property, LocalSearchScope(enclosingElement)).findFirst() == null) return false
|
||||
|
||||
val containingDeclaration = property.getStrictParentOfType<KtDeclaration>()
|
||||
if (containingDeclaration != null) {
|
||||
val validator = NewDeclarationNameValidator(
|
||||
container = containingDeclaration,
|
||||
anchor = property,
|
||||
target = NewDeclarationNameValidator.Target.VARIABLES,
|
||||
excludedDeclarations = listOfNotNull(
|
||||
DescriptorToSourceUtils.descriptorToDeclaration(initializerDescriptor) as? KtDeclaration
|
||||
)
|
||||
)
|
||||
if (!validator(copyName)) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// PROBLEM: none
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(a: List<String>, b: List<Int>) {
|
||||
a.forEach {
|
||||
val <caret>a2 = it
|
||||
b.forEach {
|
||||
println(a2.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// PROBLEM: none
|
||||
|
||||
interface Callback {
|
||||
fun on(id: Int)
|
||||
}
|
||||
|
||||
fun called(id: Int) {
|
||||
val <caret>calledId = id
|
||||
object : Callback {
|
||||
override fun on(id: Int) {
|
||||
if (id == calledId) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2510,12 +2510,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("it.kt")
|
||||
public void testIt() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unnecessaryVariable/it.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("paramCopy.kt")
|
||||
public void testParamCopy() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unnecessaryVariable/paramCopy.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("parameterWithSameNameBelow.kt")
|
||||
public void testParameterWithSameNameBelow() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unnecessaryVariable/parameterWithSameNameBelow.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyCopy.kt")
|
||||
public void testPropertyCopy() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unnecessaryVariable/propertyCopy.kt");
|
||||
|
||||
Reference in New Issue
Block a user