gordonklaus / ineffassign

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

allow ineffectual assignments to pointers #62

Closed mpldr closed 3 years ago

mpldr commented 3 years ago
func (s *Token) RPCCreate(req CreationRequest, response *Token) {               
        response = NewPermanent(req.Application, &req.Permissions, req.Expiration)
}

This triggers an ineffectual assignment warning but it is not. The response is evaluated by the request sender.

gordonklaus commented 3 years ago

You misunderstand how pointers (and pass-by-value) work. The caller of RPCCreate will not see its response modified. Try removing the assignment and see if the program behavior changes (it won't).

If you want to modify what response points to, then you need to write something like:

        *response = *NewPermanent(req.Application, &req.Permissions, req.Expiration)

but that code kind of smells. More likely RPCCreate should return the response.

mpldr commented 3 years ago

Thank you very much. I didn't even notice that I missed dereferencing it.

but that code kind of smells. More likely RPCCreate should return the response.

I agree, would be more intuitive, but that's unfortunately not how net/rpc works.