github.com/songshiyun/revive@v1.1.5-0.20220323112655-f8433a19b3c5/testdata/context-as-argument.go (about)

     1  // Test that context.Context is the first arg to a function.
     2  
     3  // Package foo ...
     4  package foo
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  )
    10  
    11  // AllowedBeforeType is a type that is configured to be allowed before context.Context
    12  type AllowedBeforeType string
    13  
    14  // AllowedBeforeStruct is a type that is configured to be allowed before context.Context
    15  type AllowedBeforeStruct struct{}
    16  
    17  // AllowedBeforePtrStruct is a type that is configured to be allowed before context.Context
    18  type AllowedBeforePtrStruct struct{}
    19  
    20  // A proper context.Context location
    21  func x(ctx context.Context) { // ok
    22  }
    23  
    24  // A proper context.Context location
    25  func x(ctx context.Context, s string) { // ok
    26  }
    27  
    28  // *testing.T is permitted in the linter config for the test
    29  func x(t *testing.T, ctx context.Context) { // ok
    30  }
    31  
    32  func x(_ AllowedBeforeType, _ AllowedBeforeType, ctx context.Context) { // ok
    33  }
    34  
    35  func x(_, _ AllowedBeforeType, ctx context.Context) { // ok
    36  }
    37  
    38  func x(_ *AllowedBeforePtrStruct, ctx context.Context) { // ok
    39  }
    40  
    41  func x(_ AllowedBeforePtrStruct, ctx context.Context) { // MATCH /context.Context should be the first parameter of a function/
    42  }
    43  
    44  // An invalid context.Context location
    45  func y(s string, ctx context.Context) { // MATCH /context.Context should be the first parameter of a function/
    46  }
    47  
    48  // An invalid context.Context location with more than 2 args
    49  func y(s string, r int, ctx context.Context, x int) { // MATCH /context.Context should be the first parameter of a function/
    50  }
    51  
    52  func y(ctx1 context.Context, ctx2 context.Context, x int) {}
    53  
    54  func y(ctx1 context.Context, ctx2 context.Context, x int, ctx3 context.Context) {} // MATCH /context.Context should be the first parameter of a function/