Skip to content

Ineffassign does not appear to detect when a map is written, but never read from #88

@jvmatl

Description

@jvmatl

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)                                                                                                                                                                                           
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions