Skip to content

Commit 32a1a70

Browse files
tklauserrolinh
authored andcommitted
timeafter: skip check on Go ≥ 1.23
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]>
1 parent 008966a commit 32a1a70

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

go.mod

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ go 1.22.0
44

55
require (
66
golang.org/x/exp v0.0.0-20230809094429-853ea248256d
7+
golang.org/x/mod v0.21.0
78
golang.org/x/tools v0.26.0
89
)
910

10-
require (
11-
golang.org/x/mod v0.21.0 // indirect
12-
golang.org/x/sync v0.8.0 // indirect
13-
)
11+
require golang.org/x/sync v0.8.0 // indirect

timeafter/doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright Authors of Cilium
33

44
// Package timeafter defines an Analyzer that checks for the use of time.After
5-
// in loops.
5+
// in loops on Go versions before 1.23
66
//
77
// # Analyzer timeafter
88
//

timeafter/time_after.go

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go/ast"
1010
"strings"
1111

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

54+
var goVersion string
55+
if pass.Module != nil {
56+
goVersion = pass.Module.GoVersion
57+
if !strings.HasPrefix(goVersion, "v") {
58+
goVersion = "v" + goVersion
59+
}
60+
}
61+
if semver.Compare(goVersion, "v1.23.0") >= 0 {
62+
// Go version ≥ 1.23 no longer has the issue of not collecting unstopped Timers and
63+
// time.After can safely be used in loops. Also see
64+
// https://go.dev/doc/go1.23#timer-changes and
65+
// https://cs.opensource.google/go/go/+/refs/tags/go1.23.2:src/time/sleep.go;l=196-201
66+
return nil, nil
67+
}
68+
5369
ignoreMap := make(map[string]struct{})
5470
for _, ign := range strings.Split(ignoreArg, ",") {
5571
ignoreMap[strings.TrimSpace(ign)] = struct{}{}

0 commit comments

Comments
 (0)