Project: JDT
Bug 154939 [clean up] Convert iterable loop does not detect additional breaking conditions
Reported: 2006-08-23 17:18:00 | Status: verified fixed

Hi, In Source->CleanUp the "Covert for loops to enhanced" will convert also loops which aren't convertable, changing the meaning of the program. E.g. this program which limits the number of executions of the loop package x; import java.util.Iterator; import java.util.List; import java.util.Vector; public class X { private static final String[] init={"a","b","c","d","e"}; public static void main(final String[] args) { final List<String> list = new Vector<String>(); for (int i = 0; i < init.length; i++) { list.add(init[i]); } int n = 2; for (Iterator<String> iterator = list.iterator(); iterator.hasNext() && (n > 0); n--) { String string = iterator.next(); System.out.println(string); } } } and thus produces the output a b will be transformed to (btw. ugly formatted) enhanced for loop even though it couldn't be done. The additional condition and decrement of n is ignored and dropped. package x; import java.util.List; import java.util.Vector; public class X { private static final String[] init={"a","b","c","d","e"}; public static void main(final String[] args) { final List<String> list = new Vector<String>(); for (String element : init) { list.add(element); } int n = 2; for (String string : list) { System.out.println(string); } } } thus the result of this program is different than what is was before and it outputs a b c d e I noticed this today with eclipse 3.2 and have then tried the above test case in a clean 3.3M1 (unpacked in the home directory of a freshly created user) to confirm the error is in the latest milestone of 3.3. claudio

Files changed in fix:
org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/AbstractFix.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/ControlStatementsFix.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/IFix.java org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/StringFix.java org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/CodeFormatFix.java org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/CommentFormatFix.java org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/FixCorrectionProposal.java