forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterableIterator.ql
More file actions
44 lines (40 loc) · 1.31 KB
/
IterableIterator.ql
File metadata and controls
44 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @name Iterator implementing Iterable
* @description An 'Iterator' that also implements 'Iterable' by returning itself as its 'Iterator'
* does not support multiple traversals. This can lead to unexpected behavior when
* it is viewed as an 'Iterable'.
* @kind problem
* @problem.severity warning
* @precision very-high
* @id java/iterator-implements-iterable
* @tags quality
* reliability
* correctness
*/
import java
import IterableClass
/** An `Iterable` that is also its own `Iterator`. */
class IterableIterator extends Iterable {
IterableIterator() { this.simpleIterator() instanceof ThisAccess }
}
/** An `IterableIterator` that never returns any elements. */
class EmptyIterableIterator extends IterableIterator {
EmptyIterableIterator() {
exists(Method m |
m.getDeclaringType().getSourceDeclaration() = this and
m.getName() = "hasNext" and
m.getBody()
.(SingletonBlock)
.getStmt()
.(ReturnStmt)
.getResult()
.(BooleanLiteral)
.getBooleanValue() = false
)
}
}
from IterableIterator i
where
// Exclude the empty iterator as that is safe to reuse.
not i instanceof EmptyIterableIterator
select i, "This Iterable is its own Iterator, but does not guard against multiple iterations."