mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
[java-refactoring] Inline variable on non-initialized declaration
Could be available if there's only one write visible for all the reads Fixes IDEA-354157 Inline variable not working when staying on uninitialized declaration GitOrigin-RevId: e257b2b493a3902e901699efa4eac90f62e3108b
This commit is contained in:
committed by
intellij-monorepo-bot
parent
bab02f85ff
commit
b01ac55564
@@ -485,7 +485,23 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
if (rExpr != null) return rExpr;
|
||||
}
|
||||
}
|
||||
return local.getInitializer();
|
||||
PsiExpression initializer = local.getInitializer();
|
||||
if (initializer != null) {
|
||||
return initializer;
|
||||
}
|
||||
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(local, block);
|
||||
Set<PsiExpression> allDefs = new HashSet<>();
|
||||
for (PsiReferenceExpression ref : refs) {
|
||||
if (PsiUtil.isAccessedForWriting(ref)) {
|
||||
if (PsiUtil.isAccessedForReading(ref)) return null;
|
||||
continue;
|
||||
}
|
||||
PsiExpression def = getDefToInline(local, ref, block, rethrow);
|
||||
if (def == null) return null;
|
||||
allDefs.add(def);
|
||||
if (allDefs.size() != 1) return null;
|
||||
}
|
||||
return ContainerUtil.getOnlyItem(allDefs);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
String a<caret>aa;
|
||||
aaa = args[0];
|
||||
System.out.println(aaa);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(args[0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
String a<caret>aa;
|
||||
if (args.length > 0) {
|
||||
aaa = args[0];
|
||||
System.out.println(aaa);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
if (args.length > 0) {
|
||||
System.out.println(args[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
String a<caret>aa;
|
||||
if (args.length > 0) {
|
||||
aaa = args[0];
|
||||
System.out.println(aaa);
|
||||
} else {
|
||||
aaa = "hello";
|
||||
System.out.println(aaa);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
String a<caret>aa;
|
||||
if (args.length > 0) {
|
||||
aaa = args[0];
|
||||
System.out.println(aaa);
|
||||
} else {
|
||||
aaa = "oops";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
String aaa;
|
||||
if (args.length > 0) {
|
||||
System.out.println(args[0]);
|
||||
} else {
|
||||
aaa = "oops";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -359,6 +359,11 @@ public class InlineLocalTest extends LightJavaCodeInsightTestCase {
|
||||
|
||||
public void testLambdaInitialization() { doTest(); }
|
||||
|
||||
public void testSeparateInitialization() { doTest(); }
|
||||
public void testSeparateInitialization2() { doTest(); }
|
||||
public void testSeparateInitialization3() { doTest("Cannot perform refactoring.\nVariable aaa has no initializer"); }
|
||||
public void testSeparateInitialization4() { doTest(); }
|
||||
|
||||
private void doTest(String conflictMessage) {
|
||||
try {
|
||||
doTest();
|
||||
|
||||
Reference in New Issue
Block a user