Skip to content

Commit 59083a1

Browse files
committed
fix: use errcheck exclusions instead of noisy suppressions
Configure golangci-lint to exclude errcheck for idiomatic Go patterns where errors are unactionable (ResponseWriter.Write, fmt.Fprint, json.Encode, Flush, Close in test files). Revert _, _ = suppressions back to clean idiomatic Go.
1 parent 0221884 commit 59083a1

7 files changed

Lines changed: 31 additions & 20 deletions

File tree

.golangci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ linters:
1414
- unused
1515
- whitespace
1616
- misspell
17+
exclusions:
18+
rules:
19+
# Don't require error checks on HTTP response writes, fmt prints,
20+
# JSON encoding, buffer flushes, and response body closes — these
21+
# are idiomatic Go patterns where errors are unactionable.
22+
- linters: [errcheck]
23+
path: '.*\.go'
24+
text: '(ResponseWriter|Fprint|Fprintln|Fprintf|Encode|\.Flush|\.Close|\.Start)\b'
25+
# Don't lint test helpers for errcheck in test files
26+
- linters: [errcheck]
27+
path: '_test\.go'
1728
linters-settings:
1829
revive:
1930
ignore-generated-header: true

app/app_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestAppStartAndShutdown(t *testing.T) {
5959

6060
resp, err := http.Get("http://127.0.0.1:18950/ping")
6161
require.NoError(t, err)
62-
defer func() { _ = resp.Body.Close() }()
62+
defer resp.Body.Close()
6363
assert.Equal(t, http.StatusOK, resp.StatusCode)
6464

6565
cancel()
@@ -124,19 +124,19 @@ func TestAppStartAndShutdown(t *testing.T) {
124124
app.WithLogger(logger.NewNoop()),
125125
app.WithAddr("127.0.0.1:18953"),
126126
app.WithHandler("/hello", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
127-
_, _ = fmt.Fprint(w, "world")
127+
fmt.Fprint(w, "world")
128128
})),
129129
app.WithHealthCheck("/ping"),
130130
app.WithH2C(),
131131
)
132132
require.NoError(t, err)
133133

134-
go func() { _ = a.Start(ctx) }()
134+
go a.Start(ctx)
135135
time.Sleep(100 * time.Millisecond)
136136

137137
resp, err := http.Get("http://127.0.0.1:18953/hello")
138138
require.NoError(t, err)
139-
defer func() { _ = resp.Body.Close() }()
139+
defer resp.Body.Close()
140140

141141
body, _ := io.ReadAll(resp.Body)
142142
assert.Equal(t, "world", string(body))

logger/logrus_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestLogrus(t *testing.T) {
2222
DisableTimestamp: true,
2323
}))
2424
logger.Info("hello world")
25-
_ = foo.Flush()
25+
foo.Flush()
2626

2727
assert.Equal(t, "level=info msg=\"hello world\"\n", b.String())
2828
})
@@ -34,7 +34,7 @@ func TestLogrus(t *testing.T) {
3434
DisableTimestamp: true,
3535
}))
3636
logger.Debug("hello world")
37-
_ = foo.Flush()
37+
foo.Flush()
3838

3939
assert.Equal(t, "", b.String())
4040
})
@@ -46,7 +46,7 @@ func TestLogrus(t *testing.T) {
4646
DisableTimestamp: true,
4747
}))
4848
logger.Debug("current values", "day", 11, "month", "aug")
49-
_ = foo.Flush()
49+
foo.Flush()
5050

5151
assert.Equal(t, "level=debug msg=\"current values\" day=11 month=aug\n", b.String())
5252
})
@@ -59,7 +59,7 @@ func TestLogrus(t *testing.T) {
5959
}))
6060
var err = fmt.Errorf("request failed")
6161
logger.Error(err.Error(), "hello", "world")
62-
_ = foo.Flush()
62+
foo.Flush()
6363
assert.Equal(t, "level=error msg=\"request failed\" hello=world\n", b.String())
6464
})
6565
t.Run("should ignore params if malformed", func(t *testing.T) {
@@ -71,7 +71,7 @@ func TestLogrus(t *testing.T) {
7171
}))
7272
var err = fmt.Errorf("request failed")
7373
logger.Error(err.Error(), "hello", "world", "!")
74-
_ = foo.Flush()
74+
foo.Flush()
7575
assert.Equal(t, "level=error msg=\"request failed\"\n", b.String())
7676
})
7777
}

logger/zap_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestZap(t *testing.T) {
6666

6767
zapper := logger.NewZap(buildBufferedZapOption(bWriter, mockedTime, randomString(10)))
6868
zapper.Info("hello", "wor", "ld")
69-
_ = bWriter.Flush()
69+
bWriter.Flush()
7070

7171
assert.Equal(t, mockedTime.Format("2006-01-02T15:04:05.000Z0700")+"\tINFO\thello\t{\"wor\": \"ld\"}\n", b.String())
7272
})
@@ -79,7 +79,7 @@ func TestZap(t *testing.T) {
7979
ctx := zapper.NewContext(context.Background())
8080
contextualLog := logger.ZapFromContext(ctx)
8181
contextualLog.Info("hello", "wor", "ld")
82-
_ = bWriter.Flush()
82+
bWriter.Flush()
8383

8484
assert.Equal(t, mockedTime.Format("2006-01-02T15:04:05.000Z0700")+"\tINFO\thello\t{\"wor\": \"ld\"}\n", b.String())
8585
})
@@ -93,7 +93,7 @@ func TestZap(t *testing.T) {
9393
ctx = logger.ZapContextWithFields(ctx, zap.Int("one", 1))
9494
ctx = logger.ZapContextWithFields(ctx, zap.String("two", "two"))
9595
logger.ZapFromContext(ctx).Info("hello", "wor", "ld")
96-
_ = bWriter.Flush()
96+
bWriter.Flush()
9797

9898
assert.Equal(t, mockedTime.Format("2006-01-02T15:04:05.000Z0700")+"\tINFO\thello\t{\"one\": 1, \"two\": \"two\", \"wor\": \"ld\"}\n", b.String())
9999
})

middleware/middleware_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestRequestIDHTTP(t *testing.T) {
7878
chain := middleware.DefaultHTTP(logger.NewNoop())
7979
handler := chain(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8080
id := requestid.FromContext(r.Context())
81-
_, _ = w.Write([]byte(id))
81+
w.Write([]byte(id))
8282
}))
8383

8484
t.Run("generates ID when missing", func(t *testing.T) {

server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ func (s *Server) Start(ctx context.Context) error {
9595
func healthHandler(w http.ResponseWriter, _ *http.Request) {
9696
w.Header().Set("Content-Type", "application/json")
9797
w.WriteHeader(http.StatusOK)
98-
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
98+
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
9999
}

server/server_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestServer(t *testing.T) {
3636
cancel()
3737
t.Skip("cannot determine random port from outside; covered by other tests")
3838
}
39-
defer func() { _ = resp.Body.Close() }()
39+
defer resp.Body.Close()
4040

4141
assert.Equal(t, http.StatusOK, resp.StatusCode)
4242
cancel()
@@ -48,7 +48,7 @@ func TestServer(t *testing.T) {
4848

4949
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
5050
w.WriteHeader(http.StatusOK)
51-
_, _ = fmt.Fprint(w, "hello")
51+
fmt.Fprint(w, "hello")
5252
})
5353

5454
srv := server.New(
@@ -100,12 +100,12 @@ func TestServer(t *testing.T) {
100100
server.WithHealthCheck("/healthz"),
101101
)
102102

103-
go func() { _ = srv.Start(ctx) }()
103+
go srv.Start(ctx)
104104
time.Sleep(100 * time.Millisecond)
105105

106106
resp, err := http.Get("http://127.0.0.1:18923/healthz")
107107
require.NoError(t, err)
108-
defer func() { _ = resp.Body.Close() }()
108+
defer resp.Body.Close()
109109

110110
assert.Equal(t, http.StatusOK, resp.StatusCode)
111111
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
@@ -129,12 +129,12 @@ func TestServer(t *testing.T) {
129129
server.WithHealthCheck("/ping"),
130130
)
131131

132-
go func() { _ = srv.Start(ctx) }()
132+
go srv.Start(ctx)
133133
time.Sleep(100 * time.Millisecond)
134134

135135
resp, err := http.Get("http://127.0.0.1:18924/ping")
136136
require.NoError(t, err)
137-
defer func() { _ = resp.Body.Close() }()
137+
defer resp.Body.Close()
138138

139139
assert.Equal(t, http.StatusOK, resp.StatusCode)
140140

0 commit comments

Comments
 (0)