Skip to content

Instantly share code, notes, and snippets.

@cypok
Created November 10, 2016 04:58
Show Gist options
  • Save cypok/d0f988c30489d21e76ce0623385491a6 to your computer and use it in GitHub Desktop.
Save cypok/d0f988c30489d21e76ce0623385491a6 to your computer and use it in GitHub Desktop.
IDEA inspection is not correct
import java.util.Iterator;
import java.util.NoSuchElementException;
final class Tree<T> {
private final T elem;
private final Tree<T> parent;
private Tree(T elem, Tree<T> parent) {
this.elem = elem;
this.parent = parent;
}
/** Iterator from current tree element to it's root. */
public Iterator<T> toRoot() {
return new Iterator<T>() {
Tree<T> curr = Tree.this;
@Override
public boolean hasNext() {
return curr.parent != null;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final T res = curr.elem; // IDEA inspection: Local variable 'res' is redundant
curr = curr.parent;
return res;
}
};
}
}
@cypok
Copy link
Author

cypok commented Nov 10, 2016

Quick-fix for inspection on line 30 inlines the variable res into line 32 which is not correct.

Note that if we remove final on line 6 the inspection disappears.

@cypok
Copy link
Author

cypok commented Nov 10, 2016

IDEA version information:

IntelliJ IDEA 2016.2.2
Build #IC-162.1628.40, built on August 16, 2016
JRE: 1.8.0_40-b25 amd64
JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment