Project: JDT
Bug 203693 [clean up] convert for loops removes local
Reported: 2007-09-18 04:06:37 | Status: verified fixed

Build ID: I20070809-1105 Steps To Reproduce: 1. Paste the code "Before" in a java file 2. Choose Source -> Clean up... 3. Use a profile wich contain only: * Convert for loops to enhanced for loops 4. Apply 5. You should have the code "After" More information: The local declaration ("nouv") which look like the iterable local ("anc") is removed, which corrupt the code and create compiler errors. Before ============================================== import java.util.Iterator; import java.util.TreeMap; public class EnhancedForLoop { public void foo() { TreeMap<String, String> tm = new TreeMap<String, String>(); String s = ""; for (Iterator<String> it = tm.keySet().iterator(); it.hasNext(); ) { String anc = it.next(); String nouv = null; if (anc.equals("dummy")) { nouv = anc.substring(0); s += nouv; } } } } After ============================================== import java.util.TreeMap; public class EnhancedForLoop { public void foo() { TreeMap<String, String> tm = new TreeMap<String, String>(); String s = ""; for (String nouv : tm.keySet()) { if (anc.equals("dummy")) { // error: anc cannot be resolved nouv = anc.substring(0); // error: anc cannot be resolved s += nouv; } } } }

Files changed in fix:
org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ConvertIterableLoopQuickFixTest.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java