Skip to content

Commit

Permalink
timeafter: skip check on Go ≥ 1.23
Browse files Browse the repository at this point in the history
Go version ≥ 1.23 no longer has the issue of not collecting unstopped
Timers and time.After can safely be used in loops. Also see
https://go.dev/doc/go1.23#timer-changes and
https://cs.opensource.google/go/go/+/refs/tags/go1.23.2:src/time/sleep.go;l=196-201

Thus we can skip the timeafter check on modules with go.mod specifying
go1.23 or later.

Example run:

```
$ mkdir timeafter && cd timeafter
$ cat <<EOF > main.go
package main

import "time"

func main() {
	for {
		select {
		case <-time.After(5 * time.Second):
		}
	}
}
$ cat <<EOF > go.mod
module timeafter

go 1.22.0
EOF
$ linters .
/home/tklauser/tmp/timeafter/main.go:8:10: use of time.After in a for loop is prohibited, use inctimer instead
$ sed -ie 's/go 1\.22\.0/go 1.23.0/' go.mod
$ linters .
$
```

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Oct 30, 2024
1 parent 008966a commit 841828c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.22.0

require (
golang.org/x/exp v0.0.0-20230809094429-853ea248256d
golang.org/x/mod v0.21.0
golang.org/x/tools v0.26.0
)

require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
)
require golang.org/x/sync v0.8.0 // indirect
2 changes: 1 addition & 1 deletion timeafter/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright Authors of Cilium

// Package timeafter defines an Analyzer that checks for the use of time.After
// in loops.
// in loops on Go versions before 1.23
//
// # Analyzer timeafter
//
Expand Down
16 changes: 16 additions & 0 deletions timeafter/time_after.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go/ast"
"strings"

"golang.org/x/mod/semver"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
Expand Down Expand Up @@ -50,6 +51,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, errors.New("analyzer is not type *inspector.Inspector")
}

var goVersion string
if pass.Module != nil {
goVersion = pass.Module.GoVersion
if !strings.HasPrefix(goVersion, "v") {
goVersion = "v" + goVersion
}
}
if semver.Compare(goVersion, "v1.23.0") >= 0 {
// Go version ≥ 1.23 no longer has the issue of not collecting unstopped Timers and
// time.After can safely be used in loops. Also see
// https://go.dev/doc/go1.23#timer-changes and
// https://cs.opensource.google/go/go/+/refs/tags/go1.23.2:src/time/sleep.go;l=196-201
return nil, nil
}

ignoreMap := make(map[string]struct{})
for _, ign := range strings.Split(ignoreArg, ",") {
ignoreMap[strings.TrimSpace(ign)] = struct{}{}
Expand Down

0 comments on commit 841828c

Please sign in to comment.