From 975a8fba18cc282bbefcc223db9cb1e57feafe67 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Tue, 30 Jan 2024 16:59:26 +0800 Subject: [PATCH 1/6] add fmt lint Signed-off-by: Cabinfever_B --- .golangci.yml | 8 ++++++++ pkg/expression/context.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 40e21a04e593e..c1dcd48591271 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,6 +25,7 @@ linters: - predeclared - revive - lll + - gofmt linters-settings: staticcheck: @@ -389,6 +390,13 @@ linters-settings: - name: waitgroup-by-value severity: warning disabled: false + gofmt: + # https://golangci-lint.run/usage/linters/#gofmt + # disable for faster check + simplify: false + rewrite-rules: + - pattern: 'interface{}' + replacement: 'any' issues: exclude-rules: - path: _test\.go diff --git a/pkg/expression/context.go b/pkg/expression/context.go index 10fb6664e4c0b..eba0124d73328 100644 --- a/pkg/expression/context.go +++ b/pkg/expression/context.go @@ -82,7 +82,7 @@ type BuildContext interface { // GetSessionVars gets the session variables. GetSessionVars() *variable.SessionVars // SetValue saves a value associated with this context for key. - SetValue(key fmt.Stringer, value interface{}) + SetValue(key fmt.Stringer, value any) // BuiltinFunctionUsageInc increase the counting of each builtin function usage // Notice that this is a thread safe function BuiltinFunctionUsageInc(scalarFuncSigName string) From 8c3210b9493c54a8221b6632fb9fad006ba46589 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Tue, 30 Jan 2024 17:11:30 +0800 Subject: [PATCH 2/6] add fmt lint Signed-off-by: Cabinfever_B --- br/cmd/br/fips.go | 1 - br/cmd/tidb-lightning-ctl/fips.go | 1 - br/cmd/tidb-lightning/fips.go | 1 - cmd/tidb-server/fips.go | 1 - 4 files changed, 4 deletions(-) diff --git a/br/cmd/br/fips.go b/br/cmd/br/fips.go index 228261f45998f..56c6942877f9d 100644 --- a/br/cmd/br/fips.go +++ b/br/cmd/br/fips.go @@ -13,7 +13,6 @@ // limitations under the License. //go:build boringcrypto -// +build boringcrypto package main diff --git a/br/cmd/tidb-lightning-ctl/fips.go b/br/cmd/tidb-lightning-ctl/fips.go index 228261f45998f..56c6942877f9d 100644 --- a/br/cmd/tidb-lightning-ctl/fips.go +++ b/br/cmd/tidb-lightning-ctl/fips.go @@ -13,7 +13,6 @@ // limitations under the License. //go:build boringcrypto -// +build boringcrypto package main diff --git a/br/cmd/tidb-lightning/fips.go b/br/cmd/tidb-lightning/fips.go index 228261f45998f..56c6942877f9d 100644 --- a/br/cmd/tidb-lightning/fips.go +++ b/br/cmd/tidb-lightning/fips.go @@ -13,7 +13,6 @@ // limitations under the License. //go:build boringcrypto -// +build boringcrypto package main diff --git a/cmd/tidb-server/fips.go b/cmd/tidb-server/fips.go index 228261f45998f..56c6942877f9d 100644 --- a/cmd/tidb-server/fips.go +++ b/cmd/tidb-server/fips.go @@ -13,7 +13,6 @@ // limitations under the License. //go:build boringcrypto -// +build boringcrypto package main From aabf26e022dcf35ffbda275112bcdbdc2b25af07 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 31 Jan 2024 11:08:49 +0800 Subject: [PATCH 3/6] address comment Signed-off-by: Cabinfever_B --- Makefile | 2 +- build/linter/gofmt/analyzer.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2a91d900f4432..35bc50fce8f28 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ check: check-bazel-prepare parser_yacc check-parallel lint tidy testSuite errdoc fmt: @echo "gofmt (simplify)" - @gofmt -s -l -w $(FILES) 2>&1 | $(FAIL_ON_STDOUT) + @gofmt -s -l -w -r 'interface{} -> any' $(FILES) 2>&1 | $(FAIL_ON_STDOUT) check-static: tools/bin/golangci-lint GO111MODULE=on CGO_ENABLED=0 tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml diff --git a/build/linter/gofmt/analyzer.go b/build/linter/gofmt/analyzer.go index 5d3eec4c14867..4110d5f9c81a9 100644 --- a/build/linter/gofmt/analyzer.go +++ b/build/linter/gofmt/analyzer.go @@ -45,9 +45,12 @@ func run(pass *analysis.Pass) (any, error) { fileNames = append(fileNames, pos.Filename) } } - + rules := []gofmt.RewriteRule{{ + Pattern: "interface{}", + Replacement: "any", + }} for _, f := range fileNames { - diff, err := gofmt.Run(f, needSimplify) + diff, err := gofmt.RunRewrite(f, needSimplify, rules) if err != nil { return nil, fmt.Errorf("could not run gofmt: %w (%s)", err, f) } From 60ee2036841d0606c7dca36eaf15ccfa578a0146 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 31 Jan 2024 11:40:50 +0800 Subject: [PATCH 4/6] address comment Signed-off-by: Cabinfever_B --- build/nogo_config.json | 2 +- pkg/parser/format/format.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build/nogo_config.json b/build/nogo_config.json index b5764e8270c65..69fb829d02662 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -258,7 +258,7 @@ }, "gofmt": { "exclude_files": { - "pkg/parser/parser.go": "parser/parser.go code", + "pkg/parser/": "parser/*.go code", "external/": "no need to vet third party code", ".*_generated\\.go$": "ignore generated code", ".*mock.go$": "ignore generated code", diff --git a/pkg/parser/format/format.go b/pkg/parser/format/format.go index 68a0269e4b745..998fc5bbc78fd 100644 --- a/pkg/parser/format/format.go +++ b/pkg/parser/format/format.go @@ -34,7 +34,7 @@ const ( // Formatter is an io.Writer extended formatter by a fmt.Printf like function Format. type Formatter interface { io.Writer - Format(format string, args ...interface{}) (n int, errno error) + Format(format string, args ...any) (n int, errno error) } type indentFormatter struct { @@ -82,7 +82,7 @@ func IndentFormatter(w io.Writer, indent string) Formatter { return &indentFormatter{w, []byte(indent), 0, stBOL} } -func (f *indentFormatter) format(flat bool, format string, args ...interface{}) (n int, errno error) { +func (f *indentFormatter) format(flat bool, format string, args ...any) (n int, errno error) { var buf = make([]byte, 0) for i := 0; i < len(format); i++ { c := format[i] @@ -161,7 +161,7 @@ func (f *indentFormatter) format(flat bool, format string, args ...interface{}) } // Format implements Format interface. -func (f *indentFormatter) Format(format string, args ...interface{}) (n int, errno error) { +func (f *indentFormatter) Format(format string, args ...any) (n int, errno error) { return f.format(false, format, args...) } @@ -187,7 +187,7 @@ func FlatFormatter(w io.Writer) Formatter { } // Format implements Format interface. -func (f *flatFormatter) Format(format string, args ...interface{}) (n int, errno error) { +func (f *flatFormatter) Format(format string, args ...any) (n int, errno error) { return (*indentFormatter)(f).format(true, format, args...) } @@ -448,7 +448,7 @@ func (ctx *RestoreCtx) WritePlain(plainText string) { } // WritePlainf write the plain text into writer without any handling. -func (ctx *RestoreCtx) WritePlainf(format string, a ...interface{}) { +func (ctx *RestoreCtx) WritePlainf(format string, a ...any) { fmt.Fprintf(ctx.In, format, a...) } From a7ce8e5f46ec5c3f397605a77b5038c72823c675 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 31 Jan 2024 15:04:55 +0800 Subject: [PATCH 5/6] address comment Signed-off-by: Cabinfever_B --- pkg/parser/format/format.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/parser/format/format.go b/pkg/parser/format/format.go index 998fc5bbc78fd..68a0269e4b745 100644 --- a/pkg/parser/format/format.go +++ b/pkg/parser/format/format.go @@ -34,7 +34,7 @@ const ( // Formatter is an io.Writer extended formatter by a fmt.Printf like function Format. type Formatter interface { io.Writer - Format(format string, args ...any) (n int, errno error) + Format(format string, args ...interface{}) (n int, errno error) } type indentFormatter struct { @@ -82,7 +82,7 @@ func IndentFormatter(w io.Writer, indent string) Formatter { return &indentFormatter{w, []byte(indent), 0, stBOL} } -func (f *indentFormatter) format(flat bool, format string, args ...any) (n int, errno error) { +func (f *indentFormatter) format(flat bool, format string, args ...interface{}) (n int, errno error) { var buf = make([]byte, 0) for i := 0; i < len(format); i++ { c := format[i] @@ -161,7 +161,7 @@ func (f *indentFormatter) format(flat bool, format string, args ...any) (n int, } // Format implements Format interface. -func (f *indentFormatter) Format(format string, args ...any) (n int, errno error) { +func (f *indentFormatter) Format(format string, args ...interface{}) (n int, errno error) { return f.format(false, format, args...) } @@ -187,7 +187,7 @@ func FlatFormatter(w io.Writer) Formatter { } // Format implements Format interface. -func (f *flatFormatter) Format(format string, args ...any) (n int, errno error) { +func (f *flatFormatter) Format(format string, args ...interface{}) (n int, errno error) { return (*indentFormatter)(f).format(true, format, args...) } @@ -448,7 +448,7 @@ func (ctx *RestoreCtx) WritePlain(plainText string) { } // WritePlainf write the plain text into writer without any handling. -func (ctx *RestoreCtx) WritePlainf(format string, a ...any) { +func (ctx *RestoreCtx) WritePlainf(format string, a ...interface{}) { fmt.Fprintf(ctx.In, format, a...) } From 4b60354830e29faaf2cb9b8bd51f5cfbf25a88af Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 31 Jan 2024 19:10:28 +0800 Subject: [PATCH 6/6] fix Signed-off-by: Cabinfever_B --- pkg/kv/key.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kv/key.go b/pkg/kv/key.go index 4536caab5118f..9b3479462a57c 100644 --- a/pkg/kv/key.go +++ b/pkg/kv/key.go @@ -464,7 +464,7 @@ func calcStrsMemUsage(strs map[string]strHandleVal) int64 { return res } -func calcIntsMemUsage(ints map[int64]interface{}) int64 { +func calcIntsMemUsage(ints map[int64]any) int64 { return int64(len(ints)) * (size.SizeOfInt64 + size.SizeOfInterface) }