-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEmptyBlock.ql
More file actions
62 lines (55 loc) · 1.81 KB
/
EmptyBlock.ql
File metadata and controls
62 lines (55 loc) · 1.81 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @name Empty branch of conditional, or empty loop body
* @description An undocumented empty block or statement hinders readability. It may also
* indicate incomplete code.
* @kind problem
* @problem.severity warning
* @precision low
* @id java/empty-block
* @tags reliability
* readability
*/
import semmle.code.java.Statement
/** A block without statements or comments. */
private BlockStmt emptyBlock() {
result.getNumStmt() = 0 and
result.getLocation().getNumberOfCommentLines() = 0
}
/** Auxiliary predicate: file and line of a comment. */
private predicate commentedLine(File file, int line) {
exists(JavadocText text, Location loc |
loc = text.getLocation() and
loc.getFile() = file and
loc.getStartLine() = line and
loc.getEndLine() = line
)
}
/** An uncommented empty statement */
private EmptyStmt emptyStmt() {
not commentedLine(result.getFile(), result.getLocation().getStartLine())
}
/** An empty statement or an empty block. */
Stmt emptyBody() { result = emptyBlock() or result = emptyStmt() }
/**
* Empty blocks or empty statements should not occur as immediate children of if-statements or loops.
* Empty blocks should not occur within other blocks.
*/
predicate blockParent(Stmt empty, string msg) {
empty = emptyBody() and
(
empty.getParent() instanceof IfStmt and
msg = "The body of an if statement should not be empty."
or
empty.getParent() instanceof LoopStmt and msg = "The body of a loop should not be empty."
or
empty.getParent() instanceof BlockStmt and
empty instanceof BlockStmt and
msg = "This block should not be empty."
)
}
from Stmt empty, string msg
where
empty.getFile().isJavaSourceFile() and
empty = emptyBody() and
blockParent(empty, msg)
select empty, msg + " Typographical error or missing code?"