-
Notifications
You must be signed in to change notification settings - Fork 8
/
cancelnil.go
48 lines (37 loc) · 930 Bytes
/
cancelnil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cancelnil
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type CancelNil struct{}
var _ viruses.Virus = (*CancelNil)(nil)
// New returns a new CancelNil virus. It changes calls to context.CancelCauseFunc to pass nil.
func New() *CancelNil {
return &CancelNil{}
}
func (v *CancelNil) Incubate(node ast.Node, typeInfo *types.Info) []*viruses.Infection {
call, ok := node.(*ast.CallExpr)
if !ok {
return nil
}
funType := typeInfo.TypeOf(call.Fun)
if funType == nil {
return nil
}
if funType.String() != "context.CancelCauseFunc" {
return nil
}
origArg := call.Args[0]
if ident, ok := origArg.(*ast.Ident); ok && ident.Name == "nil" {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Call cancel(nil)",
func() { call.Args[0] = &ast.Ident{Name: "nil", NamePos: token.NoPos, Obj: nil} },
func() { call.Args[0] = origArg },
),
}
}