Skip to content

Commit 699e994

Browse files
ci(linter): add go-critic (kubernetes-sigs#5875)
* ci: add go-critic linter follow go-critic advices when possible * docs: add a short description about go-critic
1 parent c1894f8 commit 699e994

File tree

36 files changed

+133
-121
lines changed

36 files changed

+133
-121
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ linters:
2828
- nilnil # Checks that there is no simultaneous return of nil error and an nil value. ref: https://golangci-lint.run/usage/linters/#nilnil
2929
- nonamedreturns # Checks that functions with named return values do not return named values. https://golangci-lint.run/usage/linters/#nonamedreturns
3030
- cyclop # Checks function and package cyclomatic complexity. https://golangci-lint.run/usage/linters/#cyclop
31+
- gocritic # Analyze source code for various issues, including bugs, performance hiccups, and non-idiomatic coding practices. https://golangci-lint.run/docs/linters/configuration/#gocritic
3132

3233
# tests
3334
- testifylint # Checks usage of github.com/stretchr/testify. https://golangci-lint.run/usage/linters/#testifylint

controller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (c *Controller) Run(ctx context.Context) {
353353
consecutiveSoftErrors.Gauge.Set(float64(softErrorCount))
354354
log.Errorf("Failed to do run once: %v (consecutive soft errors: %d)", err, softErrorCount)
355355
} else {
356-
log.Fatalf("Failed to do run once: %v", err)
356+
log.Fatalf("Failed to do run once: %v", err) // nolint: gocritic // exitAfterDefer
357357
}
358358
} else {
359359
if softErrorCount > 0 {

controller/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func Execute() {
104104

105105
endpointsSource, err := buildSource(ctx, cfg)
106106
if err != nil {
107-
log.Fatal(err)
107+
log.Fatal(err) // nolint: gocritic // exitAfterDefer
108108
}
109109

110110
domainFilter := createDomainFilter(cfg)

endpoint/domain_filter.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ func matchFilter(filters []string, domain string, emptyval bool) bool {
122122
continue
123123
}
124124

125-
if strings.HasPrefix(filter, ".") && strings.HasSuffix(strippedDomain, filter) {
125+
switch {
126+
case strings.HasPrefix(filter, ".") && strings.HasSuffix(strippedDomain, filter):
126127
return true
127-
} else if strings.Count(strippedDomain, ".") == strings.Count(filter, ".") {
128-
if strippedDomain == filter {
129-
return true
130-
}
131-
} else if strings.HasSuffix(strippedDomain, "."+filter) {
128+
case strings.Count(strippedDomain, ".") == strings.Count(filter, ".") && strippedDomain == filter:
129+
return true
130+
case strings.HasSuffix(strippedDomain, "."+filter):
132131
return true
133132
}
134133
}

endpoint/endpoint.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,12 @@ func FilterEndpointsByOwnerID(ownerID string, eps []*Endpoint) []*Endpoint {
386386
filtered := []*Endpoint{}
387387
for _, ep := range eps {
388388
endpointOwner, ok := ep.Labels[OwnerLabelKey]
389-
if !ok {
389+
switch {
390+
case !ok:
390391
log.Debugf(`Skipping endpoint %v because of missing owner label (required: "%s")`, ep, ownerID)
391-
} else if endpointOwner != ownerID {
392+
case endpointOwner != ownerID:
392393
log.Debugf(`Skipping endpoint %v because owner id does not match (found: "%s", required: "%s")`, ep, endpointOwner, ownerID)
393-
} else {
394+
default:
394395
filtered = append(filtered, ep)
395396
}
396397
}

internal/gen/docs/flags/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func main() {
5757
if err != nil {
5858
_ = fmt.Errorf("failed to generate markdown file '%s': %v", path, err.Error())
5959
}
60-
content = content + "\n"
60+
content += "\n"
6161
_ = utils.WriteToFile(path, content)
6262
}
6363

internal/gen/docs/flags/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestFlagsMdUpToDate(t *testing.T) {
7373
flags := computeFlags()
7474
actual, err := flags.generateMarkdownTable()
7575
assert.NoError(t, err)
76-
actual = actual + "\n"
76+
actual += "\n"
7777
assert.Len(t, actual, len(expected), "expected file '%s' to be up to date. execute 'make generate-flags-documentation", fileName)
7878
}
7979

internal/gen/docs/metrics/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
_, _ = fmt.Fprintf(os.Stderr, "failed to generate markdown file '%s': %v\n", path, err)
5555
os.Exit(1)
5656
}
57-
content = content + "\n"
57+
content += "\n"
5858
_ = utils.WriteToFile(path, content)
5959
}
6060

@@ -119,6 +119,7 @@ func getRuntimeMetrics(reg prometheus.Registerer) []string {
119119
runtimeMetrics = append(runtimeMetrics, k)
120120
}
121121
}
122+
default:
122123
}
123124
sort.Strings(runtimeMetrics)
124125
return runtimeMetrics

internal/testutils/endpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestExampleSameEndpoints(t *testing.T) {
8484
// example.org 0 IN TXT load-balancer.org []
8585
}
8686

87-
func makeEndpoint(DNSName string) *endpoint.Endpoint {
87+
func makeEndpoint(DNSName string) *endpoint.Endpoint { // nolint: gocritic // captLocal
8888
return &endpoint.Endpoint{
8989
DNSName: DNSName,
9090
Targets: endpoint.Targets{"target.com"},

pkg/events/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func sanitize(input string) string {
177177

178178
// the name should end with an alphanumeric character
179179
if len(sanitized) > 0 && !endsWithAlphaNumeric.MatchString(sanitized) {
180-
sanitized = sanitized + "z"
180+
sanitized += "z"
181181
}
182182

183183
sanitized = invalidChars.ReplaceAllString(sanitized, "-")

0 commit comments

Comments
 (0)