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

iterate-over-empty-map.yaml false positives #47

Open
GrosQuildu opened this issue Jan 18, 2024 · 2 comments
Open

iterate-over-empty-map.yaml false positives #47

GrosQuildu opened this issue Jan 18, 2024 · 2 comments

Comments

@GrosQuildu
Copy link
Collaborator

The iterate-over-empty-map.yaml should exclude from results cases when the map is passed to a function - the function probably will write to it. We can use cross-function analysis from pro engine to check if there really are the writes.

package main

import "fmt"

func Do(y map[string]int64) int64 {
	y["a"] = 1
	y["b"] = 2
	return 3
}

func main() {
	fmt.Println("Hello, 世界")
	x := map[string]int64{}
	Do(x)
	fmt.Printf("%d\n", len(x))
	for k, v := range x {
		fmt.Println(k, v)
	}
}
@paleboot
Copy link

paleboot commented Dec 5, 2024

Hi! Thanks for all these rules you folks came up with.

We encountered another false positive as well for this rule:

package main

import "strings"

var aMap = map[string]struct{}{
    "prefix1": {},
    "prefix2": {},
}

func iter2(s string) {
     for key := range aMap {
           if strings.HasPrefix(key, s) {
                return true
           }
     }
     return false
}

Playground:
Image

@GrosQuildu
Copy link
Collaborator Author

This one is because of #36
Waiting for upstream fix. Thanks for reporting the issue anyway!

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

No branches or pull requests

2 participants