diff --git a/build/BUILD.bazel b/build/BUILD.bazel index 23cf263d525e3..3c2a569dd80d8 100644 --- a/build/BUILD.bazel +++ b/build/BUILD.bazel @@ -55,6 +55,7 @@ STATICHECK_ANALYZERS = [ "S1039", "S1040", "SA1019", + "SA1029", "SA2000", "SA2001", "SA2003", diff --git a/build/nogo_config.json b/build/nogo_config.json index 7621121e6035e..90a5b7cae853f 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -773,6 +773,13 @@ "meta": "meta code" } }, + "SA1029": { + "exclude_files": { + "/external/": "no need to vet third party code", + ".*_generated\\.go$": "ignore generated code", + ".*_test\\.go$": "ignore test code" + } + }, "SA2000": { "exclude_files": { "external/": "no need to vet third party code", diff --git a/executor/memtable_reader_test.go b/executor/memtable_reader_test.go index 870a4193fb3b2..f6d98d4ec24fc 100644 --- a/executor/memtable_reader_test.go +++ b/executor/memtable_reader_test.go @@ -29,6 +29,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/fn" "github.com/pingcap/sysutil" + "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/util/pdapi" pmodel "github.com/prometheus/common/model" @@ -56,7 +57,7 @@ func TestMetricTableData(t *testing.T) { } matrix = append(matrix, &pmodel.SampleStream{Metric: metric, Values: []pmodel.SamplePair{v1}}) - ctx := context.WithValue(context.Background(), "__mockMetricsPromData", matrix) + ctx := context.WithValue(context.Background(), executor.MockMetricsPromDataKey{}, matrix) ctx = failpoint.WithHook(ctx, func(ctx context.Context, fpname string) bool { return fpname == fpName }) diff --git a/executor/metrics_reader.go b/executor/metrics_reader.go index 314616785d60f..d9e0bd39f1128 100644 --- a/executor/metrics_reader.go +++ b/executor/metrics_reader.go @@ -89,9 +89,12 @@ func (e *MetricRetriever) retrieve(ctx context.Context, sctx sessionctx.Context) return totalRows, nil } +// MockMetricsPromDataKey is for test +type MockMetricsPromDataKey struct{} + func (e *MetricRetriever) queryMetric(ctx context.Context, sctx sessionctx.Context, queryRange promv1.Range, quantile float64) (result pmodel.Value, err error) { failpoint.InjectContext(ctx, "mockMetricsPromData", func() { - failpoint.Return(ctx.Value("__mockMetricsPromData").(pmodel.Matrix), nil) + failpoint.Return(ctx.Value(MockMetricsPromDataKey{}).(pmodel.Matrix), nil) }) // Add retry to avoid network error. diff --git a/executor/slow_query.go b/executor/slow_query.go index b83a480f85857..e8a2731a476e7 100644 --- a/executor/slow_query.go +++ b/executor/slow_query.go @@ -52,6 +52,8 @@ import ( "golang.org/x/exp/slices" ) +type signalsKey struct{} + // ParseSlowLogBatchSize is the batch size of slow-log lines for a worker to parse, exported for testing. var ParseSlowLogBatchSize = 64 @@ -474,7 +476,7 @@ func (e *slowQueryRetriever) parseSlowLog(ctx context.Context, sctx sessionctx.C } failpoint.Inject("mockReadSlowLogSlow", func(val failpoint.Value) { if val.(bool) { - signals := ctx.Value("signals").([]chan int) + signals := ctx.Value(signalsKey{}).([]chan int) signals[0] <- 1 <-signals[1] } diff --git a/executor/slow_query_test.go b/executor/slow_query_test.go index d696afa3c945d..fe2a5b68a329a 100644 --- a/executor/slow_query_test.go +++ b/executor/slow_query_test.go @@ -666,7 +666,7 @@ select * from t;` retriever, err := newSlowQueryRetriever() require.NoError(t, err) var signal1, signal2 = make(chan int, 1), make(chan int, 1) - ctx := context.WithValue(context.Background(), "signals", []chan int{signal1, signal2}) + ctx := context.WithValue(context.Background(), signalsKey{}, []chan int{signal1, signal2}) ctx, cancel := context.WithCancel(ctx) err = failpoint.Enable("github.com/pingcap/tidb/executor/mockReadSlowLogSlow", "return(true)") require.NoError(t, err) diff --git a/store/gcworker/gc_worker.go b/store/gcworker/gc_worker.go index a14780b878f19..f9b53e55988d4 100644 --- a/store/gcworker/gc_worker.go +++ b/store/gcworker/gc_worker.go @@ -1198,6 +1198,7 @@ func (w *GCWorker) resolveLocksForRange( failpoint.Inject("setGcResolveMaxBackoff", func(v failpoint.Value) { sleep := v.(int) // cooperate with github.com/tikv/client-go/v2/locate/invalidCacheAndRetry + //nolint: SA1029 ctx = context.WithValue(ctx, "injectedBackoff", struct{}{}) bo = tikv.NewBackofferWithVars(ctx, sleep, nil) }) diff --git a/ttl/metrics/metrics.go b/ttl/metrics/metrics.go index 8768b0e267388..3c8ceee213a14 100644 --- a/ttl/metrics/metrics.go +++ b/ttl/metrics/metrics.go @@ -133,16 +133,16 @@ func (t *PhaseTracer) EndPhase() { t.EnterPhase("") } -const ttlPhaseTraceKey = "ttlPhaseTraceKey" +type ttlPhaseTraceKey struct{} // CtxWithPhaseTracer create a new context with tracer func CtxWithPhaseTracer(ctx context.Context, tracer *PhaseTracer) context.Context { - return context.WithValue(ctx, ttlPhaseTraceKey, tracer) + return context.WithValue(ctx, ttlPhaseTraceKey{}, tracer) } // PhaseTracerFromCtx returns a tracer from a given context func PhaseTracerFromCtx(ctx context.Context) *PhaseTracer { - if tracer, ok := ctx.Value(ttlPhaseTraceKey).(*PhaseTracer); ok { + if tracer, ok := ctx.Value(ttlPhaseTraceKey{}).(*PhaseTracer); ok { return tracer } return nil