diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll index 1da4b1e51231..1bdcf277d2c8 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll @@ -679,7 +679,7 @@ module CfgImpl { e.(Go::PromotedSelector).refersTo(explicitField) and baseType.getFieldAtDepth(_, explicitFieldDepth) = explicitField | - index = explicitFieldDepth - implicitFieldDepth + index = explicitFieldDepth - implicitFieldDepth and index > 0 ) ) or @@ -689,7 +689,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 cc26b717f605..db36be0c552f 100644 --- a/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go +++ b/go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go @@ -159,4 +159,21 @@ func test19() mystruct { return mystruct{test10(1), test10(2) == 2} // $ Alert } +type ( + embedded struct{} + recursive struct { + *embedded + value int + *recursive + } +) + +func test20(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() {}