From d8cfa4eb689233ca88f85cfa62275f02870f3ecd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@upcloud.com>
Date: Mon, 15 Jan 2024 23:45:42 +0200
Subject: [PATCH] Address gocritic findings, enable it

---
 .golangci.yml             |  2 +-
 bash_completions.go       | 21 +++++++++------------
 command_test.go           |  2 +-
 doc/man_docs.go           |  2 +-
 doc/util.go               |  2 +-
 powershell_completions.go |  4 ++--
 6 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
index a618ec24..57c62c17 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -29,7 +29,7 @@ linters:
     - gas
     #- gochecknoinits
     - goconst
-    #- gocritic
+    - gocritic
     #- gocyclo
     #- gofmt
     - goimports
diff --git a/bash_completions.go b/bash_completions.go
index be835704..f4d198cb 100644
--- a/bash_completions.go
+++ b/bash_completions.go
@@ -597,19 +597,16 @@ func writeRequiredFlag(buf io.StringWriter, cmd *Command) {
 		if nonCompletableFlag(flag) {
 			return
 		}
-		for key := range flag.Annotations {
-			switch key {
-			case BashCompOneRequiredFlag:
-				format := "    must_have_one_flag+=(\"--%s"
-				if flag.Value.Type() != "bool" {
-					format += "="
-				}
-				format += cbn
-				WriteStringAndCheck(buf, fmt.Sprintf(format, flag.Name))
+		if _, ok := flag.Annotations[BashCompOneRequiredFlag]; ok {
+			format := "    must_have_one_flag+=(\"--%s"
+			if flag.Value.Type() != "bool" {
+				format += "="
+			}
+			format += cbn
+			WriteStringAndCheck(buf, fmt.Sprintf(format, flag.Name))
 
-				if len(flag.Shorthand) > 0 {
-					WriteStringAndCheck(buf, fmt.Sprintf("    must_have_one_flag+=(\"-%s"+cbn, flag.Shorthand))
-				}
+			if len(flag.Shorthand) > 0 {
+				WriteStringAndCheck(buf, fmt.Sprintf("    must_have_one_flag+=(\"-%s"+cbn, flag.Shorthand))
 			}
 		}
 	})
diff --git a/command_test.go b/command_test.go
index b7d88e4d..5d607ff1 100644
--- a/command_test.go
+++ b/command_test.go
@@ -2777,7 +2777,7 @@ func TestFind(t *testing.T) {
 
 func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {
 	testCases := [][]string{
-		//{"--unknown", "--namespace", "foo", "child", "--bar"}, // FIXME: This test case fails, returning the error `unknown command "foo" for "root"` instead of the expected error `unknown flag: --unknown`
+		// {"--unknown", "--namespace", "foo", "child", "--bar"}, // FIXME: This test case fails, returning the error `unknown command "foo" for "root"` instead of the expected error `unknown flag: --unknown`
 		{"--namespace", "foo", "--unknown", "child", "--bar"},
 		{"--namespace", "foo", "child", "--unknown", "--bar"},
 		{"--namespace", "foo", "child", "--bar", "--unknown"},
diff --git a/doc/man_docs.go b/doc/man_docs.go
index b8c15ce8..2138f248 100644
--- a/doc/man_docs.go
+++ b/doc/man_docs.go
@@ -133,7 +133,7 @@ func fillHeader(header *GenManHeader, name string, disableAutoGen bool) error {
 		}
 		header.Date = &now
 	}
-	header.date = (*header.Date).Format("Jan 2006")
+	header.date = header.Date.Format("Jan 2006")
 	if header.Source == "" && !disableAutoGen {
 		header.Source = "Auto generated by spf13/cobra"
 	}
diff --git a/doc/util.go b/doc/util.go
index 0aaa07a1..4de4ceee 100644
--- a/doc/util.go
+++ b/doc/util.go
@@ -40,7 +40,7 @@ func hasSeeAlso(cmd *cobra.Command) bool {
 // that do not contain \n.
 func forceMultiLine(s string) string {
 	if len(s) > 60 && !strings.Contains(s, "\n") {
-		s = s + "\n"
+		s += "\n"
 	}
 	return s
 }
diff --git a/powershell_completions.go b/powershell_completions.go
index 55195193..a830b7bc 100644
--- a/powershell_completions.go
+++ b/powershell_completions.go
@@ -28,8 +28,8 @@ import (
 func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) {
 	// Variables should not contain a '-' or ':' character
 	nameForVar := name
-	nameForVar = strings.Replace(nameForVar, "-", "_", -1)
-	nameForVar = strings.Replace(nameForVar, ":", "_", -1)
+	nameForVar = strings.ReplaceAll(nameForVar, "-", "_")
+	nameForVar = strings.ReplaceAll(nameForVar, ":", "_")
 
 	compCmd := ShellCompRequestCmd
 	if !includeDesc {