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

linter: check AutoSyncInterval for etcd config (#44081) #44094

Closed
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
154 changes: 154 additions & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package(default_visibility = ["//visibility:public"])

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

bool_flag(
name = "with_nogo_flag",
build_setting_default = False,
visibility = ["//visibility:public"],
)

config_setting(
name = "with_nogo",
flag_values = {
":with_nogo_flag": "true",
},
visibility = ["//visibility:public"],
)

STATICHECK_ANALYZERS = [
"S1000",
"S1001",
"S1002",
"S1003",
"S1004",
"S1005",
"S1006",
"S1007",
"S1008",
"S1009",
"S1010",
"S1011",
"S1012",
"S1016",
"S1017",
"S1018",
"S1019",
"S1020",
"S1021",
"S1023",
"S1024",
"S1025",
"S1028",
"S1029",
"S1030",
"S1031",
"S1032",
"S1033",
"S1034",
"S1035",
"S1036",
"S1037",
"S1038",
"S1039",
"S1040",
"SA1019",
"SA1029",
"SA2000",
"SA2001",
"SA2003",
"SA3000",
"SA3001",
"SA4009",
"SA5000",
"SA5001",
"SA5002",
"SA5003",
"SA5004",
"SA5005",
"SA5007",
"SA5008",
"SA5009",
"SA5010",
#"SA5011",
"SA5012",
"SA6000",
"SA6001",
"SA6005",
"U1000",
]

nogo(
name = "tidb_nogo",
config = ":nogo_config.json",
visibility = ["//visibility:public"], # must have public visibility
deps = [
"@org_golang_x_tools//go/analysis/passes/asmdecl:go_default_library",
"@org_golang_x_tools//go/analysis/passes/assign:go_default_library",
"@org_golang_x_tools//go/analysis/passes/atomic:go_default_library",
"@org_golang_x_tools//go/analysis/passes/atomicalign:go_default_library",
"@org_golang_x_tools//go/analysis/passes/bools:go_default_library",
"@org_golang_x_tools//go/analysis/passes/buildssa:go_default_library",
"@org_golang_x_tools//go/analysis/passes/buildtag:go_default_library",
# https://github.com/bazelbuild/rules_go/issues/2396
# "@org_golang_x_tools//go/analysis/passes/cgocall:go_default_library",
"@org_golang_x_tools//go/analysis/passes/composite:go_default_library",
"@org_golang_x_tools//go/analysis/passes/copylock:go_default_library",
"@org_golang_x_tools//go/analysis/passes/ctrlflow:go_default_library",
"@org_golang_x_tools//go/analysis/passes/deepequalerrors:go_default_library",
"@org_golang_x_tools//go/analysis/passes/errorsas:go_default_library",
"@org_golang_x_tools//go/analysis/passes/fieldalignment:go_default_library",
"@org_golang_x_tools//go/analysis/passes/findcall:go_default_library",
"@org_golang_x_tools//go/analysis/passes/httpresponse:go_default_library",
"@org_golang_x_tools//go/analysis/passes/ifaceassert:go_default_library",
"@org_golang_x_tools//go/analysis/passes/inspect:go_default_library",
"@org_golang_x_tools//go/analysis/passes/loopclosure:go_default_library",
"@org_golang_x_tools//go/analysis/passes/lostcancel:go_default_library",
"@org_golang_x_tools//go/analysis/passes/nilfunc:go_default_library",
"@org_golang_x_tools//go/analysis/passes/nilness:go_default_library",
"@org_golang_x_tools//go/analysis/passes/pkgfact:go_default_library",
"@org_golang_x_tools//go/analysis/passes/printf:go_default_library",
"@org_golang_x_tools//go/analysis/passes/shift:go_default_library",
"@org_golang_x_tools//go/analysis/passes/sortslice:go_default_library",
"@org_golang_x_tools//go/analysis/passes/stdmethods:go_default_library",
"@org_golang_x_tools//go/analysis/passes/stringintconv:go_default_library",
"@org_golang_x_tools//go/analysis/passes/structtag:go_default_library",
"@org_golang_x_tools//go/analysis/passes/testinggoroutine:go_default_library",
"@org_golang_x_tools//go/analysis/passes/tests:go_default_library",
"@org_golang_x_tools//go/analysis/passes/timeformat:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unmarshal:go_default_library",
"@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/asciicheck",
"//build/linter/bodyclose",
"//build/linter/durationcheck",
"//build/linter/etcdconfig",
"//build/linter/exportloopref",
"//build/linter/forcetypeassert",
"//build/linter/gofmt",
"//build/linter/gci",
"//build/linter/gosec",
"//build/linter/ineffassign",
"//build/linter/makezero",
"//build/linter/mirror",
"//build/linter/misspell",
"//build/linter/noloopclosure",
"//build/linter/prealloc",
"//build/linter/predeclared",
"//build/linter/unconvert",
"//build/linter/rowserrcheck",
] + staticcheck_analyzers(STATICHECK_ANALYZERS) +
select({
"//build:with_nogo": [
"//build/linter/allrevive",
"//build/linter/errcheck",
"//build/linter/filepermission",
"//build/linter/lll",
"//build/linter/revive",
],
"//conditions:default": [],
}),
)
12 changes: 12 additions & 0 deletions build/linter/etcdconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "etcdconfig",
srcs = ["analyzer.go"],
importpath = "github.com/pingcap/tidb/build/linter/etcdconfig",
visibility = ["//visibility:public"],
deps = [
"@org_golang_x_tools//go/analysis",
"@org_golang_x_tools//go/analysis/passes/inspect",
],
)
100 changes: 100 additions & 0 deletions build/linter/etcdconfig/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 etcdconfig

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)

// Analyzer is the analyzer struct of unconvert.
var Analyzer = &analysis.Analyzer{
Name: "etcdconfig",
Doc: "Check necessary fields of etcd config",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}

const (
configPackagePath = "go.etcd.io/etcd/client/v3"
configPackageName = "clientv3"
configStructName = "Config"
)

// Adapted from https://github.com/mdempsky/unconvert/blob/beb68d938016d2dec1d1b078054f4d3db25f97be/unconvert.go#L371-L414.
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
packageName := ""
for _, spec := range file.Imports {
if spec.Path.Value != "\""+configPackagePath+"\"" {
continue
}
if spec.Name != nil {
packageName = spec.Name.Name
} else {
packageName = configPackageName
}
}
if packageName == "" {
continue
}

for _, decl := range file.Decls {
ast.Inspect(decl, func(n ast.Node) bool {
lit, ok := n.(*ast.CompositeLit)
if !ok {
return true
}
tp, ok := lit.Type.(*ast.SelectorExpr)
if !ok {
return true
}
litPackage, ok := tp.X.(*ast.Ident)
if !ok {
return true
}
if litPackage.Name != packageName {
return true
}
if tp.Sel.Name != configStructName {
return true
}

found := false
for _, field := range lit.Elts {
kv, ok := field.(*ast.KeyValueExpr)
if !ok {
continue
}
key, ok := kv.Key.(*ast.Ident)
if !ok {
continue
}
if key.Name == "AutoSyncInterval" {
found = true
break
}
}
if !found {
pass.Reportf(lit.Pos(), "missing field AutoSyncInterval")
}
return true
})
}
}
return nil, nil
}
Loading