github.com/azazeal/revive@v1.0.9/testdata/golint/context-keys-type.go (about)

     1  // Package contextkeytypes verifies that correct types are used as keys in
     2  // calls to context.WithValue.
     3  package contextkeytypes
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  )
     9  
    10  type ctxKey struct{}
    11  
    12  func contextKeyTypeTests() {
    13  	fmt.Println()                               // not in package context
    14  	context.TODO()                              // wrong function
    15  	c := context.Background()                   // wrong function
    16  	context.WithValue(c, "foo", "bar")          // MATCH /should not use basic type string as key in context.WithValue/
    17  	context.WithValue(c, true, "bar")           // MATCH /should not use basic type bool as key in context.WithValue/
    18  	context.WithValue(c, 1, "bar")              // MATCH /should not use basic type int as key in context.WithValue/
    19  	context.WithValue(c, int8(1), "bar")        // MATCH /should not use basic type int8 as key in context.WithValue/
    20  	context.WithValue(c, int16(1), "bar")       // MATCH /should not use basic type int16 as key in context.WithValue/
    21  	context.WithValue(c, int32(1), "bar")       // MATCH /should not use basic type int32 as key in context.WithValue/
    22  	context.WithValue(c, rune(1), "bar")        // MATCH /should not use basic type rune as key in context.WithValue/
    23  	context.WithValue(c, int64(1), "bar")       // MATCH /should not use basic type int64 as key in context.WithValue/
    24  	context.WithValue(c, uint(1), "bar")        // MATCH /should not use basic type uint as key in context.WithValue/
    25  	context.WithValue(c, uint8(1), "bar")       // MATCH /should not use basic type uint8 as key in context.WithValue/
    26  	context.WithValue(c, byte(1), "bar")        // MATCH /should not use basic type byte as key in context.WithValue/
    27  	context.WithValue(c, uint16(1), "bar")      // MATCH /should not use basic type uint16 as key in context.WithValue/
    28  	context.WithValue(c, uint32(1), "bar")      // MATCH /should not use basic type uint32 as key in context.WithValue/
    29  	context.WithValue(c, uint64(1), "bar")      // MATCH /should not use basic type uint64 as key in context.WithValue/
    30  	context.WithValue(c, uintptr(1), "bar")     // MATCH /should not use basic type uintptr as key in context.WithValue/
    31  	context.WithValue(c, float32(1.0), "bar")   // MATCH /should not use basic type float32 as key in context.WithValue/
    32  	context.WithValue(c, float64(1.0), "bar")   // MATCH /should not use basic type float64 as key in context.WithValue/
    33  	context.WithValue(c, complex64(1i), "bar")  // MATCH /should not use basic type complex64 as key in context.WithValue/
    34  	context.WithValue(c, complex128(1i), "bar") // MATCH /should not use basic type complex128 as key in context.WithValue/
    35  	context.WithValue(c, ctxKey{}, "bar")       // ok
    36  	context.WithValue(c, &ctxKey{}, "bar")      // ok
    37  	context.WithValue(c, invalid{}, "bar")      // ok
    38  }