Open ainar-g opened 4 years ago
From an implementation point of view: the anonymous function refers to err
, which means that the variable can't be lifted into registers; it'll be an allocation. The way we check for unread assignments is by checking whether the register gets read.
For allocations, this isn't nearly as trivial (nor solvable for all cases.) For example, err
could be aliased and read via the alias, or g
could panic, leading to the deferred function reading the value. We could handle some of these cases (e.g. handle allocations that can't be aliased), but for your concrete example, we still couldn't claim that err
is unread, due to the possibility of g
panicking and thus reading that value.
Formalities
The code itself: https://play.golang.org/p/97Vr6raQX-i.
On line 21, an error check is missing, but because of the following
defer
,staticcheck
doesn't complain:I remember a similar bug, but I couldn't find the issue.