-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a lint for struct
constructors
#2708
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
Comments
Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors. |
Hello, your linters seem really specific to your use case, I'm not sure there is a big demand for this type of linter. My recommendations:
|
Okay! Yep, make sense -- could I elaborate on this a bit more? Just to make it a bit more clear (because I do think this is a somewhat general pattern and tl;dr is for DI)! This is totally contrived, but: package metrics
struct Service {
cfg Config
client Client
}
func NewService(cfg Config, client Client) Service {
return Service { cfg, client }
}
func SomethingSilly() {
// All good here; this is just some data, so no need to use a ctor or anything.
cfg := Config{}
// Wait! I bypassed the ctor, so now I don't have the client set!
svc := Service { cfg: cfg }
}
// ...
package foo
func DoTheThing() {
// Wait! This definitely won't work!
metricsSvc := metrics.Service{}
} Does that illustrate this any better? |
I understand your use case, but your linter will only be useful if all the structs follow the same name pattern. So I'm still thinking that is a specific use case. But I don't represent the whole community, so we will see, with this issue, if there is some interest for this linter. |
Also without the code, I cannot see what is the approach you implemented in your linter, and it's difficult to judge. So I think that my recommendations can help:
|
Okay! Nope, that makes sense -- thanks for taking a look. Feel free to close this if you feel that makes sense! |
Your feature request related to a problem? Please describe.
I've run into runtime bugs when adding a new field to a "service-like"
struct
(aFooService
or something like that), but then I find out someone's instantiating thestruct
without a constructor so some code somewhere breaks at runtime because of anil
pointer!Describe the solution you'd like.
I'd love to see two linters created:
struct
s that should have a ctor, which don't have a ctorstruct
s that do have a ctor, but which are instantiated without using the ctorThese linters would definitely want to take regexes that specify the types of
structs
that we consider to be lintable -- moststruct
s don't have this problem since they're just data and we really don't even want a ctor for them.Describe alternatives you've considered.
Pray our tests catch this stuff! 🙏
Additional context.
I've written these linters already (and they're useful for me), but I didn't want to create a PR without gauging interest first.
I'm new to this project, so thanks for your patience!
The text was updated successfully, but these errors were encountered: