8000 staticcheck/sa1012: handle method expressions by ychampion · Pull Request #1851 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions staticcheck/sa1012/sa1012.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ e.g. \'context.TODO\' or \'context.Background\'.`,

var Analyzer = SCAnalyzer.Analyzer

var checkNilContextQ = pattern.MustParse(`(CallExpr fun@(Symbol _) (Builtin "nil"):_)`)
var checkNilContextQ = pattern.MustParse(`(Or
(CallExpr fun@(Symbol _) (Builtin "nil"):_)
(CallExpr fun@(Symbol _) _:(Builtin "nil"):_))`)

func run(pass *analysis.Pass) (any, error) {
todo := &ast.CallExpr{
Expand All @@ -49,20 +51,32 @@ func run(pass *analysis.Pass) (any, error) {
// it might also be a builtin
continue
}
argIdx := 0
if sel, ok := ast.Unparen(call.Fun).(*ast.SelectorExpr); ok {
if selection, ok := pass.TypesInfo.Selections[sel]; ok && selection.Kind() == types.MethodExpr {
// In a method expression like (*T).Foo(recv, ctx), call.Args[0]
// is the receiver and call.Args[1] is the method's first parameter.
argIdx = 1
}
}
if len(call.Args) <= argIdx {
continue
}
arg := call.Args[argIdx]
if id, ok := ast.Unparen(arg).(*ast.Ident); !ok || pass.TypesInfo.ObjectOf(id) != types.Universe.Lookup("nil") {
continue
}
sig := fun.Type().(*types.Signature)
if sig.Params().Len() == 0 {
// Our CallExpr might've matched a method expression, like
// (*T).Foo(nil) – here, nil isn't the first argument of
// the Foo method, but the method receiver.
continue
}
if !typeutil.IsTypeWithName(sig.Params().At(0).Type(), "context.Context") {
continue
}
report.Report(pass, call.Args[0],
report.Report(pass, arg,
"do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use", report.Fixes(
edit.Fix("Use context.TODO", edit.ReplaceWithNode(pass.Fset, call.Args[0], todo)),
edit.Fix("Use context.Background", edit.ReplaceWithNode(pass.Fset, call.Args[0], bg))))
edit.Fix("Use context.TODO", edit.ReplaceWithNode(pass.Fset, arg, todo)),
edit.Fix("Use context.Background", edit.ReplaceWithNode(pass.Fset, arg, bg))))
}
return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ func fn4() {}

type T struct{}

func (*T) Foo() {}
func (*T) Foo() {}
func (*T) Bar(ctx context.Context) {}

type I interface {
Bar(ctx context.Context)
}

func fn3() {
fn1(nil) //@ diag(`do not pass a nil Context`)
Expand All @@ -21,4 +26,13 @@ func fn3() {
// and don't crash on these
_ = (func())(nil)
(*T).Foo(nil)

var t *T
t.Bar(nil) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, context.Background())
((*T).Bar)(nil, context.Background())
I.Bar(nil, context.Background())
(context.Context).Value(nil, "k")
(*T).Bar(t, nil) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, nil) //@ diag(`do not pass a nil Context`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ func fn4() {}

type T struct{}

func (*T) Foo() {}
func (*T) Foo() {}
func (*T) Bar(ctx context.Context) {}

type I interface {
Bar(ctx context.Context)
}

func fn3() {
fn1(context.Background()) //@ diag(`do not pass a nil Context`)
Expand All @@ -22,6 +27,15 @@ func fn3() {
// and don't crash on these
_ = (func())(nil)
(*T).Foo(nil)

var t *T
t.Bar(context.Background()) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, context.Background())
((*T).Bar)(nil, context.Background())
I.Bar(nil, context.Background())
(context.Context).Value(nil, "k")
(*T).Bar(t, context.Background()) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, context.Background()) //@ diag(`do not pass a nil Context`)
}
-- Use context.TODO --
package pkg
Expand All @@ -34,7 +48,12 @@ func fn4() {}

type T struct{}

func (*T) Foo() {}
func (*T) Foo() {}
func (*T) Bar(ctx context.Context) {}

type I interface {
Bar(ctx context.Context)
}

func fn3() {
fn1(context.TODO()) //@ diag(`do not pass a nil Context`)
Expand All @@ -47,4 +66,13 @@ func fn3() {
// and don't crash on these
_ = (func())(nil)
(*T).Foo(nil)

var t *T
t.Bar(context.TODO()) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, context.Background())
((*T).Bar)(nil, context.Background())
I.Bar(nil, context.Background())
(context.Context).Value(nil, "k")
(*T).Bar(t, context.TODO()) //@ diag(`do not pass a nil Context`)
(*T).Bar(nil, context.TODO()) //@ diag(`do not pass a nil Context`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import "context"
func tpfn1[T any](ctx context.Context, x T) {}
func tpfn2[T1, T2 any](ctx context.Context, x T1, y T2) {}

type GT[T any] struct{}

func (*GT[T]) Bar(ctx context.Context, x T) {}

func tpbar() {
tpfn1[int](nil, 0) //@ diag(`do not pass a nil Context`)
tpfn1(nil, 0) //@ diag(`do not pass a nil Context`)

tpfn2[int, int](nil, 0, 0) //@ diag(`do not pass a nil Context`)
tpfn2(nil, 0, 0) //@ diag(`do not pass a nil Context`)

(*GT[int]).Bar(nil, context.Background(), 0)
(*GT[int]).Bar(nil, nil, 0) //@ diag(`do not pass a nil Context`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import "context"
func tpfn1[T any](ctx context.Context, x T) {}
func tpfn2[T1, T2 any](ctx context.Context, x T1, y T2) {}

type GT[T any] struct{}

func (*GT[T]) Bar(ctx context.Context, x T) {}

func tpbar() {
tpfn1[int](context.Background(), 0) //@ diag(`do not pass a nil Context`)
tpfn1(context.Background(), 0) //@ diag(`do not pass a nil Context`)

tpfn2[int, int](context.Background(), 0, 0) //@ diag(`do not pass a nil Context`)
tpfn2(context.Background(), 0, 0) //@ diag(`do not pass a nil Context`)

(*GT[int]).Bar(nil, context.Background(), 0)
(*GT[int]).Bar(nil, context.Background(), 0) //@ diag(`do not pass a nil Context`)
}
-- Use context.TODO --
package pkg
Expand All @@ -21,10 +28,17 @@ import "context"
func tpfn1[T any](ctx context.Context, x T) {}
func tpfn2[T1, T2 any](ctx context.Context, x T1, y T2) {}

type GT[T any] struct{}

func (*GT[T]) Bar(ctx context.Context, x T) {}

func tpbar() {
tpfn1[int](context.TODO(), 0) //@ diag(`do not pass a nil Context`)
tpfn1(context.TODO(), 0) //@ diag(`do not pass a nil Context`)

tpfn2[int, int](context.TODO(), 0, 0) //@ diag(`do not pass a nil Context`)
tpfn2(context.TODO(), 0, 0) //@ diag(`do not pass a nil Context`)

(*GT[int]).Bar(nil, context.Background(), 0)
(*GT[int]).Bar(nil, context.TODO(), 0) //@ diag(`do not pass a nil Context`)
}
0