You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
staticcheck: flag string(T) where T is a newtype of uint8 that has a String() method #1119
(Filing separately from #682, since this is something outside of go vet's check; feel free to merge.)
package main
import"fmt"typenewtypeuint8func (vnewtype) String() (sstring) { return"newtype" }
funcmain() {
// Example Afmt.Println(string(64))
// Example Bfmt.Println(string(newtype(64)))
// Example Cfmt.Println(newtype(64).String())
}
go vet will mark example A but not example B, probably because it still allows string(byte) conversions and thinks that newtype is the same as byte.
staticcheck (as of v0.3.0-0.dev.0.20211107063326-8cad455d2ffe) doesn't report any errors. There is already an issue about reproducing the original go vet check, but I think that this one is different, because this one concerns newtypes, and in particular ones that have their own String() method. I think that if the programmer (1) defined a new type and (2) added a String() method for it then it is extremely unlikely that they would want string(T) as opposed to T.String() anywhere in their code.
(Also, apologies for opening a blank issue instead of using a template. A force of habit 😅.)
Marking as aggressive because I'm not sure this would fit into staticcheck, and marking as needs-decision because I'm not sure it fits at all. I can imagine valid code that would do this, for example in the context of binary serialization. The type would represent some field in a binary file format, its String method returns a human-friendly representation (such as the name of an enum), and string(b) would be used while building output to write. It's somewhat far-fetched, however, because binary serialization shouldn't be using strings.
Activity
dominikh commentedon Nov 10, 2021
Marking as
aggressivebecause I'm not sure this would fit intostaticcheck, and marking asneeds-decisionbecause I'm not sure it fits at all. I can imagine valid code that would do this, for example in the context of binary serialization. The type would represent some field in a binary file format, its String method returns a human-friendly representation (such as the name of an enum), and string(b) would be used while building output to write. It's somewhat far-fetched, however, because binary serialization shouldn't be using strings.