Bug 570054 - Var cleanup causes compile error on array initialization

- modify VarCleanUp to not perform cleanup if using array initialization
  without a cast
- add new tests to CleanUpTest10
- reduce useless assignments in the tests

Change-Id: Icedcf6ce883d947f3d1b5be52979547bb9d4d969
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 daf213b..a044518 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
@@ -60,7 +60,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -68,9 +68,8 @@
 				+ "        var number = 0;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -88,7 +87,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -96,9 +95,8 @@
 				+ "        var number = 0L;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -116,7 +114,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -124,9 +122,8 @@
 				+ "        var number = 0F;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -144,7 +141,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -152,9 +149,8 @@
 				+ "        var number = 0D;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -172,7 +168,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -180,9 +176,8 @@
 				+ "        var number = 0x0L;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -202,7 +197,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.ArrayList;\n" //
@@ -212,9 +207,8 @@
 				+ "        var parameterizedType = new ArrayList<String>();\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -234,7 +228,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.HashMap;\n" //
@@ -244,9 +238,8 @@
 				+ "        var parameterizedTypeWithDiamond = new HashMap<Integer, String>();\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -431,7 +424,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.ArrayList;\n" //
@@ -445,9 +438,8 @@
 				+ "        return new ArrayList<Integer>();\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -488,7 +480,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.HashMap;\n" //
@@ -498,9 +490,8 @@
 				+ "        var parameterizedTypeFromCastExpression = (HashMap<Integer, String>) o;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -521,7 +512,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.Collection;\n" //
@@ -532,9 +523,8 @@
 				+ "        var parameterizedTypeFromMethod = m.values();\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -555,7 +545,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.Collection;\n" //
@@ -566,9 +556,8 @@
 				+ "        var parameterizedTypeFromMethod = super.values();\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -588,7 +577,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "import java.util.HashMap;\n" //
@@ -598,9 +587,8 @@
 				+ "        var parameterizedTypeFromVariable = m;\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -620,7 +608,7 @@
 
 		enable(CleanUpConstants.USE_VAR);
 
-		sample= "" //
+		String expected= "" //
 				+ "package test1;\n" //
 				+ "\n" //
 				+ "public class E {\n" //
@@ -630,9 +618,8 @@
 				+ "        }\n" //
 				+ "    }\n" //
 				+ "}\n";
-		String expected1= sample;
 
-		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
 	}
 
 	@Test
@@ -675,4 +662,54 @@
 
 		assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
 	}
+
+	@Test
+	public void testUseLocalVariableTypeForArrays() throws Exception {
+		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+		String sample= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    public void foo(String[] array) {\n" //
+				+ "        String[] a = array;\n" //
+				+ "        String[] b = new String[] {\"a\", \"b\", \"c\"};\n" //
+				+ "        String[][] c = new String[][] { {\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"} };\n" //
+				+ "    }\n" //
+				+ "}\n";
+		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
+
+		enable(CleanUpConstants.USE_VAR);
+
+		String expected= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    public void foo(String[] array) {\n" //
+				+ "        var a = array;\n" //
+				+ "        var b = new String[] {\"a\", \"b\", \"c\"};\n" //
+				+ "        var c = new String[][] { {\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"} };\n" //
+				+ "    }\n" //
+				+ "}\n";
+
+		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected });
+	}
+
+	@Test
+	public void testDoNotUseLocalVariableTypeForArrayInitialization() throws Exception {
+		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+		String sample= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    public void foo() {\n" //
+				+ "        String[] a = {\"a\", \"b\", \"c\"};\n" //
+				+ "        String[][] b = { {\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"} };\n" //
+				+ "    }\n" //
+				+ "}\n";
+		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
+
+		enable(CleanUpConstants.USE_VAR);
+
+		assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+	}
 }
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
index 666abe0..b9ad156 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
@@ -28,6 +28,7 @@
 import org.eclipse.jdt.core.dom.AST;
 import org.eclipse.jdt.core.dom.ASTNode;
 import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.ArrayInitializer;
 import org.eclipse.jdt.core.dom.CastExpression;
 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
 import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -142,7 +143,7 @@
 
 			private boolean maybeUseVar(final Type type, final Expression initializer, final int extraDimensions) {
 				if (type.isVar() || initializer == null || initializer.resolveTypeBinding() == null || type.resolveBinding() == null
-						|| extraDimensions > 0) {
+						|| extraDimensions > 0 || initializer instanceof ArrayInitializer) {
 					if (JavaModelUtil.is11OrHigher(unit.getJavaElement().getJavaProject())) {
 						if (!type.isVar() && initializer == null && type.resolveBinding() != null && extraDimensions == 0) {
 							if (type.getParent() instanceof SingleVariableDeclaration &&