KT-16828: use _ when appropriate in destructure intention
This commit is contained in:
committed by
Mikhail Glukhikh
parent
32d0d7a4d5
commit
8a02ce3dc2
@@ -68,8 +68,15 @@ class DestructureIntention : SelfTargetingRangeIntention<KtDeclaration>(
|
||||
excludedDeclarations = usagesToRemove.map { listOfNotNull(it.declarationToDrop) }.flatten()
|
||||
)
|
||||
val names = ArrayList<String>()
|
||||
val underscoreSupported = element.languageVersionSettings.supportsFeature(LanguageFeature.SingleUnderscoreForParameterName)
|
||||
usagesToRemove.forEach { (descriptor, usagesToReplace, variableToDrop, name) ->
|
||||
val suggestedName = name ?: KotlinNameSuggester.suggestNameByName(descriptor.name.asString(), validator)
|
||||
val suggestedName =
|
||||
if (usagesToReplace.isEmpty() && variableToDrop == null && underscoreSupported) {
|
||||
"_"
|
||||
}
|
||||
else {
|
||||
name ?: KotlinNameSuggester.suggestNameByName(descriptor.name.asString(), validator)
|
||||
}
|
||||
variableToDrop?.delete()
|
||||
usagesToReplace.forEach {
|
||||
it.replace(factory.createExpression(suggestedName))
|
||||
|
||||
@@ -63,4 +63,12 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||
<description>Use destructuring declaration</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>mapIndexedLast.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/mapIndexedLast.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Use destructuring declaration</problem_class>
|
||||
<description>Use destructuring declaration</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -3,7 +3,7 @@
|
||||
data class My(val first: String, val second: Int)
|
||||
|
||||
fun foo(list: List<My>) {
|
||||
list.forEach { (first, second) ->
|
||||
list.forEach { (_, second) ->
|
||||
println(second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
data class Package(val name: String, val version: String, val source: String, val id: String)
|
||||
|
||||
val pkgs = listOf<Package>().mapIndexed { i, <caret>p -> p.id to i }.toMap()
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
data class Package(val name: String, val version: String, val source: String, val id: String)
|
||||
|
||||
val pkgs = listOf<Package>().mapIndexed { i, (_, _, _, id) -> id to i }.toMap()
|
||||
@@ -3,7 +3,7 @@
|
||||
data class My(val first: String, val second: Int)
|
||||
|
||||
fun foo(list: List<My>) {
|
||||
for ((first, second) in list) {
|
||||
for ((_, second) in list) {
|
||||
println(second)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val list = listOf(MyClass(1, 2, 3, 4))
|
||||
for ((a, b, c) in list) {
|
||||
for ((a, _, c) in list) {
|
||||
println("$a$c")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val map = hashMapOf(1 to 1)
|
||||
for ((key, value) in map) {
|
||||
for ((_, value) in map) {
|
||||
println(value)
|
||||
}
|
||||
}
|
||||
@@ -7645,6 +7645,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("mapIndexedLast.kt")
|
||||
public void testMapIndexedLast() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noIt.kt")
|
||||
public void testNoIt() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/noIt.kt");
|
||||
|
||||
Reference in New Issue
Block a user