From 2dcc032bc36d89a726a77d9d6892ff38a48139d7 Mon Sep 17 00:00:00 2001 From: mahjonp Date: Fri, 10 Jan 2020 14:59:43 +0800 Subject: [PATCH] Makefile: add staticcheck (#14420) --- Makefile | 7 ++++++- ddl/column_test.go | 4 ++-- ddl/db_test.go | 2 +- executor/hash_table.go | 3 +-- executor/index_merge_reader.go | 3 +-- expression/bench_test.go | 3 +-- expression/partition_pruner.go | 2 +- planner/core/encode.go | 2 +- planner/core/exhaust_physical_plans.go | 8 ++++---- planner/core/find_best_task.go | 2 +- planner/core/rule_partition_processor.go | 6 +++--- staticcheck.conf | 10 ++++++++++ store/tikv/2pc_test.go | 4 ++-- store/tikv/coprocessor_cache.go | 2 +- util/stmtsummary/statement_summary.go | 2 +- 15 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 staticcheck.conf diff --git a/Makefile b/Makefile index b5e0dcaf237c4..2f0f3e9482e47 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ GOBUILD := $(GO) build $(BUILD_FLAG) -tags codes GOBUILDCOVERAGE := GOPATH=$(GOPATH) cd tidb-server; $(GO) test -coverpkg="../..." -c . GOTEST := $(GO) test -p $(P) OVERALLS := GO111MODULE=on overalls +STATICCHECK := GO111MODULE=on staticcheck ARCH := "`uname -s`" LINUX := "Linux" @@ -81,7 +82,7 @@ build: # Install the check tools. check-setup:tools/bin/revive tools/bin/goword tools/bin/gometalinter tools/bin/gosec -check: fmt errcheck lint tidy testSuite check-static vet +check: fmt errcheck lint tidy testSuite check-static vet staticcheck # These need to be fixed before they can be ran regularly check-fail: goword check-slow @@ -123,6 +124,10 @@ vet: @echo "vet" $(GO) vet -all $(PACKAGES) 2>&1 | $(FAIL_ON_STDOUT) +staticcheck: + $(GO) get honnef.co/go/tools/cmd/staticcheck + $(STATICCHECK) ./... + tidy: @echo "go mod tidy" ./tools/check/check-tidy.sh diff --git a/ddl/column_test.go b/ddl/column_test.go index 16f682543950e..e0dc8fe40a7bd 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -630,7 +630,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d return nil } -func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblInfo *model.TableInfo, handle int64, newCol *table.Column, oldRow []types.Datum, columnValue interface{}) error { +func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblInfo *model.TableInfo, _ int64, newCol *table.Column, oldRow []types.Datum, columnValue interface{}) error { t, err := testGetTableWithError(d, s.dbInfo.ID, tblInfo.ID) if err != nil { return errors.Trace(err) @@ -663,7 +663,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI } newRow := types.MakeDatums(int64(11), int64(22), int64(33), int64(44)) - handle, err = t.AddRecord(ctx, newRow) + handle, err := t.AddRecord(ctx, newRow) if err != nil { return errors.Trace(err) } diff --git a/ddl/db_test.go b/ddl/db_test.go index ed936cf5277b8..1932007036f03 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -2228,7 +2228,7 @@ func (s *testDBSuite5) TestRepairTable(c *C) { } func turnRepairModeAndInit(on bool) { - list := make([]string, 0, 0) + list := make([]string, 0) if on { list = append(list, "test.origin") } diff --git a/executor/hash_table.go b/executor/hash_table.go index 5b43876380cf2..709d924685ff0 100644 --- a/executor/hash_table.go +++ b/executor/hash_table.go @@ -162,8 +162,7 @@ func (c *hashRowContainer) PutChunk(chk *chunk.Chunk) error { // key of hash table: hash value of key columns // value of hash table: RowPtr of the corresponded row func (c *hashRowContainer) PutChunkSelected(chk *chunk.Chunk, selected []bool) error { - var chkIdx uint32 - chkIdx = uint32(c.rowContainer.NumChunks()) + chkIdx := uint32(c.rowContainer.NumChunks()) err := c.rowContainer.Add(chk) if err != nil { return err diff --git a/executor/index_merge_reader.go b/executor/index_merge_reader.go index 49b53307712c6..491f21696520a 100644 --- a/executor/index_merge_reader.go +++ b/executor/index_merge_reader.go @@ -544,8 +544,7 @@ func (w *partialIndexWorker) fetchHandles(ctx context.Context, result distsql.Se } } }() - var chk *chunk.Chunk - chk = chunk.NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(mysql.TypeLonglong)}, w.maxChunkSize) + chk := chunk.NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(mysql.TypeLonglong)}, w.maxChunkSize) for { handles, retChunk, err := w.extractTaskHandles(ctx, chk, result) if err != nil { diff --git a/expression/bench_test.go b/expression/bench_test.go index ef2a9334d7519..17d46a3c79f30 100644 --- a/expression/bench_test.go +++ b/expression/bench_test.go @@ -1256,8 +1256,7 @@ func testVectorizedBuiltinFunc(c *C, vecExprCases vecExprBenchCases) { c.Assert(err, IsNil, commentf(i)) c.Assert(isNull, Equals, output.IsNull(i), commentf(i)) if !isNull { - var cmp int - cmp = json.CompareBinary(val, output.GetJSON(i)) + cmp := json.CompareBinary(val, output.GetJSON(i)) c.Assert(cmp, Equals, 0, commentf(i)) } i++ diff --git a/expression/partition_pruner.go b/expression/partition_pruner.go index f380844d9e7a8..6e7c747faaa25 100644 --- a/expression/partition_pruner.go +++ b/expression/partition_pruner.go @@ -163,7 +163,7 @@ func (p *hashPartitionPruner) solve(ctx sessionctx.Context, conds []Expression, for _, col := range ExtractColumns(piExpr) { p.insertCol(col) } - p.constantMap = make([]*Constant, p.numColumn, p.numColumn) + p.constantMap = make([]*Constant, p.numColumn) conflict := p.reduceConstantEQ() if conflict { return 0, false, conflict diff --git a/planner/core/encode.go b/planner/core/encode.go index 54b1a57e19e98..ca8ee7bcabb5f 100644 --- a/planner/core/encode.go +++ b/planner/core/encode.go @@ -108,7 +108,7 @@ func NormalizePlan(p Plan) (normalized, digest string) { d := digesterPool.Get().(*planDigester) defer digesterPool.Put(d) d.normalizePlanTree(selectPlan) - normalized = string(d.buf.Bytes()) + normalized = d.buf.String() d.hasher.Write(d.buf.Bytes()) d.buf.Reset() digest = fmt.Sprintf("%x", d.hasher.Sum(nil)) diff --git a/planner/core/exhaust_physical_plans.go b/planner/core/exhaust_physical_plans.go index d17d8adc7d946..6e1892c4a2b4f 100644 --- a/planner/core/exhaust_physical_plans.go +++ b/planner/core/exhaust_physical_plans.go @@ -1174,13 +1174,13 @@ func (ijHelper *indexJoinBuildHelper) buildTemplateRange(matchedKeyCnt int, eqAn } else if haveExtraCol { // Reserve a position for the last col. ranges = append(ranges, &ranger.Range{ - LowVal: make([]types.Datum, pointLength+1, pointLength+1), - HighVal: make([]types.Datum, pointLength+1, pointLength+1), + LowVal: make([]types.Datum, pointLength+1), + HighVal: make([]types.Datum, pointLength+1), }) } else { ranges = append(ranges, &ranger.Range{ - LowVal: make([]types.Datum, pointLength, pointLength), - HighVal: make([]types.Datum, pointLength, pointLength), + LowVal: make([]types.Datum, pointLength), + HighVal: make([]types.Datum, pointLength), }) } sc := ijHelper.join.ctx.GetSessionVars().StmtCtx diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index c91f863a606b6..6a56870f89565 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -249,7 +249,7 @@ func compareBool(l, r bool) int { if l == r { return 0 } - if l == false { + if !l { return -1 } return 1 diff --git a/planner/core/rule_partition_processor.go b/planner/core/rule_partition_processor.go index f0dcf5d96943c..79519d5dd0b48 100644 --- a/planner/core/rule_partition_processor.go +++ b/planner/core/rule_partition_processor.go @@ -154,7 +154,7 @@ func (s *partitionProcessor) pruneHashPartition(ds *DataSource, pi *model.Partit // Constant false. if con, ok := filterConds[0].(*expression.Constant); ok && con.DeferredExpr == nil && con.ParamMarker == nil { ret, _, err := expression.EvalBool(sctx, expression.CNFExprs{con}, chunk.Row{}) - if err == nil && ret == false { + if err == nil && !ret { alwaysFalse = true } } @@ -234,7 +234,7 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) { // Constant false. if con, ok := filterConds[0].(*expression.Constant); ok && con.DeferredExpr == nil && con.ParamMarker == nil { ret, _, err := expression.EvalBool(sctx, expression.CNFExprs{con}, chunk.Row{}) - if err == nil && ret == false { + if err == nil && !ret { alwaysFalse = true } } @@ -313,7 +313,7 @@ func (s *partitionProcessor) canBePruned(sctx sessionctx.Context, partCol *expre // Constant false. if con, ok := conds[0].(*expression.Constant); ok && con.DeferredExpr == nil && con.ParamMarker == nil { ret, _, err := expression.EvalBool(sctx, expression.CNFExprs{con}, chunk.Row{}) - if err == nil && ret == false { + if err == nil && !ret { return true, nil } } diff --git a/staticcheck.conf b/staticcheck.conf new file mode 100644 index 0000000000000..28941ad3bcd75 --- /dev/null +++ b/staticcheck.conf @@ -0,0 +1,10 @@ +checks = ["S1002","S1004","S1007","S1009","S1010","S1012","S1019","S1020","S1021","S1024","S1030","SA2*","SA3*","SA4009","SA5*","SA6000","SA6001","SA6005", "-SA2002"] +initialisms = ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", + "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", + "IP", "JSON", "QPS", "RAM", "RPC", "SLA", + "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", + "UDP", "UI", "GID", "UID", "UUID", "URI", + "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", + "XSS"] +dot_import_whitelist = [] +http_status_code_whitelist = ["200", "400", "404", "500"] diff --git a/store/tikv/2pc_test.go b/store/tikv/2pc_test.go index 4f897ca6114d1..688180e538859 100644 --- a/store/tikv/2pc_test.go +++ b/store/tikv/2pc_test.go @@ -673,7 +673,7 @@ func (s *testCommitterSuite) TestAcquireFalseTimeoutLock(c *C) { lockCtx = &kv.LockCtx{ForUpdateTS: txn2.startTS, LockWaitTime: kv.LockNoWait, WaitStartTime: time.Now()} startTime := time.Now() err = txn2.LockKeys(context.Background(), lockCtx, k2) - elapsed := time.Now().Sub(startTime) + elapsed := time.Since(startTime) // cannot acquire lock immediately thus error c.Assert(err.Error(), Equals, ErrLockAcquireFailAndNoWaitSet.Error()) // it should return immediately @@ -683,7 +683,7 @@ func (s *testCommitterSuite) TestAcquireFalseTimeoutLock(c *C) { lockCtx = &kv.LockCtx{ForUpdateTS: txn2.startTS, LockWaitTime: 300, WaitStartTime: time.Now()} startTime = time.Now() err = txn2.LockKeys(context.Background(), lockCtx, k2) - elapsed = time.Now().Sub(startTime) + elapsed = time.Since(startTime) // cannot acquire lock in time thus error c.Assert(err.Error(), Equals, ErrLockWaitTimeout.Error()) // it should return after about 300ms diff --git a/store/tikv/coprocessor_cache.go b/store/tikv/coprocessor_cache.go index f5b13d41f8afb..318fef494e0a5 100644 --- a/store/tikv/coprocessor_cache.go +++ b/store/tikv/coprocessor_cache.go @@ -152,7 +152,7 @@ func (c *coprCache) Get(key []byte) *coprCacheValue { } typedValue := value.(*coprCacheValue) // ristretto does not handle hash collision, so check the key equality after getting a value. - if bytes.Compare(typedValue.Key, key) != 0 { + if !bytes.Equal(typedValue.Key, key) { return nil } return typedValue diff --git a/util/stmtsummary/statement_summary.go b/util/stmtsummary/statement_summary.go index 280cb3e90ed11..63cf12231c623 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -608,7 +608,7 @@ func newStmtSummaryByDigestElement(sei *StmtExecInfo, beginTime int64, intervalS minLatency: sei.TotalLatency, firstSeen: sei.StartTime, lastSeen: sei.StartTime, - backoffTypes: make(map[fmt.Stringer]int, 0), + backoffTypes: make(map[fmt.Stringer]int), } ssElement.add(sei, intervalSeconds) return ssElement