Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: enable staticcheck and prealloc on nogo #35486

Merged
merged 9 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:prefix github.com/pingcap/tidb
# gazelle:exclude tidb-binlog/proto/proto
# gazelle:exclude plugin/conn_ip_example
# gazelle:exclude build/linter/staticcheck
gazelle(name = "gazelle")

package(default_visibility = ["//visibility:public"])
Expand Down
33 changes: 25 additions & 8 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ def go_deps():
name = "co_honnef_go_tools",
build_file_proto_mode = "disable_global",
importpath = "honnef.co/go/tools",
sum = "h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8=",
version = "v0.0.1-2020.1.4",
replace = "honnef.co/go/tools",
sum = "h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34=",
version = "v0.3.2",
)
go_repository(
name = "com_github_ajg_form",
Expand Down Expand Up @@ -174,8 +175,8 @@ def go_deps():
name = "com_github_burntsushi_toml",
build_file_proto_mode = "disable_global",
importpath = "github.com/BurntSushi/toml",
sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=",
version = "v0.3.1",
sum = "h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=",
version = "v0.4.1",
)
go_repository(
name = "com_github_burntsushi_xgb",
Expand Down Expand Up @@ -918,6 +919,14 @@ def go_deps():
sum = "h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=",
version = "v0.0.4",
)
go_repository(
name = "com_github_golangci_prealloc",
build_file_proto_mode = "disable",
importpath = "github.com/golangci/prealloc",
sum = "h1:leSNB7iYzLYSSx3J/s5sVf4Drkc68W2wm4Ixh/mr0us=",
version = "v0.0.0-20180630174525-215b22d4de21",
)

go_repository(
name = "com_github_gomodule_redigo",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -2928,6 +2937,14 @@ def go_deps():
sum = "h1:rxKZ2gOnYxjfmakvUUqh9Gyb6KXfrj7JWTxORTYqb0E=",
version = "v0.0.0-20220426173459-3bcf042a4bf5",
)
go_repository(
name = "org_golang_x_exp_typeparams",
build_file_proto_mode = "disable",
importpath = "golang.org/x/exp/typeparams",
sum = "h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=",
version = "v0.0.0-20220218215828-6cf2b201936e",
)

go_repository(
name = "org_golang_x_image",
build_file_proto_mode = "disable_global",
Expand All @@ -2953,8 +2970,8 @@ def go_deps():
name = "org_golang_x_mod",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/mod",
sum = "h1:LQmS1nU0twXLA96Kt7U9qtHJEbBk3z6Q0V4UXjZkpr4=",
version = "v0.6.0-dev.0.20211013180041-c96bc1413d57",
sum = "h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=",
version = "v0.6.0-dev.0.20220419223038-86c51ed26bb4",
)
go_repository(
name = "org_golang_x_net",
Expand Down Expand Up @@ -3009,8 +3026,8 @@ def go_deps():
name = "org_golang_x_tools",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/tools",
sum = "h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=",
version = "v0.1.8",
sum = "h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg=",
version = "v0.1.11-0.20220513221640-090b14e8501f",
)
go_repository(
name = "org_golang_x_xerrors",
Expand Down
1 change: 1 addition & 0 deletions bindinfo/bind_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (c *bindCache) GetAllBindRecords() []*BindRecord {
c.lock.Lock()
defer c.lock.Unlock()
values := c.cache.Values()
//nolint: prealloc
var bindRecords []*BindRecord
for _, vals := range values {
bindRecords = append(bindRecords, vals.([]*BindRecord)...)
Expand Down
2 changes: 1 addition & 1 deletion br/cmd/tidb-lightning-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func checkpointDump(ctx context.Context, cfg *config.Config, dumpFolder string)
}

func getLocalStoringTables(ctx context.Context, cfg *config.Config) (err2 error) {
//nolint:prealloc // This is a placeholder.
//nolint: prealloc
var tables []string
defer func() {
if err2 == nil {
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/lightning/backend/kv/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (kvcodec *tableKVEncoder) Encode(

var value types.Datum
var err error
//nolint:prealloc // This is a placeholder.
//nolint: prealloc
var record []types.Datum

if kvcodec.recordCache != nil {
Expand Down
11 changes: 7 additions & 4 deletions br/pkg/lightning/backend/local/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func tableHandleKeyRanges(tableInfo *model.TableInfo) ([]tidbkv.KeyRange, error)
// tableIndexKeyRanges returns all key ranges associated with the tableInfo and indexInfo.
func tableIndexKeyRanges(tableInfo *model.TableInfo, indexInfo *model.IndexInfo) ([]tidbkv.KeyRange, error) {
tableIDs := physicalTableIDs(tableInfo)
//nolint: prealloc
var keyRanges []tidbkv.KeyRange
for _, tid := range tableIDs {
partitionKeysRanges, err := distsql.IndexRangesToKVRanges(nil, tid, indexInfo.ID, ranger.FullRange(), nil)
Expand Down Expand Up @@ -606,6 +607,7 @@ func (m *DuplicateManager) buildLocalDupTasks(dupDB *pebble.DB, keyAdapter KeyAd
if err != nil {
return nil, errors.Trace(err)
}
//nolint: prealloc
var newTasks []dupTask
for _, task := range tasks {
// FIXME: Do not hardcode sizeLimit and keysLimit.
Expand Down Expand Up @@ -706,10 +708,11 @@ func (m *DuplicateManager) processRemoteDupTaskOnce(
regionPool *utils.WorkerPool,
remainKeyRanges *pendingKeyRanges,
) (madeProgress bool, err error) {
var (
regions []*restore.RegionInfo
keyRanges []tidbkv.KeyRange
)
//nolint: prealloc
var regions []*restore.RegionInfo
//nolint: prealloc
var keyRanges []tidbkv.KeyRange

for _, kr := range remainKeyRanges.list() {
subRegions, subKeyRanges, err := m.splitKeyRangeByRegions(ctx, kr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/lightning/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func TestInvalidTOML(t *testing.T) {
delimiter = '\'
backslash-escape = true
`))
require.EqualError(t, err, "Near line 0 (last key parsed ''): bare keys cannot contain '['")
require.EqualError(t, err, "Near line 2 (last key parsed ''): expected '.' or '=', but got '[' instead")
}

func TestTOMLUnusedKeys(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions br/pkg/lightning/errormanager/errormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func (em *ErrorManager) ResolveAllConflictKeys(

go func() {
//nolint:staticcheck
//lint:ignore SA2000
taskWg.Add(1)
taskCh <- [2]int64{0, math.MaxInt64}
taskWg.Wait()
Expand Down
39 changes: 38 additions & 1 deletion build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_go//go:def.bzl", "nogo")
load("//build/linter/staticcheck:def.bzl", "staticcheck_analyzers")

STATICHECK_ANALYZERS = [
"S1002",
"S1004",
"S1007",
"S1009",
"S1010",
"S1012",
"S1019",
"S1020",
"S1021",
"S1024",
"S1030",
"SA2000",
"SA2001",
"SA2003",
"SA3000",
"SA3001",
"SA4009",
"SA5000",
"SA5001",
"SA5002",
"SA5003",
"SA5004",
"SA5005",
"SA5007",
"SA5008",
"SA5009",
"SA5010",
#"SA5011",
"SA5012",
"SA6000",
"SA6001",
"SA6005",
]

nogo(
name = "tidb_nogo",
Expand Down Expand Up @@ -45,5 +81,6 @@ nogo(
"@org_golang_x_tools//go/analysis/passes/unreachable:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unsafeptr:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unusedresult:go_default_library",
],
"//build/linter/prealloc:prealloc",
] + staticcheck_analyzers(STATICHECK_ANALYZERS),
)
13 changes: 13 additions & 0 deletions build/linter/prealloc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "prealloc",
srcs = ["analyzer.go"],
importpath = "github.com/pingcap/tidb/build/linter/prealloc",
visibility = ["//visibility:public"],
deps = [
"//build/linter/util",
"@com_github_golangci_prealloc//:prealloc",
"@org_golang_x_tools//go/analysis",
],
)
60 changes: 60 additions & 0 deletions build/linter/prealloc/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package prealloc

import (
"go/ast"

"github.com/golangci/prealloc"
"github.com/pingcap/tidb/build/linter/util"
"golang.org/x/tools/go/analysis"
)

// Settings is the settings for preallocation.
type Settings struct {
Simple bool
RangeLoops bool `mapstructure:"range-loops"`
ForLoops bool `mapstructure:"range-loops"`
}

// Name is the name of the analyzer.
const Name = "prealloc"

// Analyzer is the analyzer struct of prealloc.
var Analyzer = &analysis.Analyzer{
Name: Name,
Doc: "Finds slice declarations that could potentially be preallocated",
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
s := &Settings{
Simple: true,
RangeLoops: true,
ForLoops: false,
}
for _, f := range pass.Files {
hints := prealloc.Check([]*ast.File{f}, s.Simple, s.RangeLoops, s.ForLoops)
for _, hint := range hints {
pass.Reportf(hint.Pos, "[%s] Consider preallocating %s", Name, util.FormatCode(hint.DeclaredSliceName))
}
}

return nil, nil
}

func init() {
util.SkipAnalyzer(Analyzer)
}
26 changes: 26 additions & 0 deletions build/linter/staticcheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//build/linter/staticcheck:def.bzl", "ANALYZERS")

[
go_library(
name = analyzer,
srcs = [
"analyzer.go",
"util.go",
],
importpath = "github.com/pingcap/tidb/build/linter/staticcheck/" + analyzer,
visibility = ["//visibility:public"],
x_defs = {"name": analyzer},
deps = [
"//build/linter/util",
"@co_honnef_go_tools//analysis/lint",
"@co_honnef_go_tools//quickfix",
"@co_honnef_go_tools//simple",
"@co_honnef_go_tools//staticcheck",
"@co_honnef_go_tools//stylecheck",
"@co_honnef_go_tools//unused",
"@org_golang_x_tools//go/analysis",
],
)
for analyzer in ANALYZERS
]
33 changes: 33 additions & 0 deletions build/linter/staticcheck/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package staticcheck

import (
"github.com/pingcap/tidb/build/linter/util"
"golang.org/x/tools/go/analysis"
)

var (
// Value to be added during stamping
name = "dummy value please replace using x_defs"

// Analyzer is an analyzer from staticcheck.
Analyzer *analysis.Analyzer
)

func init() {
Analyzer = FindAnalyzerByName(name)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
util.SkipAnalyzer(Analyzer)
}
Loading