diff --git a/rule/context-as-argument.go b/rule/context-as-argument.go index 0ed28a8..6502a07 100644 --- a/rule/context-as-argument.go +++ b/rule/context-as-argument.go @@ -45,16 +45,19 @@ func (w lintContextArguments) Visit(n ast.Node) ast.Visitor { } // A context.Context should be the first parameter of a function. // Flag any that show up after the first. + previousArgIsCtx := isPkgDot(fn.Type.Params.List[0].Type, "context", "Context") for _, arg := range fn.Type.Params.List[1:] { - if isPkgDot(arg.Type, "context", "Context") { + argIsCtx := isPkgDot(arg.Type, "context", "Context") + if argIsCtx && !previousArgIsCtx { w.onFailure(lint.Failure{ - Node: fn, + Node: arg, Category: "arg-order", Failure: "context.Context should be the first parameter of a function", Confidence: 0.9, }) break // only flag one } + previousArgIsCtx = argIsCtx } return w } diff --git a/testdata/golint/context-as-argument.go b/testdata/golint/context-as-argument.go index 5580e60..62e152c 100644 --- a/testdata/golint/context-as-argument.go +++ b/testdata/golint/context-as-argument.go @@ -22,3 +22,7 @@ func y(s string, ctx context.Context) { // MATCH /context.Context should be the // An invalid context.Context location with more than 2 args func y(s string, r int, ctx context.Context, x int) { // MATCH /context.Context should be the first parameter of a function/ } + +func y(ctx1 context.Context, ctx2 context.Context, x int) {} + +func y(ctx1 context.Context, ctx2 context.Context, x int, ctx3 context.Context) {} // MATCH /context.Context should be the first parameter of a function/