Skip to content

Commit

Permalink
analysis/lint: accept new Go versions
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
dominikh committed Aug 17, 2023
1 parent cdf983c commit e1e1550
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions analysis/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/ast"
"go/build"
"go/token"
"regexp"
"strconv"
"strings"

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

func (v *VersionFlag) Set(s string) error {
if len(s) < 3 {
return fmt.Errorf("invalid Go version: %q", s)
}
if s[0] != '1' {
return fmt.Errorf("invalid Go version: %q", s)
}
if s[1] != '.' {
return fmt.Errorf("invalid Go version: %q", s)
var goVersionRE = regexp.MustCompile(`^(?:go)?1.(\d+).*$`)

// ParseGoVersion parses Go versions of the form 1.M, 1.M.N, or 1.M.NrcR, with an optional "go" prefix. It assumes that
// versions have already been verified and are valid.
func ParseGoVersion(s string) (int, bool) {
m := goVersionRE.FindStringSubmatch(s)
if m == nil {
return 0, false
}
i, err := strconv.Atoi(s[2:])
n, err := strconv.Atoi(m[1])
if err != nil {
return 0, false
}
return n, true
}

func (v *VersionFlag) Set(s string) error {
n, ok := ParseGoVersion(s)
if !ok {
return fmt.Errorf("invalid Go version: %q", s)
}
*v = VersionFlag(i)
*v = VersionFlag(n)
return nil
}

Expand Down

0 comments on commit e1e1550

Please sign in to comment.