-
Notifications
You must be signed in to change notification settings - Fork 34
Description
In the code below, (which is obviously simplified from a larger context) map m is created, then an optional code path accidentally shadows the map, adds to the (wrong) map, and then the original map is accessed.
There are probably other lint-like tools which might warn about the unintentionally shadowed variable, but in this context I'm wondering if it would be possible to detect that ineffectual assignments were made to a map. (i.e. the inner map was created, then items were added to the map, then the whole map went out of scope, without anybody ever attempting to access a member of the map or to even check then number of items in the map.) It's effectively a /dev/null map.
Maybe this is out of scope for ineffassign, but I figured I'd mention it in case anybody had ideas on how to tackle the issue.
func MinimalRepro(option bool) int {
var m map[int]struct{}
if option {
m := make(map[int]struct{}) // BUG -- intended to be =, not :=
m[1] = struct{}{}
// m is never read from within this block, and len(m) is never analyzed, so all the assignments to the map are ineffectual
}
return len(m)
}