gencon is an analyzer which reports unnecessary type constraints any.
gencon consists of two command, gencon and fixgencon.
gencon- finds type constraints
anyand gives hints of type constraints.
- finds type constraints
fixgencon- finds type constraints
anyand fixanyto appropriate type constraints.
- finds type constraints
gencon and fixgencon requires Go 1.18+.
package a
func f[T any](t T) {} // want "should not use 'any'. hint: string|~int"
func invoke() { // OK
f("gopher")
f(1)
type MyInt int
f(MyInt(18))
}gencon reports the function only with any constraint, not other constraints such as comparable or constraints.Ordered.
If the function isn't called from anywhere, gencon reports without hint.
We can see example of gencon command report here.
$ go install github.com/arkuchy/gencon/cmd/gencon@latest$ go vet -vettool=`which gencon` pkgnamefixgencon is under development and supports only single type parameter.
--- before `fixgencon` ---
package a
func f[T any](t T) {} // want "should not use 'any'. hint: string|~int"
func g[T, U any, V int](t T, u U, v V) {} // want "should not use 'any'. hint: bool|int" "should not use 'any'. hint: string|~int"
func invoke() { // OK
f("gopher")
f(1)
type MyInt int
f(MyInt(18))
g(3, "gopher", 100)
g(true, MyInt(3), 100)
}
--- after `fixgencon` ---
package a
func f[T string | ~int](t T) {} // want "should not use 'any'. hint: string|~int"
func g[T, U any, V int](t T, u U, v V) {} // want "should not use 'any'. hint: bool|int" "should not use 'any'. hint: string|~int"
func invoke() { // OK
f("gopher")
f(1)
type MyInt int
f(MyInt(18))
g(3, "gopher", 100)
g(true, MyInt(3), 100)
}fixgencon doesn't fix any when the function isn't called from anywhere, because fixgencon doesn't know what to change from any.
$ go install github.com/arkuchy/gencon/cmd/fixgencon@latest$ fixgencon ./...