Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeafter: skip check on Go ≥ 1.23 #48

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading