Project: JDT
Bug 130139 [clean up] Convert for loops to enhanced may mess up program logic in some cases
Reported: 2006-03-02 11:05:36 | Status: resolved fixed

After applying the cleanup wizard on our sources, we noticed some parts of our program failing to work properly, although they compiled well. Our program processes command line output of a console application to display it in a GUI. The following test class reproduces the problem: public class Testcase { public static void main(String[] args) { String[] test = "See also:\n www.google.de\n www.wikipedia.org\n\nSome additional text here".split("\n"); System.out.println("You may also look at these sites:"); for(int i = 0; i < test.length; i++) { if(test[i].contains("See also:")) { for(int x = i + 1; x < test.length; x++) { if(!test[x].startsWith(" ")) break; System.out.println(test[x].trim()); } } } } } After applying the cleanup wizard, the code looks as follows: public class Testcase { public static void main(String[] args) { String[] test = "See also:\n www.google.de\n www.wikipedia.org\n\nSome additional text here".split("\n"); System.out.println("You may also look at these sites:"); for(int i = 0; i < test.length; i++) { if(test[i].contains("See also:")) { for(String element : test) { if(!element.startsWith(" ")) break; System.out.println(element.trim()); } } } } } The first for-loop is kept (I think as the wizard realizes that the loop variable is used), but the inner loop is altered. The cleanup wizard should pay attention for such cases, as those logical errors are hard to find, especially as the affected class worked properly since a few months.

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