Skip to content

Feature Request: Static check involving the maps package #1621

Open
@zoltanhalassy

Description

The standard library maps package and its golang.org/x/exp/maps variant allowed newcomers to write code in a suboptimal way. Some examples found:

// exp maps, OSPACE(n) complexity
for _, k := range maps.Keys(m) { /*...*/ }
// standard library maps
for k := range maps.Keys(m) { /*...*/ }
// idiomatic
for k := range m { /*...*/ }
// exp maps, OSPACE(n) complexity
for _, v := range maps.Values(m) { /*...*/ }
// standard library maps
for v := range maps.Values(m) { /*...*/ }
// idiomatic
for _, v := range m { /*...*/ }
// exp maps, OSPACE(n) compexity, OTIME(n) complexity
if slices.Contains(maps.Keys(m), k) { /*...*/ }
// idiomatic
if _, ok := m[k]; ok { /*...*/ }

in one case, we also saw:

// standard library maps
for k, v := range maps.All(m) { /*...*/ }
// idiomatic
for k, v := range m { /*...*/ }

It would be nice to show warnings for:

  • exp maps use, as the standard library has feature parity
  • maps package use where it's not necessary, like directly in a range loop

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions