Created
November 10, 2016 04:58
-
-
Save cypok/d0f988c30489d21e76ce0623385491a6 to your computer and use it in GitHub Desktop.
IDEA inspection is not correct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
} | |
} |
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
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.