Bug 515744 - [clean up][quick assist] Converting Iterator loop to enhanced for loop removes update part
Summary: [clean up][quick assist] Converting Iterator loop to enhanced for loop remove...
Status: REOPENED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2017-04-25 05:12 EDT by Daniel Herding CLA
Modified: 2024-03-04 20:17 EST (History)
1 user (show)

See Also:


Attachments
screenshot.png (105.72 KB, image/png)
2017-04-25 05:12 EDT, Daniel Herding CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Herding CLA 2017-04-25 05:12:40 EDT
Created attachment 267962 [details]
screenshot.png

Given this Java code:

public final class Test {
    public static void main(String[] args) {
        int x = 1;
        List<String> strings = Arrays.asList("foo", "bar", "baz");
        for (Iterator<String> iterator = strings.iterator(); iterator.hasNext(); x++) {
            System.out.println(x + ": " + iterator.next());
        }
    }
}

Select the keyword "for", press Ctrl-1, and select "Convert to enhanced 'for' loop".
The functionality is broken because the value of x is no longer incremented; this problem is very difficult to spot during code reviews:

public final class Test {
    public static void main(String[] args) {
        int x = 1;
        List<String> strings = Arrays.asList("foo", "bar", "baz");
        for (String string : strings) {
            System.out.println(x + ": " + string);
        }
    }
}

This is an issue which has been known for years.
In Bug 160270, a warning has been added (see attached screenshot).
This warning is not enough. The icon decorator is easy to overlook, and developers rarely look at the tooltip with the preview.

There is no value in providing a "quick fix" which breaks the functionality of the code. The quick fix should not be available if the loop cannot be automatically converted with identical semantics.
Comment 1 Eclipse Genie CLA 2020-03-18 16:28:54 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 2 Daniel Herding CLA 2020-03-19 03:59:33 EDT
I still think that the quickfix should be disabled.
Comment 3 Eclipse Genie CLA 2022-03-10 00:11:44 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 4 Eclipse Genie CLA 2024-03-04 20:17:05 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.