Add golang.org/x/tools modernize static analyzer and fix violations (#15946)

* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
This commit is contained in:
Preston Van Loon
2025-11-13 19:27:22 -06:00
committed by GitHub
parent f77b78943a
commit 2fd6bd8150
605 changed files with 217475 additions and 2228 deletions

View File

@@ -19,20 +19,20 @@ import (
// AssertionTestingTB exposes enough testing.TB methods for assertions.
type AssertionTestingTB interface {
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
}
type assertionLoggerFn func(string, ...interface{})
type assertionLoggerFn func(string, ...any)
func SprintfAssertionLoggerFn(s *string) assertionLoggerFn {
return func(ef string, eargs ...interface{}) {
return func(ef string, eargs ...any) {
*s = fmt.Sprintf(ef, eargs...)
}
}
// Equal compares values using comparison operator.
func Equal(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func Equal(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if expected != actual {
errMsg := parseMsg("Values are not equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -41,7 +41,7 @@ func Equal(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...inte
}
// NotEqual compares values using comparison operator.
func NotEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func NotEqual(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if expected == actual {
errMsg := parseMsg("Values are equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -50,7 +50,7 @@ func NotEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...i
}
// DeepEqual compares values using DeepEqual.
func DeepEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func DeepEqual(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if !isDeepEqual(expected, actual) {
errMsg := parseMsg("Values are not equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -68,7 +68,7 @@ var protobufPrivateFields = map[string]bool{
"state": true,
}
func ProtobufPrettyDiff(a, b interface{}) string {
func ProtobufPrettyDiff(a, b any) string {
d, _ := messagediff.DeepDiff(a, b)
var dstr []string
appendNotProto := func(path, str string) {
@@ -92,7 +92,7 @@ func ProtobufPrettyDiff(a, b interface{}) string {
}
// DeepNotEqual compares values using DeepEqual.
func DeepNotEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func DeepNotEqual(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if isDeepEqual(expected, actual) {
errMsg := parseMsg("Values are equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -101,7 +101,7 @@ func DeepNotEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg
}
// DeepSSZEqual compares values using ssz.DeepEqual.
func DeepSSZEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func DeepSSZEqual(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if !equality.DeepEqual(expected, actual) {
errMsg := parseMsg("Values are not equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -111,7 +111,7 @@ func DeepSSZEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg
}
// DeepNotSSZEqual compares values using ssz.DeepEqual.
func DeepNotSSZEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
func DeepNotSSZEqual(loggerFn assertionLoggerFn, expected, actual any, msg ...any) {
if equality.DeepEqual(expected, actual) {
errMsg := parseMsg("Values are equal", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -120,7 +120,7 @@ func DeepNotSSZEqual(loggerFn assertionLoggerFn, expected, actual interface{}, m
}
// StringContains checks whether a string contains specified substring. If flag is false, inverse is checked.
func StringContains(loggerFn assertionLoggerFn, expected, actual string, flag bool, msg ...interface{}) {
func StringContains(loggerFn assertionLoggerFn, expected, actual string, flag bool, msg ...any) {
if flag {
if !strings.Contains(actual, expected) {
errMsg := parseMsg("Expected substring is not found", msg...)
@@ -137,7 +137,7 @@ func StringContains(loggerFn assertionLoggerFn, expected, actual string, flag bo
}
// NoError asserts that error is nil.
func NoError(loggerFn assertionLoggerFn, err error, msg ...interface{}) {
func NoError(loggerFn assertionLoggerFn, err error, msg ...any) {
// reflect.ValueOf is needed for nil instances of custom types implementing Error
if err != nil && !reflect.ValueOf(err).IsNil() {
errMsg := parseMsg("Unexpected error", msg...)
@@ -148,7 +148,7 @@ func NoError(loggerFn assertionLoggerFn, err error, msg ...interface{}) {
// ErrorIs uses Errors.Is to recursively unwrap err looking for target in the chain.
// If any error in the chain matches target, the assertion will pass.
func ErrorIs(loggerFn assertionLoggerFn, err, target error, msg ...interface{}) {
func ErrorIs(loggerFn assertionLoggerFn, err, target error, msg ...any) {
if !errors.Is(err, target) {
errMsg := parseMsg(fmt.Sprintf("error %s", target), msg...)
_, file, line, _ := runtime.Caller(2)
@@ -157,7 +157,7 @@ func ErrorIs(loggerFn assertionLoggerFn, err, target error, msg ...interface{})
}
// ErrorContains asserts that actual error contains wanted message.
func ErrorContains(loggerFn assertionLoggerFn, want string, err error, msg ...interface{}) {
func ErrorContains(loggerFn assertionLoggerFn, want string, err error, msg ...any) {
if want == "" {
loggerFn("Want string can't be empty")
}
@@ -169,7 +169,7 @@ func ErrorContains(loggerFn assertionLoggerFn, want string, err error, msg ...in
}
// NotNil asserts that passed value is not nil.
func NotNil(loggerFn assertionLoggerFn, obj interface{}, msg ...interface{}) {
func NotNil(loggerFn assertionLoggerFn, obj any, msg ...any) {
if deepNil(obj) {
errMsg := parseMsg("Unexpected nil value", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -178,7 +178,7 @@ func NotNil(loggerFn assertionLoggerFn, obj interface{}, msg ...interface{}) {
}
// IsNil asserts that observed value is nil.
func IsNil(loggerFn assertionLoggerFn, got interface{}, msg ...interface{}) {
func IsNil(loggerFn assertionLoggerFn, got any, msg ...any) {
if !deepNil(got) {
errMsg := parseMsg("Value is unexpectedly not nil", msg...)
_, file, line, _ := runtime.Caller(2)
@@ -187,7 +187,7 @@ func IsNil(loggerFn assertionLoggerFn, got interface{}, msg ...interface{}) {
}
// deepNil checks that underlying value of obj is nil.
func deepNil(got interface{}) bool {
func deepNil(got any) bool {
if got == nil {
return true
}
@@ -200,7 +200,7 @@ func deepNil(got interface{}) bool {
}
// LogsContain checks whether a given substring is a part of logs. If flag=false, inverse is checked.
func LogsContain(loggerFn assertionLoggerFn, hook *test.Hook, want string, flag bool, msg ...interface{}) {
func LogsContain(loggerFn assertionLoggerFn, hook *test.Hook, want string, flag bool, msg ...any) {
_, file, line, _ := runtime.Caller(2)
entries := hook.AllEntries()
logs := make([]string, 0, len(entries))
@@ -236,7 +236,7 @@ func LogsContain(loggerFn assertionLoggerFn, hook *test.Hook, want string, flag
}
}
func parseMsg(defaultMsg string, msg ...interface{}) string {
func parseMsg(defaultMsg string, msg ...any) string {
if len(msg) >= 1 {
msgFormat, ok := msg[0].(string)
if !ok {
@@ -247,7 +247,7 @@ func parseMsg(defaultMsg string, msg ...interface{}) string {
return defaultMsg
}
func isDeepEqual(expected, actual interface{}) bool {
func isDeepEqual(expected, actual any) bool {
_, isProto := expected.(proto.Message)
if isProto {
return proto.Equal(expected.(proto.Message), actual.(proto.Message))
@@ -257,7 +257,7 @@ func isDeepEqual(expected, actual interface{}) bool {
// NotEmpty asserts that an object's fields are not empty. This function recursively checks each
// pointer / struct field.
func NotEmpty(loggerFn assertionLoggerFn, obj interface{}, msg ...interface{}) {
func NotEmpty(loggerFn assertionLoggerFn, obj any, msg ...any) {
_, ignoreFieldsWithoutTags := obj.(proto.Message)
notEmpty(loggerFn, obj, ignoreFieldsWithoutTags, []string{} /*fields*/, 0 /*stackSize*/, msg...)
}
@@ -265,7 +265,7 @@ func NotEmpty(loggerFn assertionLoggerFn, obj interface{}, msg ...interface{}) {
// notEmpty checks all fields are not zero, including pointer field references to other structs.
// This method has the option to ignore fields without struct tags, which is helpful for checking
// protobuf messages that have internal fields.
func notEmpty(loggerFn assertionLoggerFn, obj interface{}, ignoreFieldsWithoutTags bool, fields []string, stackSize int, msg ...interface{}) {
func notEmpty(loggerFn assertionLoggerFn, obj any, ignoreFieldsWithoutTags bool, fields []string, stackSize int, msg ...any) {
var v reflect.Value
if vo, ok := obj.(reflect.Value); ok {
v = reflect.Indirect(vo)
@@ -333,11 +333,11 @@ type TBMock struct {
}
// Errorf writes testing logs to ErrorfMsg.
func (tb *TBMock) Errorf(format string, args ...interface{}) {
func (tb *TBMock) Errorf(format string, args ...any) {
tb.ErrorfMsg = fmt.Sprintf(format, args...)
}
// Fatalf writes testing logs to FatalfMsg.
func (tb *TBMock) Fatalf(format string, args ...interface{}) {
func (tb *TBMock) Fatalf(format string, args ...any) {
tb.FatalfMsg = fmt.Sprintf(format, args...)
}

View File

@@ -20,9 +20,9 @@ import (
func Test_Equal(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
msgs []interface{}
expected any
actual any
msgs []any
}
tests := []struct {
name string
@@ -61,7 +61,7 @@ func Test_Equal(t *testing.T) {
tb: &assertions.TBMock{},
expected: 42,
actual: 41,
msgs: []interface{}{"Custom values are not equal"},
msgs: []any{"Custom values are not equal"},
},
expectedErr: "Custom values are not equal, want: 42 (int), got: 41 (int)",
},
@@ -71,7 +71,7 @@ func Test_Equal(t *testing.T) {
tb: &assertions.TBMock{},
expected: 42,
actual: 41,
msgs: []interface{}{"Custom values are not equal (for slot %d)", 12},
msgs: []any{"Custom values are not equal (for slot %d)", 12},
},
expectedErr: "Custom values are not equal (for slot 12), want: 42 (int), got: 41 (int)",
},
@@ -98,9 +98,9 @@ func Test_Equal(t *testing.T) {
func Test_NotEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
msgs []interface{}
expected any
actual any
msgs []any
}
tests := []struct {
name string
@@ -138,7 +138,7 @@ func Test_NotEqual(t *testing.T) {
tb: &assertions.TBMock{},
expected: 42,
actual: 42,
msgs: []interface{}{"Custom values are equal"},
msgs: []any{"Custom values are equal"},
},
expectedErr: "Custom values are equal, both values are equal",
},
@@ -165,9 +165,9 @@ func Test_NotEqual(t *testing.T) {
func TestAssert_DeepEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
msgs []interface{}
expected any
actual any
msgs []any
}
tests := []struct {
name string
@@ -197,7 +197,7 @@ func TestAssert_DeepEqual(t *testing.T) {
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{41},
msgs: []interface{}{"Custom values are not equal"},
msgs: []any{"Custom values are not equal"},
},
expectedErr: "Custom values are not equal, expected != actual, diff: struct{ i int }{\n- \ti: 42,\n+ \ti: 41,\n }",
},
@@ -207,7 +207,7 @@ func TestAssert_DeepEqual(t *testing.T) {
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{41},
msgs: []interface{}{"Custom values are not equal (for slot %d)", 12},
msgs: []any{"Custom values are not equal (for slot %d)", 12},
},
expectedErr: "Custom values are not equal (for slot 12), expected != actual, diff: struct{ i int }{\n- \ti: 42,\n+ \ti: 41,\n }\n",
},
@@ -249,9 +249,9 @@ func TestAssert_DeepEqual(t *testing.T) {
func TestAssert_DeepNotEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
msgs []interface{}
expected any
actual any
msgs []any
}
tests := []struct {
name string
@@ -281,7 +281,7 @@ func TestAssert_DeepNotEqual(t *testing.T) {
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{42},
msgs: []interface{}{"Custom values are equal"},
msgs: []any{"Custom values are equal"},
},
expectedErr: "Custom values are equal, want: struct { i int }{i:42}, got: struct { i int }{i:42}",
},
@@ -291,7 +291,7 @@ func TestAssert_DeepNotEqual(t *testing.T) {
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{42},
msgs: []interface{}{"Custom values are equal (for slot %d)", 12},
msgs: []any{"Custom values are equal (for slot %d)", 12},
},
expectedErr: "Custom values are equal (for slot 12), want: struct { i int }{i:42}, got: struct { i int }{i:42}",
},
@@ -318,8 +318,8 @@ func TestAssert_DeepNotEqual(t *testing.T) {
func TestAssert_DeepSSZEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
expected any
actual any
}
tests := []struct {
name string
@@ -380,8 +380,8 @@ func TestAssert_DeepSSZEqual(t *testing.T) {
func TestAssert_DeepNotSSZEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
expected any
actual any
}
tests := []struct {
name string
@@ -443,7 +443,7 @@ func TestAssert_NoError(t *testing.T) {
type args struct {
tb *assertions.TBMock
err error
msgs []interface{}
msgs []any
}
tests := []struct {
name string
@@ -469,7 +469,7 @@ func TestAssert_NoError(t *testing.T) {
args: args{
tb: &assertions.TBMock{},
err: errors.New("failed"),
msgs: []interface{}{"Custom error message"},
msgs: []any{"Custom error message"},
},
expectedErr: "Custom error message: failed",
},
@@ -478,7 +478,7 @@ func TestAssert_NoError(t *testing.T) {
args: args{
tb: &assertions.TBMock{},
err: errors.New("failed"),
msgs: []interface{}{"Custom error message (for slot %d)", 12},
msgs: []any{"Custom error message (for slot %d)", 12},
},
expectedErr: "Custom error message (for slot 12): failed",
},
@@ -507,7 +507,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb *assertions.TBMock
want string
err error
msgs []interface{}
msgs []any
}
tests := []struct {
name string
@@ -546,7 +546,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb: &assertions.TBMock{},
want: "another error",
err: errors.New("failed"),
msgs: []interface{}{"Something wrong"},
msgs: []any{"Something wrong"},
},
expectedErr: "Something wrong, got: failed, want: another error",
},
@@ -556,7 +556,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb: &assertions.TBMock{},
want: "failed",
err: errors.New("failed"),
msgs: []interface{}{"Something wrong"},
msgs: []any{"Something wrong"},
},
expectedErr: "",
},
@@ -566,7 +566,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb: &assertions.TBMock{},
want: "another error",
err: errors.New("failed"),
msgs: []interface{}{"Something wrong (for slot %d)", 12},
msgs: []any{"Something wrong (for slot %d)", 12},
},
expectedErr: "Something wrong (for slot 12), got: failed, want: another error",
},
@@ -576,7 +576,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb: &assertions.TBMock{},
want: "failed",
err: errors.New("failed"),
msgs: []interface{}{"Something wrong (for slot %d)", 12},
msgs: []any{"Something wrong (for slot %d)", 12},
},
expectedErr: "",
},
@@ -586,7 +586,7 @@ func TestAssert_ErrorContains(t *testing.T) {
tb: &assertions.TBMock{},
want: "",
err: errors.New("failed"),
msgs: []interface{}{"Something wrong (for slot %d)", 12},
msgs: []any{"Something wrong (for slot %d)", 12},
},
expectedErr: "Want string can't be empty",
},
@@ -613,8 +613,8 @@ func TestAssert_ErrorContains(t *testing.T) {
func Test_NotNil(t *testing.T) {
type args struct {
tb *assertions.TBMock
obj interface{}
msgs []interface{}
obj any
msgs []any
}
var nilBlock *eth.SignedBeaconBlock = nil
tests := []struct {
@@ -633,7 +633,7 @@ func Test_NotNil(t *testing.T) {
name: "nil custom message",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"This should not be nil"},
msgs: []any{"This should not be nil"},
},
expectedErr: "This should not be nil",
},
@@ -641,7 +641,7 @@ func Test_NotNil(t *testing.T) {
name: "nil custom message with params",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"This should not be nil (for slot %d)", 12},
msgs: []any{"This should not be nil (for slot %d)", 12},
},
expectedErr: "This should not be nil (for slot 12)",
},
@@ -693,7 +693,7 @@ func Test_LogsContainDoNotContain(t *testing.T) {
tb *assertions.TBMock
want string
flag bool
msgs []interface{}
msgs []any
}
tests := []struct {
name string
@@ -726,7 +726,7 @@ func Test_LogsContainDoNotContain(t *testing.T) {
name: "should contain not found custom message",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"Waited for logs"},
msgs: []any{"Waited for logs"},
want: "here goes some expected log string",
flag: true,
},
@@ -736,7 +736,7 @@ func Test_LogsContainDoNotContain(t *testing.T) {
name: "should contain not found custom message with params",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"Waited for %d logs", 10},
msgs: []any{"Waited for %d logs", 10},
want: "here goes some expected log string",
flag: true,
},
@@ -765,7 +765,7 @@ func Test_LogsContainDoNotContain(t *testing.T) {
name: "should not contain but found custom message",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"Dit not expect logs"},
msgs: []any{"Dit not expect logs"},
want: "here goes some unexpected log string",
},
updateLogs: func(log *logrus.Logger) {
@@ -777,7 +777,7 @@ func Test_LogsContainDoNotContain(t *testing.T) {
name: "should not contain but found custom message with params",
args: args{
tb: &assertions.TBMock{},
msgs: []interface{}{"Dit not expect %d logs", 10},
msgs: []any{"Dit not expect %d logs", 10},
want: "here goes some unexpected log string",
},
updateLogs: func(log *logrus.Logger) {
@@ -824,9 +824,9 @@ func Test_LogsContainDoNotContain(t *testing.T) {
func TestAssert_NotEmpty(t *testing.T) {
type args struct {
tb *assertions.TBMock
input interface{}
actual interface{}
msgs []interface{}
input any
actual any
msgs []any
}
tests := []struct {
name string