From 7f7f27f992850a06551c2798a3b874f5d5356ae9 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Tue, 27 Sep 2022 19:28:39 +0800 Subject: [PATCH] cmd/go: using strings.CutSuffix replace strings.HasSuffix and strings.TrimSuffix Change-Id: I79854419091d6c5c5c2922a1f45a3c5589673f11 Reviewed-on: https://go-review.googlesource.com/c/go/+/435138 Run-TryBot: xie cui <523516579@qq.com> TryBot-Result: Gopher Robot Reviewed-by: Dmitri Shuralyov Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills Run-TryBot: Bryan Mills --- src/cmd/go/internal/modfetch/cache.go | 3 +-- src/cmd/go/internal/modfetch/codehost/git.go | 4 ++-- src/cmd/go/internal/modfetch/fetch.go | 4 ++-- src/cmd/go/internal/work/exec.go | 8 ++++---- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/cmd/go/internal/modfetch/cache.go b/src/cmd/go/internal/modfetch/cache.go index c1ed18736c..928eb1f70e 100644 --- a/src/cmd/go/internal/modfetch/cache.go +++ b/src/cmd/go/internal/modfetch/cache.go @@ -710,8 +710,7 @@ func rewriteVersionList(dir string) (err error) { // involved in module graph construction, many *.zip files // will never be requested. name := info.Name() - if strings.HasSuffix(name, ".mod") { - v := strings.TrimSuffix(name, ".mod") + if v, found := strings.CutSuffix(name, ".mod"); found { if v != "" && module.CanonicalVersion(v) == v { list = append(list, v) } diff --git a/src/cmd/go/internal/modfetch/codehost/git.go b/src/cmd/go/internal/modfetch/codehost/git.go index ac2dc2348e..7127d6afe9 100644 --- a/src/cmd/go/internal/modfetch/codehost/git.go +++ b/src/cmd/go/internal/modfetch/codehost/git.go @@ -262,8 +262,8 @@ func (r *gitRepo) loadRefs() (map[string]string, error) { } } for ref, hash := range refs { - if strings.HasSuffix(ref, "^{}") { // record unwrapped annotated tag as value of tag - refs[strings.TrimSuffix(ref, "^{}")] = hash + if k, found := strings.CutSuffix(ref, "^{}"); found { // record unwrapped annotated tag as value of tag + refs[k] = hash delete(refs, ref) } } diff --git a/src/cmd/go/internal/modfetch/fetch.go b/src/cmd/go/internal/modfetch/fetch.go index 2e8c4c8aca..003bc79fe9 100644 --- a/src/cmd/go/internal/modfetch/fetch.go +++ b/src/cmd/go/internal/modfetch/fetch.go @@ -698,9 +698,9 @@ func addModSumLocked(mod module.Version, h string) { func checkSumDB(mod module.Version, h string) error { modWithoutSuffix := mod noun := "module" - if strings.HasSuffix(mod.Version, "/go.mod") { + if before, found := strings.CutSuffix(mod.Version, "/go.mod"); found { noun = "go.mod" - modWithoutSuffix.Version = strings.TrimSuffix(mod.Version, "/go.mod") + modWithoutSuffix.Version = before } db, lines, err := lookupSumDB(mod) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index 198d6081bb..ca2f5a80b3 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -642,12 +642,12 @@ OverlayLoop: var sourceFile string var coverFile string var key string - if strings.HasSuffix(file, ".cgo1.go") { + if base, found := strings.CutSuffix(file, ".cgo1.go"); found { // cgo files have absolute paths - base := filepath.Base(file) + base = filepath.Base(base) sourceFile = file - coverFile = objdir + base - key = strings.TrimSuffix(base, ".cgo1.go") + ".go" + coverFile = objdir + base + ".cgo1.go" + key = base + ".go" } else { sourceFile = filepath.Join(p.Dir, file) coverFile = objdir + file -- 2.48.1