Don't suggest destructuring if at least half components not used
So #KT-16828 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
318314f1a4
commit
feb9dd4d83
@@ -48,9 +48,13 @@ import java.util.*
|
|||||||
class DestructureInspection : IntentionBasedInspection<KtDeclaration>(
|
class DestructureInspection : IntentionBasedInspection<KtDeclaration>(
|
||||||
DestructureIntention::class,
|
DestructureIntention::class,
|
||||||
{ element, _ ->
|
{ element, _ ->
|
||||||
if (element is KtParameter) true
|
val usagesToRemove = DestructureIntention.collectUsagesToRemove(element)?.data
|
||||||
|
if (element is KtParameter) {
|
||||||
|
usagesToRemove != null &&
|
||||||
|
(usagesToRemove.any { it.declarationToDrop is KtDestructuringDeclaration } ||
|
||||||
|
usagesToRemove.filter { it.usagesToReplace.isNotEmpty() }.size > usagesToRemove.size / 2)
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
val usagesToRemove = DestructureIntention.collectUsagesToRemove(element)?.data
|
|
||||||
usagesToRemove?.any { it.declarationToDrop is KtDestructuringDeclaration } ?: false
|
usagesToRemove?.any { it.declarationToDrop is KtDestructuringDeclaration } ?: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,10 @@
|
|||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
<problem>
|
||||||
<file>last.kt</file>
|
<file>twoOfThree.kt</file>
|
||||||
<line>6</line>
|
<line>6</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/last.kt" />
|
<entry_point TYPE="file" FQNAME="temp:///src/twoOfThree.kt" />
|
||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
@@ -64,10 +64,10 @@
|
|||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
<problem>
|
||||||
<file>mapIndexedLast.kt</file>
|
<file>mapIndexedExceptFirst.kt</file>
|
||||||
<line>5</line>
|
<line>5</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/mapIndexedLast.kt" />
|
<entry_point TYPE="file" FQNAME="temp:///src/mapIndexedExceptFirst.kt" />
|
||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
data class Package(val name: String, val version: String, val source: String, val id: Int)
|
||||||
|
|
||||||
|
val pkgs = listOf<Package>().mapIndexed { i, <caret>p -> "${p.name} ${p.version}" to i + p.id }.toMap()
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
data class Package(val name: String, val version: String, val source: String, val id: Int)
|
||||||
|
|
||||||
|
val pkgs = listOf<Package>().mapIndexed { i, (name, version, _, id) -> "${name} ${version}" to i + id }.toMap()
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
data class My(val first: String, val second: Int, val third: Boolean)
|
||||||
|
|
||||||
|
fun foo(list: List<My>) {
|
||||||
|
list.forEach { my<caret> ->
|
||||||
|
println(my.second)
|
||||||
|
println(my.third)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
data class My(val first: String, val second: Int, val third: Boolean)
|
||||||
|
|
||||||
|
fun foo(list: List<My>) {
|
||||||
|
list.forEach { (_, second, third) ->
|
||||||
|
println(second)
|
||||||
|
println(third)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -135,14 +135,6 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
|
||||||
<file>ValueOnly.kt</file>
|
|
||||||
<line>5</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/ValueOnly.kt" />
|
|
||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
|
||||||
<description>Use destructuring declaration</description>
|
|
||||||
</problem>
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>KeyOnly.kt</file>
|
<file>KeyOnly.kt</file>
|
||||||
<line>5</line>
|
<line>5</line>
|
||||||
@@ -159,14 +151,6 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||||
<description>Use destructuring declaration</description>
|
<description>Use destructuring declaration</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
|
||||||
<file>DataClassLast.kt</file>
|
|
||||||
<line>6</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/DataClassLast.kt" />
|
|
||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
|
||||||
<description>Use destructuring declaration</description>
|
|
||||||
</problem>
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>DataClassNameConflict.kt</file>
|
<file>DataClassNameConflict.kt</file>
|
||||||
<line>7</line>
|
<line>7</line>
|
||||||
|
|||||||
@@ -7645,6 +7645,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("mapIndexedExceptFirst.kt")
|
||||||
|
public void testMapIndexedExceptFirst() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/mapIndexedExceptFirst.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("mapIndexedLast.kt")
|
@TestMetadata("mapIndexedLast.kt")
|
||||||
public void testMapIndexedLast() throws Exception {
|
public void testMapIndexedLast() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt");
|
||||||
@@ -7687,6 +7693,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("twoOfThree.kt")
|
||||||
|
public void testTwoOfThree() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/twoOfThree.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("variables.kt")
|
@TestMetadata("variables.kt")
|
||||||
public void testVariables() throws Exception {
|
public void testVariables() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/variables.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/variables.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user