Can and should unconvert simplify this case?
dmitshur opened this issue · 5 comments
Suppose there's a declaration of a []int32
slice done in the following way:
x := []int32{
(int32)(1),
(int32)(5),
(int32)(8),
}
Is there a reason unconvert
can't or shouldn't simplify it to:
x := []int32{
1,
5,
8,
}
Since constants are used, the type conversion is not needed, as the constants will have their types inferred correctly from the context. Any thoughts?
Nice, I agree the type conversions there are unnecessary because of context, just like in
var x int32 = int32(1)
It should also work the same for struct and map literals.
Great!
As I understand, this will only be possible in some cases. For example, if it looks like this:
var x, y, z = 1, 2, 3
s := []int32{
int32(x),
int32(y),
int32(z),
4,
5,
}
Then it won't be possible, since variables x
, y
and z
will need to be converted. But if they were declared as int32
, then it could be dropped.
It should also work the same for struct and map literals.
Yep. Also arrays. I didn't mention others because I wanted to start the conversation simpler.
Correct, in the int32(x)
case, x
's type is int
, so it's a necessary conversion. It's only unnecessary if x is untyped or int32 already.
I suspect this issue is also related to #20 (comment). Specifically, it could be that the reason this case isn't detected and reported because the constants would become untyped as a result.