vi-eclipse / Eclipse-JDT

Umbrella repository for managing a backlog of Eclipse-JDT-related features/issues
0 stars 0 forks source link

Quick Fix: Convert Iterable to Enhanced For Loop Omits Next Method for Any Class #23

Closed amartya4256 closed 10 months ago

amartya4256 commented 11 months ago

Issue: https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1013 There's a bug currently on converting an iterable to enhanced for loop. When there is a statement like object.next(); The statement is omitted upon conversion even if the object belongs to a different class.

The problem is that in the block there's is no check for the iterator variable before performing the operation and it must be added.

public void foo() throws IOException {
    Test i = new Test();
    for (Iterator<String> iterator = c.iterator(); iterator.hasNext();) {
        i.next();
                iterator.next();
    }
}
class Test {
    public Object next() {
        return null;
    }
}

Upon Conversion:

Current Behavior

public void foo() {
    Test i = new Test();
    for (String string: c) {
    }
}

Expected Behavior

public void foo() {
    Test i = new Test();
    for (String string: c) {
        i.next(); 
    }
}
HeikoKlare commented 10 months ago

PR proposed and merged: https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/1014