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.

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Oct 30, 2024
1 parent 008966a commit 61aa912
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 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
13 changes: 13 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,18 @@ 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 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 61aa912

Please sign in to comment.