From edd0baa936b3aa584c0579aa48f2ab54b2e48e55 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 22 Oct 2023 19:51:34 +0200 Subject: [PATCH] enable gocritic lint Signed-off-by: Matthieu MOREL --- .golangci.yml | 1 + templates/cc/register.go | 2 +- templates/goshared/register.go | 9 +++++---- templates/java/register.go | 16 ++++++++-------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 809a572ec..908ec9914 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,6 +3,7 @@ linters: - unused enable: - gci + - gocritic - gofumpt - govet - misspell diff --git a/templates/cc/register.go b/templates/cc/register.go index dcc5ed6a6..da773fcd1 100644 --- a/templates/cc/register.go +++ b/templates/cc/register.go @@ -436,7 +436,7 @@ func (fns CCFuncs) failUnimplemented(message string) string { } func (fns CCFuncs) staticVarName(msg pgs.Message) string { - return "validator_" + strings.Replace(fns.className(msg), ":", "_", -1) + return "validator_" + strings.ReplaceAll(fns.className(msg), ":", "_") } func (fns CCFuncs) output(file pgs.File, ext string) string { diff --git a/templates/goshared/register.go b/templates/goshared/register.go index e4f2aed43..19a2a00ae 100644 --- a/templates/goshared/register.go +++ b/templates/goshared/register.go @@ -114,11 +114,12 @@ func (fns goSharedFuncs) errIdxCause(ctx shared.RuleContext, idx, cause string, n := fns.Name(f) var fld string - if idx != "" { + switch { + case idx != "": fld = fmt.Sprintf(`fmt.Sprintf("%s[%%v]", %s)`, n, idx) - } else if ctx.Index != "" { + case ctx.Index != "": fld = fmt.Sprintf(`fmt.Sprintf("%s[%%v]", %s)`, n, ctx.Index) - } else { + default: fld = fmt.Sprintf("%q", n) } @@ -395,7 +396,7 @@ func (fns goSharedFuncs) enumPackages(enums []pgs.Enum) map[pgs.Name]NormalizedE if collision, ok := nameCollision[pkgName]; ok { nameCollision[pkgName] = collision + 1 - pkgName = pkgName + pgs.Name(strconv.Itoa(nameCollision[pkgName])) + pkgName += pgs.Name(strconv.Itoa(nameCollision[pkgName])) } else { nameCollision[pkgName] = 0 } diff --git a/templates/java/register.go b/templates/java/register.go index c376ba5d1..baf6b7a68 100644 --- a/templates/java/register.go +++ b/templates/java/register.go @@ -124,14 +124,14 @@ func JavaFilePath(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.Fi return nil } - fullPath := strings.Replace(javaPackage(f), ".", string(os.PathSeparator), -1) + fullPath := strings.ReplaceAll(javaPackage(f), ".", string(os.PathSeparator)) fileName := classNameFile(f) + "Validator.java" filePath := pgs.JoinPaths(fullPath, fileName) return &filePath } func JavaMultiFilePath(f pgs.File, m pgs.Message) pgs.FilePath { - fullPath := strings.Replace(javaPackage(f), ".", string(os.PathSeparator), -1) + fullPath := strings.ReplaceAll(javaPackage(f), ".", string(os.PathSeparator)) fileName := classNameMessage(m) + "Validator.java" filePath := pgs.JoinPaths(fullPath, fileName) return filePath @@ -167,9 +167,9 @@ func classNameMessage(m pgs.Message) string { // When multiple files is false, underscores are stripped. Short of rewriting all the name sanitization // logic for java, using "UnderscoreUnderscoreUnderscore" is an escape sequence seems to work with an extremely // small likelihood of name conflict. - className = strings.Replace(className, "_", "UnderscoreUnderscoreUnderscore", -1) + className = strings.ReplaceAll(className, "_", "UnderscoreUnderscoreUnderscore") className = sanitizeClassName(className) - className = strings.Replace(className, "UnderscoreUnderscoreUnderscore", "_", -1) + className = strings.ReplaceAll(className, "UnderscoreUnderscoreUnderscore", "_") return className } @@ -442,10 +442,10 @@ func (fns javaFuncs) javaTypeLiteralSuffixForPrototype(t pgs.ProtoType) string { func (fns javaFuncs) javaStringEscape(s string) string { s = fmt.Sprintf("%q", s) s = s[1 : len(s)-1] - s = strings.Replace(s, `\u00`, `\x`, -1) - s = strings.Replace(s, `\x`, `\\x`, -1) - // s = strings.Replace(s, `\`, `\\`, -1) - s = strings.Replace(s, `"`, `\"`, -1) + s = strings.ReplaceAll(s, `\u00`, `\x`) + s = strings.ReplaceAll(s, `\x`, `\\x`) + // s = strings.ReplaceAll(s, `\`, `\\`) + s = strings.ReplaceAll(s, `"`, `\"`) return `"` + s + `"` }