New linter to forbid unusual map keys (e.g. pointers & interfaces) #5172
Replies: 2 comments
-
First, just for the context:
https://go.dev/ref/spec#Map_types I can see 2 ways:
We can also think of map wrappers like https://go.dev/play/p/Py7hAS8U5JX I have the feeling this will lead to a lot of false positives 🤔 |
Beta Was this translation helpful? Give feedback.
-
Thank you for the feedback. I am aware that there are valid use cases for such map keys. I see some situations
Intention of the suggested linter isn't expert level code, but code written by young talents learning Go on the job an being surprised by those issues every now and then. Also catching effects of large scale refactorings when changing type usages from value to pointer and e.g. having a local type alias on it may even surprise expert Gophers. |
Beta Was this translation helpful? Give feedback.
-
It happens quite a few times that people add interfaces or pointer types as map keys.
Sometimes it can also be a regression introduced by an seemingly harmless change of a datatype can trigger an unintended change in a map key.
Of course there will be valid use cases for such keys, that's why the language allows them, but by default it usually is a correctness issue leading to duplicate data elsewhere.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions