1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/testdata/context-as-argument.go
Euan Kemp af953e6189
Allow whitelist for the context parameter check (#616)
* Allow a whitelist for the context parameter check

This allows users to configure a set of types that may appear before
`context.Context`.

Notably, I think this rule is useful for allowing the `*testing.T` type
to come before `context.Context`, though there may be other uses (such
as putting a tracer before it, etc).

See #605 for a little more context on this.

Fixes #605

* Save a level of indentation in context-as-arg validation

We can unindent if we make the above check more specific

* refactoring taking into account chavacava's review

Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
2021-12-31 17:11:18 -08:00

55 lines
1.6 KiB
Go

// Test that context.Context is the first arg to a function.
// Package foo ...
package foo
import (
"context"
"testing"
)
// AllowedBeforeType is a type that is configured to be allowed before context.Context
type AllowedBeforeType string
// AllowedBeforeStruct is a type that is configured to be allowed before context.Context
type AllowedBeforeStruct struct{}
// AllowedBeforePtrStruct is a type that is configured to be allowed before context.Context
type AllowedBeforePtrStruct struct{}
// A proper context.Context location
func x(ctx context.Context) { // ok
}
// A proper context.Context location
func x(ctx context.Context, s string) { // ok
}
// *testing.T is permitted in the linter config for the test
func x(t *testing.T, ctx context.Context) { // ok
}
func x(_ AllowedBeforeType, _ AllowedBeforeType, ctx context.Context) { // ok
}
func x(_, _ AllowedBeforeType, ctx context.Context) { // ok
}
func x(_ *AllowedBeforePtrStruct, ctx context.Context) { // ok
}
func x(_ AllowedBeforePtrStruct, ctx context.Context) { // MATCH /context.Context should be the first parameter of a function/
}
// An invalid context.Context location
func y(s string, ctx context.Context) { // MATCH /context.Context should be the first parameter of a function/
}
// 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/