diff --git a/go/ql/src/RedundantCode/DeadStoreOfLocal.ql b/go/ql/src/RedundantCode/DeadStoreOfLocal.ql index 7a523926f498..2b6886222aa4 100644 --- a/go/ql/src/RedundantCode/DeadStoreOfLocal.ql +++ b/go/ql/src/RedundantCode/DeadStoreOfLocal.ql @@ -37,8 +37,11 @@ where def.getBasicBlock() instanceof ReachableBasicBlock and // exclude assignments with default values or simple expressions not isSimple(rhs) and - // exclude variables that are not used at all - exists(target.getAReference()) and + // exclude variables that are not used in reachable code + exists(IR::Instruction ref | + ref != def and + (ref.reads(target) or ref.writes(target, _)) + ) and // exclude variables with indirect references not target.mayHaveIndirectReferences() and // Report the assigned variable rather than the whole write instruction. A write to an diff --git a/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/CONSISTENCY/UnexpectedFrontendErrors.expected b/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/CONSISTENCY/UnexpectedFrontendErrors.expected index a7a03f17abcf..dbdac1b9d081 100644 --- a/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/CONSISTENCY/UnexpectedFrontendErrors.expected +++ b/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/CONSISTENCY/UnexpectedFrontendErrors.expected @@ -1 +1 @@ -| main.go:36:9:36:9 | undefined: unknownFunction | +| main.go:41:9:41:9 | undefined: unknownFunction | diff --git a/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/main.go b/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/main.go index ee7b9214a66b..6574f5377bb2 100644 --- a/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/main.go +++ b/go/ql/test/query-tests/RedundantCode/DeadStoreOfLocal/main.go @@ -26,6 +26,11 @@ func deadParameter(x int) bool { // we don't want to flag x here return true } +func usedOnlyAfterFalse() bool { + s := deadStore() + return false && s == 0 +} + func test2(x int) (int, int) { y := x >> 5 z := x % (1)