Skip to content

Commit

Permalink
Match type of unused variable name.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Dec 2, 2023
1 parent 258ef2c commit 98caf69
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/main/kotlin/org/openrewrite/kotlin/KotlinTypeMapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.openrewrite.java.tree.JavaType
import org.openrewrite.java.tree.JavaType.*
import org.openrewrite.java.tree.TypeUtils
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.convertClassIdToFqn
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.variableName
import kotlin.collections.ArrayList

@Suppress("DuplicatedCode")
Expand Down Expand Up @@ -827,7 +828,7 @@ class KotlinTypeMapping(
val vt = Variable(
null,
mapToFlagsBitmap(variable.visibility, variable.modality),
variable.name.asString(),
variableName(variable.name.asString()),
null, null, null
)
typeCache.put(signature, vt)
Expand Down Expand Up @@ -1154,7 +1155,7 @@ class KotlinTypeMapping(
val variable = Variable(
null,
convertToFlagsBitMap(javaField.visibility, javaField.isStatic, javaField.isFinal, javaField.isAbstract),
javaField.name.asString(),
variableName(javaField.name.asString()),
null, null, null
)
typeCache.put(signature, variable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val
else -> fileSignature(firFile)
}
sig.append(owner)
sig.append("{name=${property.name.asString()}")
sig.append("{name=${variableName(property.name.asString())}")
sig.append(",type=${signature(property.returnTypeRef)}}")
return sig.toString()
}
Expand Down Expand Up @@ -661,7 +661,7 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val
owner = owner.substring(0, owner.indexOf('<'))
}
sig.append(owner)
sig.append("{name=${javaField.name.asString()}")
sig.append("{name=${variableName(javaField.name.asString())}")
sig.append(",type=${signature(javaField.type)}}")
return sig.toString()
}
Expand All @@ -687,5 +687,12 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val
.replace("?", "")
return if (cleanedFqn.startsWith(".")) cleanedFqn.substring(1) else cleanedFqn
}

fun variableName(name: String): String {
return when (name) {
"<unused var>" -> "_"
else -> name
}
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1016,5 +1016,30 @@ public J.Identifier visitIdentifier(J.Identifier identifier, Integer integer) {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/483")
@Test
void unusedVar() {
//noinspection RemoveRedundantBackticks,RemoveRedundantQualifierName
rewriteRun(
kotlin(
"val unused: (Int, Int) -> Int = { _, y -> y }",
spec -> spec.afterRecipe(cu -> {
AtomicBoolean found = new AtomicBoolean(false);
new KotlinIsoVisitor<Integer>() {
@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Integer integer) {
if ("_".equals(identifier.getSimpleName())) {
assertThat(identifier.getFieldType().getName()).isEqualTo("_");
found.set(true);
}
return super.visitIdentifier(identifier, integer);
}
}.visit(cu, 0);
assertThat(found.get()).isTrue();
})
)
);
}
}
}

0 comments on commit 98caf69

Please sign in to comment.