-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/vet: flag accidental integer division inside float casts #36681
Comments
I'm not convinced that Basically I'm not sure this rises to the level of a vet check, which is a fairly high level. It might be more appropriate for some other checker. |
Adopting this check would mean forcing people to write float64(int(a/b)) for integer a, b to express "no I really mean what the code says". That's pretty unfortunate. In general we do assume that people write what they mean. Vet flags obvious mistakes, but this is not an obvious mistake. There is a way to write an integer divide and a way to write a float divide. Both are reasonable in different circumstances. Note cmd/vet/README's "Precision" requirement, in particular that a check must not have a significant number of false positives. To move forward with this, we'd need evidence that essentially all instances of float64(a/b) are bugs. |
We need evidence to move forward with this, and we don't have any evidence. This seems like a likely decline. I also note that staticcheck doesn't check this either. |
Sorry for the delay. I mocked up a check to run over the k8s codebase, and came back with five hits, of which four are copies of each other (and harmless, if silly, since in context it amounts to This is possibly a slight underestimate since I was only looking for literal float{32,64} casts and there could be some other type with an underlying float type, but I'll accept that this doesn't meet the quality bar for a |
Even |
Correct, I share your concern about false positives. Staticcheck has a related check, however, which flags |
Thanks for taking the time to dig up numbers. |
We have caught bugs of the form seen in kubernetes/kubernetes#83132 (review) a number of times in Kubernetes — namely, code that writes
float64(some_int / another_int)
when what is clearly meant isfloat64(some_int) / another_int
.It strikes me that the likelihood that anyone would ever intentionally lose precision with an integer divison and then cast the known-to-be-integral result to
float64
is not necessarily exactly zero, but very low. In the rare cases where that is what's desired, it's easy to replacef := float64(a/b)
witht := a/b; f := float64(t)
and doing so more clearly communicates the intentional truncation.The proposal is to have
go vet
flag expressions of the formfloat32(x / y)
orfloat64(x / y)
wherex
andy
are expressions yielding a result of integral type.The text was updated successfully, but these errors were encountered: