esimonov / ifshort

Go linter for checking that your code uses short syntax for if-statements whenever possible.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ifshort difficult suggestion when the if already is short

jhi opened this issue · comments

commented

Using the ifshort v1.36.0 packaged with golangci-lint on the following code

package test

import "fmt"

func test() {
        p := map[string]int{}
        q := "bar"                                                                                                                                                   

        if _, ok := p[q]; ok {
                fmt.Println(q)
        }
}

elicits this warning

variable 'q' is only used in the if-statement (test.go:9:2); consider using short syntax (ifshort)

which is all good, but I do not off-hand see how that would work since the if statement already is using the short form, and there is dependency so that the q needs to be set first.

I can see some horrible ways of constructing small helper functions just for this one spot, but as I said, horrible.

While whittling this down from the original code, I noticed that using the q inside the if is important. If the q is not used there, the ifshort warning goes away.