From d67363a71f5418f1136e986cdcb028eecfafd7ac Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 15:43:39 +0800 Subject: [PATCH 1/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- DEPS.bzl | 8 ++++++++ build/BUILD.bazel | 1 + build/linter/mirror/BUILD.bazel | 12 ++++++++++++ build/linter/mirror/analyzer.go | 27 +++++++++++++++++++++++++++ build/nogo_config.json | 8 ++++++++ go.mod | 1 + go.sum | 2 ++ 7 files changed, 59 insertions(+) create mode 100644 build/linter/mirror/BUILD.bazel create mode 100644 build/linter/mirror/analyzer.go diff --git a/DEPS.bzl b/DEPS.bzl index b55b6d1909be6..6b7779c7414a6 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -426,6 +426,14 @@ def go_deps(): sum = "h1:QvrO2QF2+/Cx1WA/vETCIYBKtRjc30vesdoPUNo1EbY=", version = "v0.1.1", ) + go_repository( + name = "com_github_butuzov_mirror", + build_file_proto_mode = "disable", + importpath = "github.com/butuzov/mirror", + sum = "h1:AOFqNeGL+3znnsBptNI4PNKT1IpzTD+95vY7bsMM1pg=", + version = "v0.1.2", + ) + go_repository( name = "com_github_cakturk_go_netstat", build_file_proto_mode = "disable", diff --git a/build/BUILD.bazel b/build/BUILD.bazel index 350aa0b33e220..e9c5f36dd1778 100644 --- a/build/BUILD.bazel +++ b/build/BUILD.bazel @@ -132,6 +132,7 @@ nogo( "//build/linter/gosec", "//build/linter/ineffassign", "//build/linter/makezero", + "//build/linter/mirror", "//build/linter/misspell", "//build/linter/noloopclosure", "//build/linter/prealloc", diff --git a/build/linter/mirror/BUILD.bazel b/build/linter/mirror/BUILD.bazel new file mode 100644 index 0000000000000..33ff6e4126703 --- /dev/null +++ b/build/linter/mirror/BUILD.bazel @@ -0,0 +1,12 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "mirror", + srcs = ["analyzer.go"], + importpath = "github.com/pingcap/tidb/build/linter/mirror", + visibility = ["//visibility:public"], + deps = [ + "//build/linter/util", + "@com_github_butuzov_mirror//:mirror", + ], +) diff --git a/build/linter/mirror/analyzer.go b/build/linter/mirror/analyzer.go new file mode 100644 index 0000000000000..ab900cd347cd0 --- /dev/null +++ b/build/linter/mirror/analyzer.go @@ -0,0 +1,27 @@ +// 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 mirror + +import ( + "github.com/butuzov/mirror" + "github.com/pingcap/tidb/build/linter/util" +) + +// Analyzer is the analyzer struct of mirror. +var Analyzer = mirror.NewAnalyzer() + +func init() { + util.SkipAnalyzer(Analyzer) +} diff --git a/build/nogo_config.json b/build/nogo_config.json index 663609ad938e8..10a6fb1af3239 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -421,6 +421,14 @@ ".*_generated\\.go$": "ignore generated code" } }, + "mirror": { + "exclude_files": { + "parser/parser.go": "parser/parser.go code", + "/cgo/": "ignore cgo code", + "external/": "no need to vet third party code", + ".*_generated\\.go$": "ignore generated code" + } + }, "misspell": { "exclude_files": { "parser/parser.go": "parser/parser.go code", diff --git a/go.mod b/go.mod index 688efbc573191..5c697d82e1ab5 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/aws/aws-sdk-go v1.44.48 github.com/bazelbuild/buildtools v0.0.0-20230317132445-9c3c1fc0106e github.com/blacktear23/go-proxyprotocol v1.0.6 + github.com/butuzov/mirror v0.1.2 github.com/carlmjohnson/flagext v0.21.0 github.com/charithe/durationcheck v0.0.10 github.com/cheggaaa/pb/v3 v3.0.8 diff --git a/go.sum b/go.sum index 7e99945737dbd..18ae1cf746f98 100644 --- a/go.sum +++ b/go.sum @@ -141,6 +141,8 @@ github.com/blacktear23/go-proxyprotocol v1.0.6 h1:eTt6UMpEnq59NjON49b3Cay8Dm0sCs github.com/blacktear23/go-proxyprotocol v1.0.6/go.mod h1:FSCbgnRZrQXazBLL5snfBbrcFSMtcmUDhSRb9OfFA1o= github.com/bmatcuk/doublestar/v2 v2.0.4 h1:6I6oUiT/sU27eE2OFcWqBhL1SwjyvQuOssxT4a1yidI= github.com/bmatcuk/doublestar/v2 v2.0.4/go.mod h1:QMmcs3H2AUQICWhfzLXz+IYln8lRQmTZRptLie8RgRw= +github.com/butuzov/mirror v0.1.2 h1:AOFqNeGL+3znnsBptNI4PNKT1IpzTD+95vY7bsMM1pg= +github.com/butuzov/mirror v0.1.2/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= github.com/carlmjohnson/flagext v0.21.0 h1:/c4uK3ie786Z7caXLcIMvePNSSiH3bQVGDvmGLMme60= github.com/carlmjohnson/flagext v0.21.0/go.mod h1:Eenv0epIUAr4NuedNmkzI8WmBmjIxZC239XcKxYS2ac= github.com/cenk/backoff v2.0.0+incompatible/go.mod h1:7FtoeaSnHoZnmZzz47cM35Y9nSW7tNyaidugnHTaFDE= From e2f64321f6bf9447959574b1f63aea109fc5f685 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 15:53:33 +0800 Subject: [PATCH 2/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- parser/auth/caching_sha2.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/parser/auth/caching_sha2.go b/parser/auth/caching_sha2.go index a31d466d9b09e..b97ac29b0ddd1 100644 --- a/parser/auth/caching_sha2.go +++ b/parser/auth/caching_sha2.go @@ -74,14 +74,14 @@ func hashCrypt(plaintext string, salt []byte, iterations int, hash func([]byte) // 1, 2, 3 bufA := bytes.NewBuffer(make([]byte, 0, 4096)) - bufA.Write([]byte(plaintext)) + bufA.WriteString(plaintext) bufA.Write(salt) // 4, 5, 6, 7, 8 bufB := bytes.NewBuffer(make([]byte, 0, 4096)) - bufB.Write([]byte(plaintext)) + bufB.WriteString(plaintext) bufB.Write(salt) - bufB.Write([]byte(plaintext)) + bufB.WriteString(plaintext) sumB := hash(bufB.Bytes()) bufB.Reset() @@ -95,7 +95,7 @@ func hashCrypt(plaintext string, salt []byte, iterations int, hash func([]byte) // 11 for i = len(plaintext); i > 0; i >>= 1 { if i%2 == 0 { - bufA.Write([]byte(plaintext)) + bufA.WriteString(plaintext) } else { bufA.Write(sumB[:]) } @@ -108,7 +108,7 @@ func hashCrypt(plaintext string, salt []byte, iterations int, hash func([]byte) // 13, 14, 15 bufDP := bufA for range []byte(plaintext) { - bufDP.Write([]byte(plaintext)) + bufDP.WriteString(plaintext) } sumDP := hash(bufDP.Bytes()) bufDP.Reset() @@ -170,7 +170,7 @@ func hashCrypt(plaintext string, salt []byte, iterations int, hash func([]byte) buf := bytes.NewBuffer(make([]byte, 0, 100)) buf.Write([]byte{'$', 'A', '$'}) rounds := fmt.Sprintf("%03d", iterations/ITERATION_MULTIPLIER) - buf.Write([]byte(rounds)) + buf.WriteString(rounds) buf.Write([]byte{'$'}) buf.Write(salt) From 301bd8e10d81080c170a2e1eab5ed789fc50f06b Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:06:04 +0800 Subject: [PATCH 3/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- tools/check/xprog.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/check/xprog.go b/tools/check/xprog.go index 48bb1593afb01..c5034bdf329a1 100644 --- a/tools/check/xprog.go +++ b/tools/check/xprog.go @@ -78,7 +78,9 @@ func getPackageInfo(dir string) string { if err != nil { os.Exit(-2) } + //nolint:mirror start := strings.IndexByte(string(line), ' ') + //nolint:mirror end := strings.IndexByte(string(line), '=') pkg := string(line[start+1 : end]) return pkg From 6d289eb6f26c5365790d72170e538b8cd619e208 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:07:49 +0800 Subject: [PATCH 4/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- build/nogo_config.json | 1 + tools/check/xprog.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/build/nogo_config.json b/build/nogo_config.json index 10a6fb1af3239..162a01a36e98e 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -426,6 +426,7 @@ "parser/parser.go": "parser/parser.go code", "/cgo/": "ignore cgo code", "external/": "no need to vet third party code", + "tools/": "ignore tools code", ".*_generated\\.go$": "ignore generated code" } }, diff --git a/tools/check/xprog.go b/tools/check/xprog.go index c5034bdf329a1..48bb1593afb01 100644 --- a/tools/check/xprog.go +++ b/tools/check/xprog.go @@ -78,9 +78,7 @@ func getPackageInfo(dir string) string { if err != nil { os.Exit(-2) } - //nolint:mirror start := strings.IndexByte(string(line), ' ') - //nolint:mirror end := strings.IndexByte(string(line), '=') pkg := string(line[start+1 : end]) return pkg From f3fe9aa43023b417062de30c8079193578382049 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:14:10 +0800 Subject: [PATCH 5/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- types/json_binary.go | 3 +-- types/json_binary_functions.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/types/json_binary.go b/types/json_binary.go index cacf5b69b025a..859c54b59ff87 100644 --- a/types/json_binary.go +++ b/types/json_binary.go @@ -23,7 +23,6 @@ import ( "math" "reflect" "strconv" - "strings" "time" "unicode/utf8" @@ -717,7 +716,7 @@ func appendBinaryNumber(buf []byte, x json.Number) (JSONTypeCode, []byte, error) // - Then uint64 (valid in MySQL JSON, not in JSON decode library) // - Then float64 // - Return an error - if strings.ContainsAny(string(x), "Ee.") { + if bytes.ContainsAny([]byte(x), "Ee.") { f64, err := x.Float64() if err != nil { return JSONTypeCodeFloat64, nil, errors.Trace(err) diff --git a/types/json_binary_functions.go b/types/json_binary_functions.go index 7c4d7bf7f97bc..8a97bda141bd9 100644 --- a/types/json_binary_functions.go +++ b/types/json_binary_functions.go @@ -200,7 +200,7 @@ func quoteJSONString(s string) string { i++ } } else { - c, size := utf8.DecodeRune([]byte(s[i:])) + c, size := utf8.DecodeRuneInString(s[i:]) if c == utf8.RuneError && size == 1 { // refer to codes of `binary.jsonMarshalStringTo` if start < i { ret.WriteString(s[start:i]) From d27fa45e0b211fa4536d89be94e308bfdb853727 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:20:25 +0800 Subject: [PATCH 6/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- types/json_binary.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/json_binary.go b/types/json_binary.go index 859c54b59ff87..6c184e7d06dd6 100644 --- a/types/json_binary.go +++ b/types/json_binary.go @@ -23,6 +23,7 @@ import ( "math" "reflect" "strconv" + "strings" "time" "unicode/utf8" @@ -716,7 +717,7 @@ func appendBinaryNumber(buf []byte, x json.Number) (JSONTypeCode, []byte, error) // - Then uint64 (valid in MySQL JSON, not in JSON decode library) // - Then float64 // - Return an error - if bytes.ContainsAny([]byte(x), "Ee.") { + if strings.Contains(x.String(), "Ee.") { f64, err := x.Float64() if err != nil { return JSONTypeCodeFloat64, nil, errors.Trace(err) From 044421ab9ccc9984fd6627faddbb61617789c246 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:23:06 +0800 Subject: [PATCH 7/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- session/txninfo/txn_info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/txninfo/txn_info.go b/session/txninfo/txn_info.go index 5e19fa15fe676..7ad72ee6308d2 100644 --- a/session/txninfo/txn_info.go +++ b/session/txninfo/txn_info.go @@ -244,7 +244,7 @@ var columnValueGetterMap = map[string]func(*TxnInfo) types.Datum{ first := true for tblID := range relatedTableIDs { if !first { - str.Write([]byte(",")) + str.WriteString(",") } else { first = false } From 045af202f813d825dad6f97a689b04aa27007987 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 9 May 2023 16:26:53 +0800 Subject: [PATCH 8/8] *: enable mirror linter Signed-off-by: Weizhen Wang --- br/pkg/utils/key.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/br/pkg/utils/key.go b/br/pkg/utils/key.go index 62d194ca57a2e..db4e884003d9d 100644 --- a/br/pkg/utils/key.go +++ b/br/pkg/utils/key.go @@ -37,7 +37,7 @@ func ParseKey(format, key string) ([]byte, error) { // Ref PD: https://github.com/pingcap/pd/blob/master/tools/pd-ctl/pdctl/command/region_command.go#L334 func unescapedKey(text string) ([]byte, error) { var buf []byte - r := bytes.NewBuffer([]byte(text)) + r := bytes.NewBufferString(text) for { c, err := r.ReadByte() if err != nil {