-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting to allow zero-values for structs #89
Comments
Another example I have in my code is |
@mitar I think the That said, I agree with the general utility of ignoring empty struct literals in general. It comes in handy in many situations. e.g. I have a pattern of namespacing constructors using |
@navijation For me, the |
I found this example in one of my libraries. |
It seems the linter requires the function to return something with exact type I'm inclined to believe that the linter should never complain about empty struct literals in return values for the reason @JakeCapra mentioned. That said, one workaround would be to add a named return value e.g. |
@navijation Any progress or update on this? |
Not yet - there is extra hard task on supporting comment directives on structures rather on field tags (tags will remain) As i am pretty tight on time now - anyone willing is free to come up with PR. |
@xobotyi there is a semantic ambiguity here for this "return nil of type which implements error":
Namely, for case 2, consider this example package main
import "fmt"
type SomeError struct{}
func (*SomeError) Error() string {
return "There was an error"
}
func returnsSomeError() *SomeError {
return nil
}
func main() {
var err error = returnsSomeError()
fmt.Printf("err is nil: %t\n", err == nil)
} A bigger problem is that the zero value of a struct has many valid use case where developers might not want to subject it to exhaustive enforcement. Perhaps a better solution is to ignore enforcement on one of the following categorizations (maybe based on an analyzer config):
I'm not sure which makes the most sense; I'm inclined to believe either 1 or 2. |
Hi, there is a PR #102 which I think is pretty related to what you mentioned here earlier. It does not address cases where you return custom error from function whose return type is the custom error itself, but it does eliminate false positives when you return a custom error on the position of the |
@navijation imo the way it shoul be done - the one it is done already, but with support for detection of custom error types. Therefore it can be detmined as |
Closing as #102 being merged. |
Thanks! |
@xobotyi Any idea when this will be released? |
I will release minor version in the evening, but have no idea about release plan of golangci-lint |
In my code I often have
return foobar{}, errors.New("some error")
to return an empty struct when there is an error. Currently linter complains about that.I can go around that by doing
return *new(foobar), errors.New("some error")
but that is too verbose for me.So I wonder, could there be a setting to allow
foobar{}
and not complain about it? So if you initialize without any field, then nothing happens. If you add any field, then linter requires to add all of them.The text was updated successfully, but these errors were encountered: