[java-refactoring] Inline variable: fix CompilationError test; support inline at ref in the presence of compilation error

Improves IDEA-336815 "Inline variable" misleading error message when compile-errors are present

GitOrigin-RevId: 6e443a44c4ccf8a65d10f4f83665cfe2641282b0
This commit is contained in:
Tagir Valeev
2023-11-28 12:44:11 +00:00
committed by intellij-monorepo-bot
parent 0679a1a9f4
commit fd00898d32
6 changed files with 37 additions and 12 deletions
@@ -461,6 +461,13 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
def = refExpr;
}
else {
if (local instanceof PsiLocalVariable && refExpr instanceof PsiReferenceExpression) {
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(local, block);
// Simple case when variable is not rewritten: avoid getDefs, to make it working in the presence of compilation errors
if (!ContainerUtil.exists(refs, ref -> PsiUtil.isAccessedForWriting(ref))) {
return local.getInitializer();
}
}
final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr, rethrow);
if (defs.length == 1) {
def = defs[0];
@@ -1,11 +1,9 @@
public class CompilationError {
public void test() {
public static void main(String[] args) {
int <caret>inlineMe = 3; // inline
int dd = 4 + inlineMe;
public static void main(String[] args) {
int <caret>inlineMe = 3; // inline
int dd = 4 + inlineMe;
"Error".
}
"Error".
}
}
@@ -1,11 +1,9 @@
public class CompilationError {
public void test() {
public static void main(String[] args) {
// inline
int dd = 4 + 3;
public static void main(String[] args) {
// inline
int dd = 4 + 3;
"Error".
}
"Error".
}
}
@@ -0,0 +1,9 @@
public class CompilationError {
public static void main(String[] args) {
int inlineMe = 3; // inline
int dd = 4 + <caret>inlineMe;
"Error".
}
}
@@ -0,0 +1,9 @@
public class CompilationError {
public static void main(String[] args) {
// inline
int dd = 4 + 3;
"Error".
}
}
@@ -339,6 +339,10 @@ public class InlineLocalTest extends LightJavaCodeInsightTestCase {
doTest();
}
public void testCompilationErrorAtRef() {
doTest();
}
public void testCompilationErrorAssignment() {
doTest("Cannot perform refactoring.\n" +
"Code contains syntax errors. Cannot perform necessary analysis.");