Go-tools: staticcheck: SA5002 claims `for false {}` will spin but `for { continue }` won't

Created on 9 Nov 2018  路  1Comment  路  Source: dominikh/go-tools

package main

func main() {
    for { // L4
    }
    for {
        continue
    }
    for false { // L10
    }
    for false {
        continue
    }
}
$ staticcheck main.go 
main.go:4:2: this loop will spin, using 100% CPU (SA5002)
main.go:10:2: loop condition never changes or has a race condition (SA5002)
main.go:10:2: this loop will spin, using 100% CPU (SA5002)
false-positive

Most helpful comment

main.go:10:2: this loop will spin, using 100% CPU

Is a bug that we should fix, though not a high priority one.

That we don't detect for { continue } is acceptable. We don't strive to find all bugs. There are an infinite number of programs that spin without doing any useful work, but only for {} is commonly used by beginners. It isn't worth making the check vastly more complex to catch artificial bugs.

>All comments

main.go:10:2: this loop will spin, using 100% CPU

Is a bug that we should fix, though not a high priority one.

That we don't detect for { continue } is acceptable. We don't strive to find all bugs. There are an infinite number of programs that spin without doing any useful work, but only for {} is commonly used by beginners. It isn't worth making the check vastly more complex to catch artificial bugs.

Was this page helpful?
0 / 5 - 0 ratings