diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll index ee7c9b1136a5..1eb9a84f50fb 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll @@ -682,7 +682,7 @@ module CfgImpl { e.(Go::PromotedSelector).refersTo(explicitField) and baseType.getFieldAtDepth(_, explicitFieldDepth) = explicitField | - index = explicitFieldDepth - implicitFieldDepth + index = explicitFieldDepth - implicitFieldDepth and index > 0 ) ) or @@ -692,7 +692,8 @@ module CfgImpl { baseType = e.(Go::PromotedSelector).getSelectedStructType() and e.(Go::PromotedSelector).refersTo(method) and baseType.getMethodAtDepth(_, mDepth) = method and - index = mDepth - implicitFieldDepth + index = mDepth - implicitFieldDepth and + index > 0 | method = baseType.getMethodOfEmbedded(implicitField, _, implicitFieldDepth + 1) or diff --git a/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go b/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go index 0dacf0c44984..016920175bb7 100644 --- a/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go +++ b/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go @@ -186,4 +186,21 @@ func test23() { unreachable() // OK: deliberately unreachable } +type ( + embedded struct{} + recursive struct { + *embedded + value int + *recursive + } +) + +func test24(x *recursive) []int { + values := []int{0} + if x.value != 0 { + values = append(values, x.value) + } + return values // OK: reachable because value is a direct field of recursive +} + func main() {}