From 91264a97520b44eb68da0be2929b207c6ac93647 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 16 Jun 2026 11:46:21 +0300 Subject: [PATCH] all: go fix --- .golangci.yml | 10 +++++----- annotated.go | 14 +++++++------- app_unixes.go | 1 - app_wasm.go | 1 - app_windows.go | 1 - app_windows_test.go | 1 - docs/ex/annotate/cast_bad.go | 1 - docs/ex/value-groups/consume/param.go | 2 +- docs/ex/value-groups/feed/result.go | 2 +- docs/src/container.md | 6 +++--- extract.go | 6 +++--- internal/lifecycle/lifecycle.go | 8 ++++---- populate.go | 4 ++-- provide.go | 2 +- shutdown_test.go | 1 - tools/analysis/passes/allfxevents/analysis.go | 4 ++-- 16 files changed, 29 insertions(+), 35 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c1d0eafe5..3acc65757 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,6 +18,7 @@ linters: # Our own extras: - errorlint + - modernize - nolintlint # lints //nolint directives - revive @@ -28,11 +29,10 @@ linters: # These govet checks are disabled by default, but they're useful. govet: - enable: - - nilness - - reflectvaluecompare - - sortslice - - unusedwrite + enable-all: true + disable: + - fieldalignment + - shadow goheader: values: diff --git a/annotated.go b/annotated.go index 4a732f1e3..7629905d9 100644 --- a/annotated.go +++ b/annotated.go @@ -100,13 +100,13 @@ var ( // field used for embedding fx.In type in generated struct. _inAnnotationField = reflect.StructField{ Name: "In", - Type: reflect.TypeOf(In{}), + Type: reflect.TypeFor[In](), Anonymous: true, } // field used for embedding fx.Out type in generated struct. _outAnnotationField = reflect.StructField{ Name: "Out", - Type: reflect.TypeOf(Out{}), + Type: reflect.TypeFor[Out](), Anonymous: true, } ) @@ -120,7 +120,7 @@ type Annotation interface { } var ( - _typeOfError = reflect.TypeOf((*error)(nil)).Elem() + _typeOfError = reflect.TypeFor[error]() _nilError = reflect.Zero(_typeOfError) ) @@ -661,8 +661,8 @@ func (la *lifecycleHookAnnotation) build(ann *annotated) (any, error) { } var ( - _typeOfLifecycle = reflect.TypeOf((*Lifecycle)(nil)).Elem() - _typeOfContext = reflect.TypeOf((*context.Context)(nil)).Elem() + _typeOfLifecycle = reflect.TypeFor[Lifecycle]() + _typeOfContext = reflect.TypeFor[context.Context]() ) // validateHookDeps validates the dependencies of a hook function and returns true if the dependencies are valid. @@ -1299,7 +1299,7 @@ func (at *asAnnotation) apply(ann *annotated) error { continue } t := reflect.TypeOf(typ) - if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Interface { + if t.Kind() != reflect.Pointer || t.Elem().Kind() != reflect.Interface { return fmt.Errorf("fx.As: argument must be a pointer to an interface: got %v", t) } t = t.Elem() @@ -1498,7 +1498,7 @@ func (fr *fromAnnotation) apply(ann *annotated) error { return errors.New("fx.From: cannot annotate a variadic argument") } t := reflect.TypeOf(typ) - if t == nil || t.Kind() != reflect.Ptr { + if t == nil || t.Kind() != reflect.Pointer { return fmt.Errorf("fx.From: argument must be a pointer to a type that implements some interface: got %v", t) } fr.types[i] = t.Elem() diff --git a/app_unixes.go b/app_unixes.go index 8de1c509c..bcc472777 100644 --- a/app_unixes.go +++ b/app_unixes.go @@ -19,7 +19,6 @@ // THE SOFTWARE. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris -// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris package fx diff --git a/app_wasm.go b/app_wasm.go index fb4197a5a..0cfe84ad2 100644 --- a/app_wasm.go +++ b/app_wasm.go @@ -19,7 +19,6 @@ // THE SOFTWARE. //go:build (js && wasm) || (wasip1 && wasm) -// +build js,wasm wasip1,wasm package fx diff --git a/app_windows.go b/app_windows.go index cbd571408..723950528 100644 --- a/app_windows.go +++ b/app_windows.go @@ -19,7 +19,6 @@ // THE SOFTWARE. //go:build windows -// +build windows package fx diff --git a/app_windows_test.go b/app_windows_test.go index 795b75ac7..11680ff68 100644 --- a/app_windows_test.go +++ b/app_windows_test.go @@ -19,7 +19,6 @@ // THE SOFTWARE. //go:build windows -// +build windows package fx_test diff --git a/docs/ex/annotate/cast_bad.go b/docs/ex/annotate/cast_bad.go index c2ae3a882..c7d9960bc 100644 --- a/docs/ex/annotate/cast_bad.go +++ b/docs/ex/annotate/cast_bad.go @@ -19,7 +19,6 @@ // THE SOFTWARE. //go:build ignore -// +build ignore package annotate diff --git a/docs/ex/value-groups/consume/param.go b/docs/ex/value-groups/consume/param.go index 8fedec3f5..86a965a92 100644 --- a/docs/ex/value-groups/consume/param.go +++ b/docs/ex/value-groups/consume/param.go @@ -23,7 +23,7 @@ package consume import "go.uber.org/fx" // Watcher watches for events. -type Watcher interface{} +type Watcher any // ParamsModule is the module defined in this file. var ParamsModule = fx.Options( diff --git a/docs/ex/value-groups/feed/result.go b/docs/ex/value-groups/feed/result.go index 60621d235..88672ffdb 100644 --- a/docs/ex/value-groups/feed/result.go +++ b/docs/ex/value-groups/feed/result.go @@ -30,7 +30,7 @@ var ResultModule = fx.Options( ) // Watcher watches for events. -type Watcher interface{} +type Watcher any type watcher struct{} diff --git a/docs/src/container.md b/docs/src/container.md index 2e3327988..c5fbbad96 100644 --- a/docs/src/container.md +++ b/docs/src/container.md @@ -18,8 +18,8 @@ type App func (app *App) Run() type Option - func Provide(constructors ...interface{}) Option - func Invoke(funcs ...interface{}) Option + func Provide(constructors ...any) Option + func Invoke(funcs ...any) Option ``` Check the [API Reference](https://pkg.go.dev/go.uber.org/fx#Option) @@ -82,7 +82,7 @@ it can only be used for non-interface values. on runtime reflection to determine the type of the value. Passing an interface value to `fx.Supply` is a lossy operation: - it loses the original interface type, only giving us `interface{}`, + it loses the original interface type, only giving us `any`, at which point reflection will only reveal the concrete type of the value. For example, consider: diff --git a/extract.go b/extract.go index 713216c83..e07ff36a4 100644 --- a/extract.go +++ b/extract.go @@ -27,7 +27,7 @@ import ( "unicode/utf8" ) -var _typeOfIn = reflect.TypeOf(In{}) +var _typeOfIn = reflect.TypeFor[In]() // Extract fills the given struct with values from the dependency injection // container on application initialization. The target MUST be a pointer to a @@ -37,7 +37,7 @@ var _typeOfIn = reflect.TypeOf(In{}) func Extract(target any) Option { v := reflect.ValueOf(target) - if t := v.Type(); t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { + if t := v.Type(); t.Kind() != reflect.Pointer || t.Elem().Kind() != reflect.Struct { return Error(fmt.Errorf("Extract expected a pointer to a struct, got a %v", t)) } @@ -99,7 +99,7 @@ func Extract(target any) Option { // https://github.com/golang/go/issues/21122 t := f.Type - if t.Kind() == reflect.Ptr { + if t.Kind() == reflect.Pointer { t = t.Elem() } diff --git a/internal/lifecycle/lifecycle.go b/internal/lifecycle/lifecycle.go index 5abca666d..51fbcb380 100644 --- a/internal/lifecycle/lifecycle.go +++ b/internal/lifecycle/lifecycle.go @@ -41,10 +41,10 @@ import ( // function type that cannot be converted to an underlying function type with // a conventional conversion or type switch. var ( - _reflFunc = reflect.TypeOf(Func(nil)) - _reflErrorFunc = reflect.TypeOf(ErrorFunc(nil)) - _reflContextFunc = reflect.TypeOf(ContextFunc(nil)) - _reflContextErrorFunc = reflect.TypeOf(ContextErrorFunc(nil)) + _reflFunc = reflect.TypeFor[Func]() + _reflErrorFunc = reflect.TypeFor[ErrorFunc]() + _reflContextFunc = reflect.TypeFor[ContextFunc]() + _reflContextErrorFunc = reflect.TypeFor[ContextErrorFunc]() ) // Discrete function signatures that are allowed as part of a [Callable]. diff --git a/populate.go b/populate.go index 2ace21488..6af557675 100644 --- a/populate.go +++ b/populate.go @@ -66,7 +66,7 @@ func Populate(targets ...any) Option { fields := make([]reflect.StructField, len(targets)+1) fields[0] = reflect.StructField{ Name: "In", - Type: reflect.TypeOf(In{}), + Type: reflect.TypeFor[In](), Anonymous: true, } for i, t := range targets { @@ -85,7 +85,7 @@ func Populate(targets ...any) Option { default: rt = reflect.TypeOf(t) } - if rt.Kind() != reflect.Ptr { + if rt.Kind() != reflect.Pointer { return Error(fmt.Errorf("failed to Populate: target %v is not a pointer type, got %T", i+1, t)) } fields[i+1] = reflect.StructField{ diff --git a/provide.go b/provide.go index f55348966..64fed159b 100644 --- a/provide.go +++ b/provide.go @@ -169,7 +169,7 @@ func runProvide(c container, p provide, opts ...dig.ProvideOption) error { for i := 0; i < ft.NumOut(); i++ { t := ft.Out(i) - if t == reflect.TypeOf(Annotated{}) { + if t == reflect.TypeFor[Annotated]() { return fmt.Errorf( "fx.Annotated should be passed to fx.Provide directly, "+ "it should not be returned by the constructor: "+ diff --git a/shutdown_test.go b/shutdown_test.go index 13edc23b4..f44f21590 100644 --- a/shutdown_test.go +++ b/shutdown_test.go @@ -189,7 +189,6 @@ func TestDataRace(t *testing.T) { // the signal received. wg.Add(N) for i := range N { - i := i go func() { defer wg.Done() <-ready diff --git a/tools/analysis/passes/allfxevents/analysis.go b/tools/analysis/passes/allfxevents/analysis.go index 7ad5c8746..272b17ce9 100644 --- a/tools/analysis/passes/allfxevents/analysis.go +++ b/tools/analysis/passes/allfxevents/analysis.go @@ -58,7 +58,7 @@ var _filter = []ast.Node{ &ast.TypeAssertExpr{}, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { fxeventPkg, ok := findPackage(pass.Pkg, "go.uber.org/fx/fxevent") if !ok { // If the package doesn't import fxevent, and itself isn't @@ -261,7 +261,7 @@ func (ts *typeSet) Remove(t types.Type) (found bool) { // Iterate iterates through the type set in an unspecified order. func (ts *typeSet) Iterate(f func(types.Type)) { - ts.m.Iterate(func(t types.Type, _ interface{}) { + ts.m.Iterate(func(t types.Type, _ any) { f(t) }) }