-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: cmd/vet: report len usage with strings #50166
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
How would you measure the size of a string if every use of |
@seankhliao Thanks for correcting the title of the issue. To answer your question regarding measuring the size of a string |
vet checks are for things which are probably wrong, at the very least, and which wouldn't be the correct and idiomatic way to do a thing. I think that the behavior of I've only been using go for a few years but I don't think I've ever yet actually wanted to measure a string's length in runes. |
Have you looked at how much code this would mark as invalid for a very marginal benefit? |
I think this fails the 3rd criterion for new
Just skimming over e.g. the |
Consider:
All of these are widely used and correct usages of len on string. Take a look at the correctness criteria for "cmd/vet" https://github.com/golang/go/blob/master/src/cmd/vet/README . I do not see a path forward without making the proposed check narrower than |
This is impractical because getting the length of bytes is the most common use case and such change would break backward compatibility with all existing Go code to date. If you believe something should be done about this, my suggestion to you is to open a proposal for adding to the |
I have more than once seen unexpected behaviour, mostly related to string validation, caused by the
len
built-in function returning the count of bytes in the string, rather than the number of runes. For example:In my experience, the author of code similar to the above is nearly always looking to get the number of runes in the string - not the bytes needed to represent when encoded as UTF-8. Of course, the code above is fully legal and therefore this often unexpected behavior does not surface until runtime.
I believe a go vet check that warns about using
len
in combination with a string argument would be valuable - especially to programmers that are learning Go.The text was updated successfully, but these errors were encountered: