Bug 579471 - Create array with curly clean up is not applied to var

Variable declarations using local variable type inference with var are
excluded from the "Create array with curly if possible" clean-up as the
resulting code would not compile due to missing type information.

Change-Id: I27cee832f5d7a11bfd6f0de6f52de2d2fee90427
Signed-off-by: Christian Femers <christian.femers@t-online.de>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/192321
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Thomas  M??der <tmader@redhat.com>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ArrayWithCurlyFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ArrayWithCurlyFixCore.java
index d21509c..6403e6b 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ArrayWithCurlyFixCore.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ArrayWithCurlyFixCore.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2021 Fabrice TIERCELIN and others.
+ * Copyright (c) 2021, 2022 Fabrice TIERCELIN and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
  *
  * Contributors:
  *     Fabrice TIERCELIN - initial API and implementation
+ *     Christian Femers - Bug 579471
  *******************************************************************************/
 package org.eclipse.jdt.internal.corext.fix;
 
@@ -28,6 +29,8 @@
 import org.eclipse.jdt.core.dom.CompilationUnit;
 import org.eclipse.jdt.core.dom.Expression;
 import org.eclipse.jdt.core.dom.ITypeBinding;
+import org.eclipse.jdt.core.dom.Type;
+import org.eclipse.jdt.core.dom.VariableDeclaration;
 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
 import org.eclipse.jdt.core.manipulation.ICleanUpFixCore;
@@ -75,12 +78,21 @@
 		}
 
 		private boolean isDestinationAllowed(final ASTNode visited) {
-			int parentType= visited.getParent().getNodeType();
+			ASTNode parent= visited.getParent();
+			int parentType= parent.getNodeType();
 
-			return parentType == ASTNode.FIELD_DECLARATION
+			boolean correctParent= parentType == ASTNode.FIELD_DECLARATION
 					|| parentType == ASTNode.VARIABLE_DECLARATION_EXPRESSION
 					|| parentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT
 					|| parentType == ASTNode.VARIABLE_DECLARATION_STATEMENT;
+			if (!correctParent) {
+				return false;
+			}
+			if (parent instanceof VariableDeclaration) {
+				Type type= ASTNodes.getType((VariableDeclaration) parent);
+				return type == null || !type.isVar();
+			}
+			return true;
 		}
 	}
 
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
index 482f6b3..07dbe45 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2020, 2021 Fabrice TIERCELIN and others.
+ * Copyright (c) 2020, 2022 Fabrice TIERCELIN and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
  *
  * Contributors:
  *     Fabrice TIERCELIN - initial API and implementation
+ *     Christian Femers - Bug 579471
  *******************************************************************************/
 package org.eclipse.jdt.ui.tests.quickfix;
 
@@ -712,4 +713,27 @@
 
 		assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
 	}
+
+	@Test
+	public void testDoNotUseCurlyBracesOnlyArrayInitializationForVar() throws Exception {
+		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+		String sample= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    public void foo() {\n" //
+				+ "        var a = new String[0];\n" //
+				+ "        var b = new String[][]{ {\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"} };\n" //
+				+ "    }\n" //
+				+ "}\n";
+		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
+
+		/*
+		 * As Array initialization requires and explicit target type, the code above must not change
+		 * even if we activate the "Create array with curly if possible" cleanup.
+		 */
+		enable(CleanUpConstants.ARRAY_WITH_CURLY);
+
+		assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+	}
 }