From e4e87d137cac6eed3cddaa83679d23c0785f4165 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 18 Nov 2024 22:39:22 +0200 Subject: [PATCH] refactor: simplify with strings.Prefix, strings.CutPrefix --- cli/main.go | 5 +---- rule/exported.go | 4 ++-- rule/struct_tag.go | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cli/main.go b/cli/main.go index c00f4b012..992e52dc8 100644 --- a/cli/main.go +++ b/cli/main.go @@ -189,10 +189,7 @@ func getVersion(builtBy, date, commit, version string) string { if version == defaultVersion { bi, ok := debug.ReadBuildInfo() if ok { - version = bi.Main.Version - if strings.HasPrefix(version, "v") { - version = strings.TrimLeft(bi.Main.Version, "v") - } + version = strings.TrimPrefix(bi.Main.Version, "v") if len(buildInfo) == 0 { return fmt.Sprintf("version %s\n", version) } diff --git a/rule/exported.go b/rule/exported.go index b9f1e4687..a9eeb83f8 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -252,8 +252,8 @@ func (w *lintExported) lintTypeDoc(t *ast.TypeSpec, doc *ast.CommentGroup) { if t.Name.Name == a { continue } - if strings.HasPrefix(s, a+" ") { - s = s[len(a)+1:] + var found bool + if s, found = strings.CutPrefix(s, a+" "); found { break } } diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 4dd927827..ac7ac210d 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -139,8 +139,8 @@ func (lintStructTagRule) getTagName(tag *structtag.Tag) string { switch tag.Key { case keyProtobuf: for _, option := range tag.Options { - if strings.HasPrefix(option, "name=") { - return strings.TrimPrefix(option, "name=") + if tagName, found := strings.CutPrefix(option, "name="); found { + return tagName } } return "" // protobuf tag lacks 'name' option