Skip to content
/ gencon Public

Analyzer: generator of type constraints for Go

License

Notifications You must be signed in to change notification settings

arkuchy/gencon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gencon Go Reference

gencon is an analyzer which reports unnecessary type constraints any.

gencon consists of two command, gencon and fixgencon.

  • gencon
    • finds type constraints any and gives hints of type constraints.
  • fixgencon
    • finds type constraints any and fix any to appropriate type constraints.

requirement

gencon and fixgencon requires Go 1.18+.

gencon command

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.

install

$ go install github.com/arkuchy/gencon/cmd/gencon@latest

usage

$ go vet -vettool=`which gencon` pkgname

fixgencon

fixgencon 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.

install

$ go install github.com/arkuchy/gencon/cmd/fixgencon@latest

usage

$ fixgencon ./...

About

Analyzer: generator of type constraints for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages