Skip to content

Commit 97ef50d

Browse files
authored
fix 525 (#526)
1 parent 052235c commit 97ef50d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

rule/context-as-argument.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ func (w lintContextArguments) Visit(n ast.Node) ast.Visitor {
4545
}
4646
// A context.Context should be the first parameter of a function.
4747
// Flag any that show up after the first.
48+
previousArgIsCtx := isPkgDot(fn.Type.Params.List[0].Type, "context", "Context")
4849
for _, arg := range fn.Type.Params.List[1:] {
49-
if isPkgDot(arg.Type, "context", "Context") {
50+
argIsCtx := isPkgDot(arg.Type, "context", "Context")
51+
if argIsCtx && !previousArgIsCtx {
5052
w.onFailure(lint.Failure{
51-
Node: fn,
53+
Node: arg,
5254
Category: "arg-order",
5355
Failure: "context.Context should be the first parameter of a function",
5456
Confidence: 0.9,
5557
})
5658
break // only flag one
5759
}
60+
previousArgIsCtx = argIsCtx
5861
}
5962
return w
6063
}

testdata/golint/context-as-argument.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ func y(s string, ctx context.Context) { // MATCH /context.Context should be the
2222
// An invalid context.Context location with more than 2 args
2323
func y(s string, r int, ctx context.Context, x int) { // MATCH /context.Context should be the first parameter of a function/
2424
}
25+
26+
func y(ctx1 context.Context, ctx2 context.Context, x int) {}
27+
28+
func y(ctx1 context.Context, ctx2 context.Context, x int, ctx3 context.Context) {} // MATCH /context.Context should be the first parameter of a function/

0 commit comments

Comments
 (0)