Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 563269

Summary: [quickfix] enhanced loop quickfix loop element name starts with 2 instead of 1 if name is already used
Product: [Eclipse Project] JDT Reporter: Carsten Hammer <carsten.hammer>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jjohnstn
Version: 4.16   
Target Milestone: ---   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/163249
Whiteboard:

Description Carsten Hammer CLA 2020-05-17 12:28:48 EDT
If the variable name created for the loop element variable is already used and a postfix number has to be appended it starts with 2 instead of 1.

Look at the results of this failing test:

	@Test
	public void testNameDetectionChildrenChildAlreadyUsed() throws Exception {
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
		StringBuilder buf= new StringBuilder();
		buf.append("package test1;\n");
		buf.append("public class A {\n");
		buf.append("	public void foo() {\n");
		buf.append("		int[] children = {1,2,3,4};\n");
		buf.append("		for (int i = 0; i < children.length; i++){\n");
		buf.append("			int child=1+1;\n");
		buf.append("		}\n");
		buf.append("	}\n");
		buf.append("}\n");
		ICompilationUnit cu= pack1.createCompilationUnit("A.java", buf.toString(), false, null);

		List<IJavaCompletionProposal> proposals= fetchConvertingProposal(buf, cu);

		assertNotNull(fConvertLoopProposal);

		assertCorrectLabels(proposals);

		String preview1= getPreviewContent(fConvertLoopProposal);

		buf= new StringBuilder();
		buf.append("package test1;\n");
		buf.append("public class A {\n");
		buf.append("	public void foo() {\n");
		buf.append("		int[] children = {1,2,3,4};\n");
		buf.append("		for (int child1 : children) {\n");
		buf.append("			int child=1+1;\n");
		buf.append("		}\n");
		buf.append("	}\n");
		buf.append("}\n");
		String expected= buf.toString();
		assertEqualString(preview1, expected);
	}

The created code contains "for (int child2 : children) {" although "child1" is not used.
I am not sure if this is a bug or is intended to have a bigger difference to the already used variable.
Comment 1 Eclipse Genie CLA 2020-05-19 11:55:40 EDT
New Gerrit change created: https://git.eclipse.org/r/163249
Comment 2 Jeff Johnston CLA 2020-06-10 17:52:30 EDT
Hi Carsten, this is intentional.  The InternalNamingConventions.excludeNames() method starts with 2.  It kind of makes sense (child, child2, child3) where child is implied as child1 or "first" child.

If no further issues, I will close this as WONTFIX.
Comment 3 Carsten Hammer CLA 2020-06-11 12:42:22 EDT
OK, I am not completely convinced as it goes on with child3 when I already make use of child and child2 but I get the idea:)
Somehow all the variable naming game code should be refactored into a separate class or package. Looks like it is spread over different classes in jdt.ui and jdt.core.