compiler: process literals in analyzeVoidCalls
Function call can occur in the slice or map literal and its result surely isn't unused.
This commit is contained in:
parent
5f3b8c6d51
commit
fd52dee79f
3 changed files with 34 additions and 0 deletions
|
@ -136,6 +136,18 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool {
|
||||||
c.voidCalls[n] = true
|
c.voidCalls[n] = true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *ast.CompositeLit:
|
||||||
|
for _, e := range n.Elts {
|
||||||
|
switch val := e.(type) {
|
||||||
|
case *ast.CallExpr: // slice
|
||||||
|
c.voidCalls[val] = false
|
||||||
|
case *ast.KeyValueExpr: // struct and map
|
||||||
|
ce, ok := val.Value.(*ast.CallExpr)
|
||||||
|
if ok {
|
||||||
|
c.voidCalls[ce] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -321,6 +321,15 @@ var sliceTestCases = []testCase{
|
||||||
}`,
|
}`,
|
||||||
[]byte("string"),
|
[]byte("string"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"literal slice with function call",
|
||||||
|
`package foo
|
||||||
|
func fn() byte { return 't' }
|
||||||
|
func Main() []byte {
|
||||||
|
return []byte{'s', fn(), 'r'}
|
||||||
|
}`,
|
||||||
|
[]byte("str"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSliceOperations(t *testing.T) {
|
func TestSliceOperations(t *testing.T) {
|
||||||
|
|
|
@ -29,6 +29,19 @@ var structTestCases = []testCase{
|
||||||
`,
|
`,
|
||||||
big.NewInt(2),
|
big.NewInt(2),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"struct field from func result",
|
||||||
|
`
|
||||||
|
package foo
|
||||||
|
type S struct { x int }
|
||||||
|
func fn() int { return 2 }
|
||||||
|
func Main() int {
|
||||||
|
t := S{x: fn()}
|
||||||
|
return t.x
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
big.NewInt(2),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"struct field return",
|
"struct field return",
|
||||||
`
|
`
|
||||||
|
|
Loading…
Reference in a new issue