staticcheck: simplify CheckIneffectiveAppend
@@ -1854,37 +1854,6 @@ func (c *Checker) CheckSeeker(j *lint.Job) {
}
func (c *Checker) CheckIneffectiveAppend(j *lint.Job) {
- fn := func(node ast.Node) bool {
- assign, ok := node.(*ast.AssignStmt)
- if !ok || len(assign.Lhs) != 1 || len(assign.Rhs) != 1 {
- return true
- }
- ident, ok := assign.Lhs[0].(*ast.Ident)
- if !ok || ident.Name == "_" {
- return true
- }
- call, ok := assign.Rhs[0].(*ast.CallExpr)
- if !ok {
- return true
- }
- if callIdent, ok := call.Fun.(*ast.Ident); !ok || callIdent.Name != "append" {
- // XXX check that it's the built-in append
- return true
- }
- ssafn := c.nodeFns[assign]
- if ssafn == nil {
- return true
- }
- tfn, ok := ssafn.Object().(*types.Func)
- if ok {
- res := tfn.Type().(*types.Signature).Results()
- for i := 0; i < res.Len(); i++ {
- if res.At(i) == j.Program.Info.ObjectOf(ident) {
- // Don't flag appends assigned to named return arguments
- return true
- }
- }
- }
isAppend := func(ins ssa.Value) bool {
call, ok := ins.(*ssa.Call)
if !ok {
@@ -1898,6 +1867,15 @@ func (c *Checker) CheckIneffectiveAppend(j *lint.Job) {
}
return true
}
+
+ for _, ssafn := range j.Program.InitialFunctions {
+ for _, block := range ssafn.Blocks {
+ for _, ins := range block.Instrs {
+ val, ok := ins.(ssa.Value)
+ if !ok || !isAppend(val) {
+ continue
+ }
+
isUsed := false
visited := map[ssa.Instruction]bool{}
var walkRefs func(refs []ssa.Instruction)
@@ -1914,6 +1892,8 @@ func (c *Checker) CheckIneffectiveAppend(j *lint.Job) {
switch ref := ref.(type) {
case *ssa.Phi:
walkRefs(*ref.Referrers())
+ case *ssa.Sigma:
+ walkRefs(*ref.Referrers())
case ssa.Value:
if !isAppend(ref) {
isUsed = true
@@ -1926,22 +1906,16 @@ func (c *Checker) CheckIneffectiveAppend(j *lint.Job) {
}
}
}
- expr, _ := ssafn.ValueForExpr(call)
- if expr == nil {
- return true
- }
- refs := expr.Referrers()
+ refs := val.Referrers()
if refs == nil {
- return true
+ continue
}
walkRefs(*refs)
if !isUsed {
- j.Errorf(assign, "this result of append is never used, except maybe in other appends")
+ j.Errorf(ins, "this result of append is never used, except maybe in other appends")
+ }
}
- return true
}
- for _, f := range j.Program.Files {
- ast.Inspect(f, fn)
}
}
