From 5fc112fa775a59a60902dc03cc9362a328c9b01b Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Wed, 25 Jun 2025 20:41:17 +0200 Subject: [PATCH] [java-property-tests] IDEA-375027 incorrect var type when use var type is enabled GitOrigin-RevId: a24859428c9fac54a2aa75eb0e82a2ac018114ce --- .../JavaVariableInplaceIntroducer.java | 2 +- .../unresolvedVariable.java | 11 +++++++++++ .../unresolvedVariable_after.java | 12 ++++++++++++ .../refactoring/InplaceIntroduceVariableTest.java | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java create mode 100644 java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java diff --git a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java index 6358b248d248..3d5ea9998a7f 100644 --- a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -345,7 +345,7 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer } } } - if (psiVariable != null && psiVariable.isValid()) { + if (psiVariable != null && psiVariable.isValid() && !PsiTypes.nullType().equals(psiVariable.getType())) { DumbService.getInstance(project).runWithAlternativeResolveEnabled(() -> createCastInVariableDeclaration(project, psiVariable)); } } diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java new file mode 100644 index 000000000000..980ed8d67525 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java @@ -0,0 +1,11 @@ + +class Test { + void f(Object o) { + System.out.println(((SomeUnresolved)o).doSomething()); + } + + class SomeUnresolved{ + Test doSomething() {} + } +} + diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java new file mode 100644 index 000000000000..b41da4418898 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java @@ -0,0 +1,12 @@ + +class Test { + void f(Object o) { + var o = ((SomeUnresolved) o).doSomething(); + System.out.println(o); + } + + class SomeUnresolved{ + Test doSomething() {} + } +} + diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java index c1b354e0f6f6..fc44cbfe036e 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java @@ -20,6 +20,7 @@ import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.IntroduceVariableUtil; +import com.intellij.refactoring.JavaRefactoringSettings; import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer; import com.intellij.refactoring.introduceVariable.IntroduceVariableHandler; import com.intellij.refactoring.util.CommonRefactoringUtil; @@ -77,6 +78,19 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe doTest(introducer -> type("constants")); } + public void testUnresolvedVariable() { + JavaRefactoringSettings instance = JavaRefactoringSettings.getInstance(); + Boolean oldValue = instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE; + instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE = true; + try { + doTest(introducer -> type("o")); + } + finally { + instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE = oldValue; + } + + } + public void testInsideInjectedString() { doTestInsideInjection(introducer -> type("expr")); }