diff --git a/.golangci.yml b/.golangci.yml index dddccb2f25e39..a4a087333ed3c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,6 +21,8 @@ linters: - exportloopref - rowserrcheck - unconvert + - makezero + linters-settings: staticcheck: checks: ["S1002","S1004","S1007","S1009","S1010","S1012","S1019","S1020","S1021","S1024","S1030","SA2*","SA3*","SA4009","SA5*","SA6000","SA6001","SA6005", "-SA2002"] @@ -36,3 +38,4 @@ issues: - errcheck - gosec - rowserrcheck + - makezero diff --git a/executor/index_lookup_hash_join.go b/executor/index_lookup_hash_join.go index b77e446c62104..755c0713f13f8 100644 --- a/executor/index_lookup_hash_join.go +++ b/executor/index_lookup_hash_join.go @@ -759,7 +759,7 @@ func (iw *indexHashJoinInnerWorker) doJoinInOrder(ctx context.Context, task *ind } } // TODO: matchedInnerRowPtrs and matchedInnerRows can be moved to inner worker. - matchedInnerRows := make([]chunk.Row, len(task.matchedInnerRowPtrs)) + matchedInnerRows := make([]chunk.Row, 0, len(task.matchedInnerRowPtrs)) var hasMatched, hasNull, ok bool for chkIdx, innerRowPtrs4Chk := range task.matchedInnerRowPtrs { for outerRowIdx, innerRowPtrs := range innerRowPtrs4Chk { diff --git a/executor/slow_query.go b/executor/slow_query.go index 09f3b2b6df41d..0b57d1fc55e6d 100755 --- a/executor/slow_query.go +++ b/executor/slow_query.go @@ -264,7 +264,7 @@ func getOneLine(reader *bufio.Reader) ([]byte, error) { var tempLine []byte for isPrefix { tempLine, isPrefix, err = reader.ReadLine() - resByte = append(resByte, tempLine...) + resByte = append(resByte, tempLine...) // nozero // Use the max value of max_allowed_packet to check the single line length. if len(resByte) > int(variable.MaxOfMaxAllowedPacket) { return resByte, errors.Errorf("single line length exceeds limit: %v", variable.MaxOfMaxAllowedPacket) @@ -1050,7 +1050,7 @@ func readLastLines(ctx context.Context, file *os.File, endCursor int64) ([]strin if err != nil { return nil, 0, err } - lines = append(chars, lines...) + lines = append(chars, lines...) // nozero // find first '\n' or '\r' for i := 0; i < len(chars); i++ { diff --git a/kv/key.go b/kv/key.go index 191bc65eb7ac3..ae75765535331 100644 --- a/kv/key.go +++ b/kv/key.go @@ -59,7 +59,7 @@ func (k Key) PrefixNext() Key { } if i == -1 { copy(buf, k) - buf = append(buf, 0) + buf = append(buf, 0) // nozero } return buf } diff --git a/planner/cascades/transformation_rules.go b/planner/cascades/transformation_rules.go index 14cf33a6410d5..6a0d12eb977c0 100644 --- a/planner/cascades/transformation_rules.go +++ b/planner/cascades/transformation_rules.go @@ -920,7 +920,7 @@ func (r *pushDownJoin) predicatePushDown( leftCond = append(join.LeftConditions, derivedLeftJoinCond...) join.LeftConditions = nil remainCond = append(expression.ScalarFuncs2Exprs(equalCond), otherCond...) - remainCond = append(remainCond, leftPushCond...) + remainCond = append(remainCond, leftPushCond...) // nozero } else { remainCond = expression.ExtractFiltersFromDNFs(join.SCtx(), remainCond) // Only derive left where condition, because right where condition cannot be pushed down @@ -931,7 +931,7 @@ func (r *pushDownJoin) predicatePushDown( rightCond = append(join.RightConditions, derivedRightJoinCond...) join.RightConditions = nil remainCond = append(expression.ScalarFuncs2Exprs(equalCond), otherCond...) - remainCond = append(remainCond, rightPushCond...) + remainCond = append(remainCond, rightPushCond...) // nozero } default: // TODO: Enhance this rule to deal with Semi/SmiAnti Joins. diff --git a/planner/core/exhaust_physical_plans.go b/planner/core/exhaust_physical_plans.go index b6b83e5ce598d..575a177697325 100644 --- a/planner/core/exhaust_physical_plans.go +++ b/planner/core/exhaust_physical_plans.go @@ -482,11 +482,11 @@ func (p *LogicalJoin) constructIndexJoin( } outerSchema, innerSchema := p.Children()[outerIdx].Schema(), p.Children()[1-outerIdx].Schema() if outerSchema.Contains(lhs) && innerSchema.Contains(rhs) { - outerHashKeys = append(outerHashKeys, lhs) - innerHashKeys = append(innerHashKeys, rhs) + outerHashKeys = append(outerHashKeys, lhs) // nozero + innerHashKeys = append(innerHashKeys, rhs) // nozero } else if innerSchema.Contains(lhs) && outerSchema.Contains(rhs) { - outerHashKeys = append(outerHashKeys, rhs) - innerHashKeys = append(innerHashKeys, lhs) + outerHashKeys = append(outerHashKeys, rhs) // nozero + innerHashKeys = append(innerHashKeys, lhs) // nozero } newOtherConds = append(newOtherConds[:i], newOtherConds[i+1:]...) } @@ -1203,7 +1203,7 @@ func (cwc *ColWithCmpFuncManager) BuildRangesByRow(ctx sessionctx.Context, row c if err != nil { return nil, err } - exprs = append(exprs, newExpr) + exprs = append(exprs, newExpr) // nozero } ranges, err := ranger.BuildColumnRange(exprs, ctx.GetSessionVars().StmtCtx, cwc.TargetCol.RetType, cwc.colLength) if err != nil { diff --git a/planner/core/physical_plans.go b/planner/core/physical_plans.go index 01f5ada33df88..52dd75268fec0 100644 --- a/planner/core/physical_plans.go +++ b/planner/core/physical_plans.go @@ -579,13 +579,13 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema, for _, baseCol := range baseCols { if !schema.Contains(baseCol) { schema.Columns = append(schema.Columns, baseCol) - copyColumn = append(copyColumn, FindColumnInfoByID(colsInfo, baseCol.ID)) + copyColumn = append(copyColumn, FindColumnInfoByID(colsInfo, baseCol.ID)) // nozero } } } if extraColumn != nil { schema.Columns = append(schema.Columns, extraColumn) - copyColumn = append(copyColumn, extraColumnModel) + copyColumn = append(copyColumn, extraColumnModel) // nozero } return copyColumn } diff --git a/planner/util/path.go b/planner/util/path.go index 4742a42bc3f79..8c4abd6a94ab5 100644 --- a/planner/util/path.go +++ b/planner/util/path.go @@ -103,7 +103,7 @@ func (path *AccessPath) SplitCorColAccessCondFromFilters(ctx sessionctx.Context, } for i, ok := range used { if !ok { - remained = append(remained, path.TableFilters[i]) + remained = append(remained, path.TableFilters[i]) // nozero } } return access, remained diff --git a/store/gcworker/gc_worker.go b/store/gcworker/gc_worker.go index ac47930818262..1a8da7c96ad05 100644 --- a/store/gcworker/gc_worker.go +++ b/store/gcworker/gc_worker.go @@ -1115,9 +1115,9 @@ retryScanAndResolve: return stat, errors.Errorf("unexpected scanlock error: %s", locksResp) } locksInfo := locksResp.GetLocks() - locks := make([]*txnlock.Lock, len(locksInfo)) - for i := range locksInfo { - locks[i] = txnlock.NewLock(locksInfo[i]) + locks := make([]*txnlock.Lock, 0, len(locksInfo)) + for _, li := range locksInfo { + locks = append(locks, txnlock.NewLock(li)) } if w.testingKnobs.scanLocks != nil { locks = append(locks, w.testingKnobs.scanLocks(key, loc.Region.GetID())...) diff --git a/store/mockstore/unistore/tikv/mvcc/tikv.go b/store/mockstore/unistore/tikv/mvcc/tikv.go index ef12e2f9eadb1..7b8a52dfd9159 100644 --- a/store/mockstore/unistore/tikv/mvcc/tikv.go +++ b/store/mockstore/unistore/tikv/mvcc/tikv.go @@ -70,8 +70,8 @@ const ( // EncodeWriteCFValue accepts a write cf parameters and return the encoded bytes data. // Just like the tikv encoding form. See tikv/src/storage/mvcc/write.rs for more detail. func EncodeWriteCFValue(t WriteType, startTs uint64, shortVal []byte) []byte { - data := make([]byte, 1) - data[0] = t + data := make([]byte, 0) + data = append(data, t) data = codec.EncodeUvarint(data, startTs) if len(shortVal) != 0 { data = append(data, byte(shortValuePrefix), byte(len(shortVal))) @@ -82,16 +82,16 @@ func EncodeWriteCFValue(t WriteType, startTs uint64, shortVal []byte) []byte { // EncodeLockCFValue encodes the mvcc lock and returns putLock value and putDefault value if exists. func EncodeLockCFValue(lock *Lock) ([]byte, []byte) { - data := make([]byte, 1) + data := make([]byte, 0) switch lock.Op { case byte(kvrpcpb.Op_Put): - data[0] = LockTypePut + data = append(data, LockTypePut) case byte(kvrpcpb.Op_Del): - data[0] = LockTypeDelete + data = append(data, LockTypeDelete) case byte(kvrpcpb.Op_Lock): - data[0] = LockTypeLock + data = append(data, LockTypeLock) case byte(kvrpcpb.Op_PessimisticLock): - data[0] = LockTypePessimistic + data = append(data, LockTypePessimistic) default: panic("invalid lock op") } diff --git a/tablecodec/tablecodec.go b/tablecodec/tablecodec.go index 48b4eda5410ee..f610e09104572 100644 --- a/tablecodec/tablecodec.go +++ b/tablecodec/tablecodec.go @@ -1159,7 +1159,8 @@ func TryGetCommonPkColumnRestoredIds(tbl *model.TableInfo) []int64 { // GenIndexValueForClusteredIndexVersion1 generates the index value for the clustered index with version 1(New in v5.0.0). func GenIndexValueForClusteredIndexVersion1(sc *stmtctx.StatementContext, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, IdxValNeedRestoredData bool, distinct bool, untouched bool, indexedValues []types.Datum, h kv.Handle, partitionID int64, handleRestoredData []types.Datum) ([]byte, error) { - idxVal := make([]byte, 1) + idxVal := make([]byte, 0) + idxVal = append(idxVal, 0) tailLen := 0 // Version info. idxVal = append(idxVal, IndexVersionFlag) @@ -1216,7 +1217,8 @@ func GenIndexValueForClusteredIndexVersion1(sc *stmtctx.StatementContext, tblInf // genIndexValueVersion0 create index value for both local and global index. func genIndexValueVersion0(sc *stmtctx.StatementContext, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, IdxValNeedRestoredData bool, distinct bool, untouched bool, indexedValues []types.Datum, h kv.Handle, partitionID int64) ([]byte, error) { - idxVal := make([]byte, 1) + idxVal := make([]byte, 0) + idxVal = append(idxVal, 0) newEncode := false tailLen := 0 if !h.IsInt() && distinct {