github.com/nozzle/golangci-lint@v1.49.0-nz3/test/testdata/contextcheck.go (about)

     1  //golangcitest:args -Econtextcheck
     2  package testdata
     3  
     4  import "context"
     5  
     6  type MyString string
     7  
     8  func contextcheckCase1(ctx context.Context) {
     9  	funcWithoutCtx() // want "Function `funcWithoutCtx` should pass the context parameter"
    10  }
    11  
    12  func contextcheckCase2(ctx context.Context) {
    13  	ctx = context.WithValue(ctx, MyString("aaa"), "aaaaaa")
    14  	funcWithCtx(ctx)
    15  
    16  	defer func() {
    17  		funcWithCtx(ctx)
    18  	}()
    19  
    20  	func(ctx context.Context) {
    21  		funcWithCtx(ctx)
    22  	}(ctx)
    23  
    24  	funcWithCtx(context.Background()) // want "Non-inherited new context, use function like `context.WithXXX` instead"
    25  }
    26  
    27  func contextcheckCase3(ctx context.Context) {
    28  	func() {
    29  		funcWithCtx(ctx)
    30  	}()
    31  
    32  	ctx = context.Background() // want "Non-inherited new context, use function like `context.WithXXX` instead"
    33  	funcWithCtx(ctx)
    34  }
    35  
    36  func contextcheckCase4(ctx context.Context) {
    37  	ctx, cancel := getNewCtx(ctx)
    38  	defer cancel()
    39  	funcWithCtx(ctx)
    40  }
    41  
    42  func funcWithCtx(ctx context.Context) {}
    43  
    44  func funcWithoutCtx() {
    45  	funcWithCtx(context.TODO())
    46  }
    47  
    48  func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
    49  	return context.WithCancel(ctx)
    50  }