simple: check uneeded []byte or []string conversion for len builtin
Converting a string or []byte to the other type to compute the length is not needed.
Example: https://github.com/golang/net/blob/0ba52f642ac2f9371a88bfdde41f4b4e195a37c0/dns/dnsmessage/message.go#L1848
Looks like a typo in the title.
Not []byte or []string, but []byte or string.
+ json.RawMessage conversion
@Antonboom
Does the code you tried when playing with testifylint around len(string( and len([]byte( could be used to help to provide the linter needed here
could be used to help to provide the linter needed here
I don't mind if it makes sense.
My question was somehow to ask you if you plan to open a PR for fixing this issue
you plan to open a PR for fixing this issue
I don't plan to, it's out of my focus at the moment. My apologies 🙏
you plan to open a PR for fixing this issue
I don't plan to, it's out of my focus at the moment. My apologies 🙏
It's OK, so I know I can work on it if I find time
ruleguard pattern:
func lenStrByteSlice(m dsl.Matcher) {
// len(string([]byte)) -> len([]byte)
m.Match(`len(string($b))`).
Where(m["b"].Type.Underlying().Is("[]byte")).
Report(`Call len() on the byte slice instead of converting to a string first`).
Suggest(`len($b)`)
}
func lenByteSliceStr(m dsl.Matcher) {
// len([]byte(string)) -> len(string)
m.Match(`len([]byte($s))`).
Where(m["s"].Type.Underlying().Is("string")).
Report(`Call len() on the string instead of converting to []byte first.`).
Suggest(`len($s)`)
}
Thanks @dgryski
I should give a try at rule guard at some point !