[java-inspections] IDEA-287311 Bug: Inspection 'bring variable into scope' introduces Compilation error when used with 'var'

GitOrigin-RevId: 85f7f526ec1cee6533f441bf42bc43b1b0bb8be1
This commit is contained in:
Tagir Valeev
2022-01-28 10:04:59 +00:00
committed by intellij-monorepo-bot
parent eea5b2207e
commit c7ed6477fb
3 changed files with 25 additions and 0 deletions
@@ -123,6 +123,10 @@ public final class BringVariableIntoScopeFix implements IntentionAction, HighPri
PsiDeclarationStatement newDeclaration = (PsiDeclarationStatement)JavaPsiFacade.getElementFactory(manager.getProject()).createStatementFromText("int i = 0", null);
PsiVariable variable = (PsiVariable)newDeclaration.getDeclaredElements()[0].replace(outOfScopeVariable);
if (variable.getInitializer() != null) {
PsiTypeElement typeElement = variable.getTypeElement();
if (typeElement != null && typeElement.isInferredType()) {
PsiTypesUtil.replaceWithExplicitType(typeElement);
}
variable.getInitializer().delete();
}
@@ -0,0 +1,11 @@
// "Bring 'String msg' into scope" "true"
public class BringVarIntoScope {
public void moveVariableDeclarationOutOfBlock(boolean b) {
String msg;
if (b) {
msg = "Leap year";
} else {
msg = "Not a leap year";
}
}
}
@@ -0,0 +1,10 @@
// "Bring 'String msg' into scope" "true"
public class BringVarIntoScope {
public void moveVariableDeclarationOutOfBlock(boolean b) {
if (b) {
var msg = "Leap year";
} else {
<caret>msg = "Not a leap year";
}
}
}