truncation in comparison 64->32 bit; cast the other operand to uint64 instead
88 }
89
90 got := LimitedUint32(test.max)
91 if got > uint32(test.max) { 92 t.Errorf("more than %v. got: %v", test.max, got)
93 }
94 })
Description
When integers of different sizes are compared, the integer in the larger type is truncated to the size of the integer in the smaller type.
In the following example, if the value in x
exceeds 16 bits, the truncation will
result in unwanted results as x
is of type int32
. Hence, y
should instead go
through type conversion, i.e., x < int32(y)
so that it is comparable.
Bad practice
func f(x int32, y int16) bool {
return int16(x) < y
}
Recommended
func f(x int32, y int16) bool {
return x < int32(y)
}