Skip to content

Commit

Permalink
Makefile: add staticcheck (pingcap#14420)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahjonp authored and sre-bot committed Jan 10, 2020
1 parent 2c256ba commit 2dcc032
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 24 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions executor/hash_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions executor/index_merge_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
2 changes: 1 addition & 1 deletion expression/partition_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion planner/core/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func compareBool(l, r bool) int {
if l == r {
return 0
}
if l == false {
if !l {
return -1
}
return 1
Expand Down
6 changes: 3 additions & 3 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
10 changes: 10 additions & 0 deletions staticcheck.conf
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 2 additions & 2 deletions store/tikv/2pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/coprocessor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2dcc032

Please sign in to comment.