Inline Variable: Show error message on attempt to inline unused declaration
This commit is contained in:
@@ -71,6 +71,11 @@ class KotlinInlineValHandler : InlineActionHandler() {
|
|||||||
(it.element as? KtExpression)?.getQualifiedExpressionForSelectorOrThis()
|
(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>()
|
val assignments = Sets.newHashSet<PsiElement>()
|
||||||
referenceExpressions.forEach { expression ->
|
referenceExpressions.forEach { expression ->
|
||||||
val parent = expression.parent
|
val parent = expression.parent
|
||||||
@@ -120,6 +125,8 @@ class KotlinInlineValHandler : InlineActionHandler() {
|
|||||||
assignments.forEach { it.delete() }
|
assignments.forEach { it.delete() }
|
||||||
declaration.delete()
|
declaration.delete()
|
||||||
|
|
||||||
|
if (inlinedExpressions.isEmpty()) return@executeWriteCommand
|
||||||
|
|
||||||
typeArgumentsForCall?.let { addTypeArguments(it, inlinedExpressions) }
|
typeArgumentsForCall?.let { addTypeArguments(it, inlinedExpressions) }
|
||||||
|
|
||||||
parametersForFunctionLiteral?.let { addFunctionLiteralParameterTypes(it, inlinedExpressions) }
|
parametersForFunctionLiteral?.let { addFunctionLiteralParameterTypes(it, inlinedExpressions) }
|
||||||
|
|||||||
@@ -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);
|
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")
|
@TestMetadata("Parameter.kt")
|
||||||
public void testParameter() throws Exception {
|
public void testParameter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/Parameter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/Parameter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user