blizzy78 / varnamelen

Go analyzer checking that the length of a variable's name matches its usage scope
MIT License
20 stars 2 forks source link

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

Closed butuzov closed 2 years ago

butuzov commented 2 years ago

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
blizzy78 commented 2 years ago

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.

butuzov commented 2 years ago

Awesome @blizzy78 !

blizzy78 commented 2 years ago

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.

blizzy78 commented 2 years ago

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