Inline Variable: Show error message on attempt to inline unused declaration

This commit is contained in:
Alexey Sedunov
2015-12-07 14:04:15 +03:00
parent 5c88a1c63e
commit e82e4bcfc8
3 changed files with 17 additions and 0 deletions
@@ -71,6 +71,11 @@ class KotlinInlineValHandler : InlineActionHandler() {
(it.element as? KtExpression)?.getQualifiedExpressionForSelectorOrThis()
}
if (referenceExpressions.isEmpty()) {
val kind = if (declaration.isLocal) "Variable" else "Property"
return showErrorHint(project, editor, "$kind '$name' is never used")
}
val assignments = Sets.newHashSet<PsiElement>()
referenceExpressions.forEach { expression ->
val parent = expression.parent
@@ -120,6 +125,8 @@ class KotlinInlineValHandler : InlineActionHandler() {
assignments.forEach { it.delete() }
declaration.delete()
if (inlinedExpressions.isEmpty()) return@executeWriteCommand
typeArgumentsForCall?.let { addTypeArguments(it, inlinedExpressions) }
parametersForFunctionLiteral?.let { addFunctionLiteralParameterTypes(it, inlinedExpressions) }
+4
View File
@@ -0,0 +1,4 @@
// ERROR: Variable 'a' is never used
fun foo() {
val <caret>a = 1 + 2
}
@@ -83,6 +83,12 @@ public class InlineTestGenerated extends AbstractInlineTest {
doTest(fileName);
}
@TestMetadata("noUsages.kt")
public void testNoUsages() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/noUsages.kt");
doTest(fileName);
}
@TestMetadata("Parameter.kt")
public void testParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/Parameter.kt");