Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Static check involving the maps package #1621

Open
zoltanhalassy opened this issue Nov 28, 2024 · 1 comment
Open

Feature Request: Static check involving the maps package #1621

zoltanhalassy opened this issue Nov 28, 2024 · 1 comment

Comments

@zoltanhalassy
Copy link

zoltanhalassy commented Nov 28, 2024

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
@zoltanhalassy zoltanhalassy added the needs-triage Newly filed issue that needs triage label Nov 28, 2024
@dominikh dominikh added new-check and removed needs-triage Newly filed issue that needs triage labels Nov 28, 2024
@dominikh
Copy link
Owner

exp maps use, as the standard library has feature parity

We'll wait for the Go team to deprecate the package instead.

maps package use where it's not necessary, like directly in a range loop

I agree that these uses should be simplified and we can probably flag that.

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

No branches or pull requests

2 participants