Skip to content

Commit e1e1550

Browse files
committed
analysis/lint: accept new Go versions
Go versions in go.mod files can now look like "go1.21rc5" or "go1.21.0". See https://go.dev/doc/toolchain#versions. Closes: gh-1431 (cherry picked from commit ac367e4)
1 parent cdf983c commit e1e1550

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

analysis/lint/lint.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go/ast"
99
"go/build"
1010
"go/token"
11+
"regexp"
1112
"strconv"
1213
"strings"
1314

@@ -206,21 +207,28 @@ func (v *VersionFlag) String() string {
206207
return fmt.Sprintf("1.%d", *v)
207208
}
208209

209-
func (v *VersionFlag) Set(s string) error {
210-
if len(s) < 3 {
211-
return fmt.Errorf("invalid Go version: %q", s)
212-
}
213-
if s[0] != '1' {
214-
return fmt.Errorf("invalid Go version: %q", s)
215-
}
216-
if s[1] != '.' {
217-
return fmt.Errorf("invalid Go version: %q", s)
210+
var goVersionRE = regexp.MustCompile(`^(?:go)?1.(\d+).*$`)
211+
212+
// ParseGoVersion parses Go versions of the form 1.M, 1.M.N, or 1.M.NrcR, with an optional "go" prefix. It assumes that
213+
// versions have already been verified and are valid.
214+
func ParseGoVersion(s string) (int, bool) {
215+
m := goVersionRE.FindStringSubmatch(s)
216+
if m == nil {
217+
return 0, false
218218
}
219-
i, err := strconv.Atoi(s[2:])
219+
n, err := strconv.Atoi(m[1])
220220
if err != nil {
221+
return 0, false
222+
}
223+
return n, true
224+
}
225+
226+
func (v *VersionFlag) Set(s string) error {
227+
n, ok := ParseGoVersion(s)
228+
if !ok {
221229
return fmt.Errorf("invalid Go version: %q", s)
222230
}
223-
*v = VersionFlag(i)
231+
*v = VersionFlag(n)
224232
return nil
225233
}
226234

0 commit comments

Comments
 (0)