dgryski/semgrep-go

Check against for x := range <-ch?

ainar-g opened this issue · 0 comments

An interesting gotcha I found recently, which could be dangerous in overly-dynamically-typed code:

ch := make(chan []int, 3)
ch <- []int{10, 20, 30}
close(ch)

for s := range <-ch {
        fmt.Println(s)
}

As opposed to:

[10 20 30]

This prints:

0
1
2

Because what the user actually wanted to write is probably:

for s := range ch {
        fmt.Println(s)
}

Without the <-.