blizzy78 / varnamelen

Go analyzer checking that the length of a variable's name matches its usage scope

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feature: ability to ignore variables of particular type (results of functions work)

butuzov opened this issue · comments

It would be great if linter can find the type of variable as a result of the function assigment. Like in next example.

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		print("1")
		print("1")
		print("1")
		print("1") 
		print("1")
		return c.String(http.StatusOK, "Hello, World!")
	})
	e.Logger.Fatal(e.Start(":1323"))
}

For example this is how we can make it working now.

func main() {
	var e *echo.Echo
	e = echo.New()
	e.GET("/", func(c echo.Context) error {
		print("1")
		print("1")
		print("1")
		print("1") 
		print("1")
		return c.String(http.StatusOK, "Hello, World!")
	})
	e.Logger.Fatal(e.Start(":1323"))
}

I am using it as part of golangci-lint with the next configuration.

linter-settings:
  varnamelen:
    ignore-decls:
    - c echo.Context 
    - e *echo.Echo

varnamelen can now infer the type of expressions, so this should work now.

This will be in varnamelen 0.6.0. Please note that it may take a while before 0.6.0 will make it into golangci-lint.

One more note: The current implementation works for one-to-one single-valued assignments only. It shouldn't be very hard to support multi-assignments as well in the future, though.

varnamelen v0.7.0 contains improvements to the way the types of idents are inferred, so multi-value assigments should work now, too.