gordonklaus / ineffassign

Detect ineffectual assignments in Go code.
MIT License
390 stars 22 forks source link

false report for assigining to the receiver pointer #59

Closed zhijianli88 closed 3 years ago

zhijianli88 commented 3 years ago

there is a false report in where it assigns to a receiver

https://github.com/kubernetes/test-infra/blob/master/def/configmap/main.go#L64

func (mkv *multiKeyValue) Set(v string) error {
    p := strings.SplitN(v, "=", 2)
    if len(p) != 2 {
        return fmt.Errorf("%s does not match label=value", v)
    }
    if mkv == nil {
        mkv = &multiKeyValue{
            p[0]: p[1],
        }
    } else {
        (*mkv)[p[0]] = p[1]
    }
    return nil
}
gordonklaus commented 3 years ago

Assigning to a receiver works the same way as assigning to a parameter: its effect is only visible within the function scope.

What you want is

*mkv = multiKeyValue{
    p[0]: p[1],
}