Skip to content

Commit 31e1ee5

Browse files
committed
SA9010: simplify implementation
1 parent 4d738e5 commit 31e1ee5

File tree

2 files changed

+5
-49
lines changed

2 files changed

+5
-49
lines changed

staticcheck/sa9010/sa9010.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,53 +48,9 @@ function:
4848
var Analyzer = SCAnalyzer.Analyzer
4949

5050
func run(pass *analysis.Pass) (any, error) {
51-
checkIdent := func(c *ast.Ident) bool {
52-
var (
53-
obj = pass.TypesInfo.ObjectOf(c)
54-
sig *types.Signature
55-
)
56-
switch f := obj.(type) {
57-
case *types.Builtin:
58-
return false
59-
case *types.Func:
60-
sig = f.Type().(*types.Signature)
61-
case *types.Var:
62-
switch ff := f.Type().(type) {
63-
case *types.Signature:
64-
sig = ff
65-
case *types.Named:
66-
sig = ff.Underlying().(*types.Signature)
67-
}
68-
}
69-
r := sig.Results()
70-
if r != nil && r.Len() == 1 {
71-
_, ok := r.At(0).Type().(*types.Signature)
72-
return ok
73-
}
74-
return false
75-
}
76-
7751
fn := func(n ast.Node) {
78-
var (
79-
returnsFunc bool
80-
def = n.(*ast.DeferStmt)
81-
)
82-
switch c := def.Call.Fun.(type) {
83-
case *ast.FuncLit: // defer func() { }()
84-
r := c.Type.Results
85-
if r != nil && len(r.List) == 1 {
86-
_, returnsFunc = r.List[0].Type.(*ast.FuncType)
87-
}
88-
case *ast.Ident: // defer f()
89-
returnsFunc = checkIdent(c)
90-
case *ast.SelectorExpr: // defer t.f()
91-
returnsFunc = checkIdent(c.Sel)
92-
case *ast.IndexExpr: // defer f[int](0)
93-
if id, ok := c.X.(*ast.Ident); ok {
94-
returnsFunc = checkIdent(id)
95-
}
96-
}
97-
if returnsFunc {
52+
def := n.(*ast.DeferStmt)
53+
if _, ok := pass.TypesInfo.TypeOf(def.Call).Underlying().(*types.Signature); ok {
9854
report.Report(pass, def, "deferred return function not called")
9955
}
10056
}

staticcheck/sa9010/testdata/go1.0/example.com/deferr/deferr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func x() {
6868
defer func(int) func(int) int { return func(int) int { return 0 } }(0) //@ diag(`deferred return function not called`)
6969

7070
// Function returns a function which returns another function. This is
71-
// getting silly and is not checked.
72-
defer silly1()()
73-
defer func() func() func() {
71+
// getting silly, but we do flag it.
72+
defer silly1()() //@ diag(`deferred return function not called`)
73+
defer func() func() func() { //@ diag(`deferred return function not called`)
7474
return func() func() {
7575
return func() {}
7676
}

0 commit comments

Comments
 (0)