Skip to content

Commit 8f5a2a9

Browse files
authored
Merge pull request #19484 from owen-mc/go/minor-fix
Go: Remove redundant code in `IR::ExtractTupleElementInstruction.getResultType()` and expand tests
2 parents c2f2522 + 933e01b commit 8f5a2a9

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,6 @@ module IR {
718718
predicate extractsElement(Instruction base, int idx) { base = this.getBase() and idx = i }
719719

720720
override Type getResultType() {
721-
exists(CallExpr c | this.getBase() = evalExprInstruction(c) |
722-
result = c.getTarget().getResultType(i)
723-
)
724-
or
725721
exists(Expr e | this.getBase() = evalExprInstruction(e) |
726722
result = e.getType().(TupleType).getComponentType(pragma[only_bind_into](i))
727723
)
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
| test.go:9:2:9:16 | ... := ...[0] | file://:0:0:0:0 | bool |
2-
| test.go:9:2:9:16 | ... := ...[1] | file://:0:0:0:0 | bool |
3-
| test.go:15:2:15:20 | ... := ...[0] | file://:0:0:0:0 | string |
4-
| test.go:15:2:15:20 | ... := ...[1] | file://:0:0:0:0 | bool |
5-
| test.go:21:2:21:22 | ... := ...[0] | file://:0:0:0:0 | string |
6-
| test.go:21:2:21:22 | ... := ...[1] | file://:0:0:0:0 | bool |
1+
| test.go:9:2:9:16 | ... := ...[0] | test.go:9:13:9:16 | <-... | 0 | file://:0:0:0:0 | bool |
2+
| test.go:9:2:9:16 | ... := ...[1] | test.go:9:13:9:16 | <-... | 1 | file://:0:0:0:0 | bool |
3+
| test.go:15:2:15:20 | ... := ...[0] | test.go:15:13:15:20 | index expression | 0 | file://:0:0:0:0 | string |
4+
| test.go:15:2:15:20 | ... := ...[1] | test.go:15:13:15:20 | index expression | 1 | file://:0:0:0:0 | bool |
5+
| test.go:21:2:21:22 | ... := ...[0] | test.go:21:13:21:22 | type assertion | 0 | file://:0:0:0:0 | string |
6+
| test.go:21:2:21:22 | ... := ...[1] | test.go:21:13:21:22 | type assertion | 1 | file://:0:0:0:0 | bool |
7+
| test.go:29:2:29:7 | call to f[0] | test.go:29:4:29:6 | call to g | 0 | file://:0:0:0:0 | int |
8+
| test.go:29:2:29:7 | call to f[1] | test.go:29:4:29:6 | call to g | 1 | file://:0:0:0:0 | int |
9+
| test.go:33:2:33:7 | call to f[0] | test.go:33:4:33:6 | call to v | 0 | file://:0:0:0:0 | int |
10+
| test.go:33:2:33:7 | call to f[1] | test.go:33:4:33:6 | call to v | 1 | file://:0:0:0:0 | int |

go/ql/test/library-tests/semmle/go/IR/test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ func testTypeAssert() {
2121
got, ok := i.(string)
2222
fmt.Printf("%v %v", got, ok)
2323
}
24+
25+
func f(x, y int) {}
26+
func g() (int, int) { return 0, 0 }
27+
28+
func testNestedFunctionCalls() {
29+
f(g())
30+
31+
// Edge case: when we call a function from a variable, `getTarget()` is not defined
32+
v := g
33+
f(v())
34+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import go
22

3-
from IR::ExtractTupleElementInstruction extract
4-
select extract, extract.getResultType()
3+
from IR::ExtractTupleElementInstruction extract, IR::Instruction base, int idx, Type resultType
4+
where extract.extractsElement(base, idx) and resultType = extract.getResultType()
5+
select extract, base, idx, resultType

0 commit comments

Comments
 (0)