Skip to content

Commit 4cf5529

Browse files
committed
Fix bug relating to allowlist
1 parent 36865e3 commit 4cf5529

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎go/ql/src/RedundantCode/UnreachableStatement.ql‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,28 @@ predicate allowlist(Stmt s) {
9393
exists(getPreviousStmt(s).(IfStmt).getCondition().getBoolValue())
9494
}
9595

96+
/** Holds if `s` is part of an allowlisted prefix of a run of unreachable statements. */
97+
predicate isInAllowlistedUnreachablePrefix(Stmt s) {
98+
allowlist(s) and
99+
(
100+
firstUnreachableStmt(s)
101+
or
102+
isInAllowlistedUnreachablePrefix(getPreviousStmt(s))
103+
)
104+
}
105+
106+
/** Holds if `s` is the first non-allowlisted statement in a run of unreachable statements. */
107+
predicate firstNonAllowlistedUnreachableStmt(Stmt s) {
108+
not isReachable(s) and
109+
not s instanceof EmptyStmt and
110+
not allowlist(s) and
111+
(
112+
firstUnreachableStmt(s)
113+
or
114+
isInAllowlistedUnreachablePrefix(getPreviousStmt(s))
115+
)
116+
}
117+
96118
from Stmt s
97-
where
98-
firstUnreachableStmt(s) and
99-
not allowlist(s)
119+
where firstNonAllowlistedUnreachableStmt(s)
100120
select s, "This statement is unreachable."

‎go/ql/test/query-tests/RedundantCode/UnreachableStatement/UnreachableStatement.expected‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
| main.go:147:2:147:17 | return statement | This statement is unreachable. |
1111
| main.go:153:2:153:22 | return statement | This statement is unreachable. |
1212
| main.go:159:2:159:43 | return statement | This statement is unreachable. |
13+
| main.go:165:2:165:14 | expression statement | This statement is unreachable. |

‎go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func test19() mystruct {
162162
func test20() {
163163
select {}
164164
panic("unreachable") // OK: allowlisted statement
165-
unreachable() // $ MISSING: Alert
165+
unreachable() // $ Alert
166166
}
167167

168168
func main() {}

0 commit comments

Comments
 (0)