Skip to content

Commit 9aa666e

Browse files
authoredAug 29, 2023
Enable gocritic linter; fix lint issues (#1612)
1 parent 0e99e64 commit 9aa666e

11 files changed

+74
-63
lines changed
 

‎.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
uses: golangci/golangci-lint-action@v3
1818
with:
1919
version: v1.54.2
20-
args: --enable=nolintlint,gochecknoinits,bodyclose,gofumpt --verbose
20+
args: --enable=nolintlint,gochecknoinits,bodyclose,gofumpt,gocritic --verbose

‎args.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,14 @@ func decodeArgAppend(dst, src []byte) []byte {
552552
}
553553

554554
idx := 0
555-
if idxPercent == -1 {
555+
switch {
556+
case idxPercent == -1:
556557
idx = idxPlus
557-
} else if idxPlus == -1 {
558+
case idxPlus == -1:
558559
idx = idxPercent
559-
} else if idxPercent > idxPlus {
560+
case idxPercent > idxPlus:
560561
idx = idxPlus
561-
} else {
562+
default:
562563
idx = idxPercent
563564
}
564565

@@ -567,7 +568,8 @@ func decodeArgAppend(dst, src []byte) []byte {
567568
// slow path
568569
for i := idx; i < len(src); i++ {
569570
c := src[i]
570-
if c == '%' {
571+
switch c {
572+
case '%':
571573
if i+2 >= len(src) {
572574
return append(dst, src[i:]...)
573575
}
@@ -579,9 +581,9 @@ func decodeArgAppend(dst, src []byte) []byte {
579581
dst = append(dst, x1<<4|x2)
580582
i += 2
581583
}
582-
} else if c == '+' {
584+
case '+':
583585
dst = append(dst, ' ')
584-
} else {
586+
default:
585587
dst = append(dst, c)
586588
}
587589
}

‎bytesconv_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ func testParseIPv4(t *testing.T, ipStr string, isValid bool) {
8282
if s != ipStr {
8383
t.Fatalf("unexpected ip parsed %q. Expecting %q", s, ipStr)
8484
}
85-
} else {
86-
if err == nil {
87-
t.Fatalf("expecting error when parsing ip %q", ipStr)
88-
}
85+
} else if err == nil {
86+
t.Fatalf("expecting error when parsing ip %q", ipStr)
8987
}
9088
}
9189

‎client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
13431343
userAgent = defaultUserAgent
13441344
}
13451345
if userAgent != "" {
1346-
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
1346+
req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
13471347
}
13481348
}
13491349

@@ -2303,7 +2303,7 @@ func (c *pipelineConnClient) DoDeadline(req *Request, resp *Response, deadline t
23032303
userAgent = defaultUserAgent
23042304
}
23052305
if userAgent != "" {
2306-
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
2306+
req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
23072307
}
23082308
}
23092309

@@ -2410,7 +2410,7 @@ func (c *pipelineConnClient) Do(req *Request, resp *Response) error {
24102410
userAgent = defaultUserAgent
24112411
}
24122412
if userAgent != "" {
2413-
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
2413+
req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
24142414
}
24152415
}
24162416

‎client_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,12 @@ func TestClientHeaderCase(t *testing.T) {
669669

670670
code, body, err := c.Get(nil, "http://example.com")
671671
if err != nil {
672-
t.Error(err)
673-
} else if code != 200 {
672+
t.Fatal(err)
673+
}
674+
if code != 200 {
674675
t.Errorf("expected status code 200 got %d", code)
675-
} else if string(body) != "This is the data in the first chunk and this is the second one " {
676+
}
677+
if string(body) != "This is the data in the first chunk and this is the second one " {
676678
t.Errorf("wrong body: %q", body)
677679
}
678680
}

‎header.go

+22-17
Original file line numberDiff line numberDiff line change
@@ -1298,19 +1298,20 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
12981298

12991299
switch key[0] | 0x20 {
13001300
case 'c':
1301-
if caseInsensitiveCompare(strContentType, key) {
1301+
switch {
1302+
case caseInsensitiveCompare(strContentType, key):
13021303
h.SetContentTypeBytes(value)
13031304
return true
1304-
} else if caseInsensitiveCompare(strContentLength, key) {
1305+
case caseInsensitiveCompare(strContentLength, key):
13051306
if contentLength, err := parseContentLength(value); err == nil {
13061307
h.contentLength = contentLength
13071308
h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
13081309
}
13091310
return true
1310-
} else if caseInsensitiveCompare(strContentEncoding, key) {
1311+
case caseInsensitiveCompare(strContentEncoding, key):
13111312
h.SetContentEncodingBytes(value)
13121313
return true
1313-
} else if caseInsensitiveCompare(strConnection, key) {
1314+
case caseInsensitiveCompare(strConnection, key):
13141315
if bytes.Equal(strClose, value) {
13151316
h.SetConnectionClose()
13161317
} else {
@@ -1361,24 +1362,25 @@ func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
13611362

13621363
switch key[0] | 0x20 {
13631364
case 'c':
1364-
if caseInsensitiveCompare(strContentType, key) {
1365+
switch {
1366+
case caseInsensitiveCompare(strContentType, key):
13651367
h.SetContentTypeBytes(value)
13661368
return true
1367-
} else if caseInsensitiveCompare(strContentLength, key) {
1369+
case caseInsensitiveCompare(strContentLength, key):
13681370
if contentLength, err := parseContentLength(value); err == nil {
13691371
h.contentLength = contentLength
13701372
h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
13711373
}
13721374
return true
1373-
} else if caseInsensitiveCompare(strConnection, key) {
1375+
case caseInsensitiveCompare(strConnection, key):
13741376
if bytes.Equal(strClose, value) {
13751377
h.SetConnectionClose()
13761378
} else {
13771379
h.ResetConnectionClose()
13781380
h.setNonSpecial(key, value)
13791381
}
13801382
return true
1381-
} else if caseInsensitiveCompare(strCookie, key) {
1383+
case caseInsensitiveCompare(strCookie, key):
13821384
h.collectCookies()
13831385
h.cookies = parseRequestCookies(h.cookies, value)
13841386
return true
@@ -2791,16 +2793,17 @@ func (h *RequestHeader) parseFirstLine(buf []byte) (int, error) {
27912793
protoStr := strHTTP11
27922794
// parse requestURI
27932795
n = bytes.LastIndexByte(b, ' ')
2794-
if n < 0 {
2796+
switch {
2797+
case n < 0:
27952798
h.noHTTP11 = true
27962799
n = len(b)
27972800
protoStr = strHTTP10
2798-
} else if n == 0 {
2801+
case n == 0:
27992802
if h.secureErrorLogMessage {
28002803
return 0, fmt.Errorf("requestURI cannot be empty")
28012804
}
28022805
return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
2803-
} else if !bytes.Equal(b[n+1:], strHTTP11) {
2806+
case !bytes.Equal(b[n+1:], strHTTP11):
28042807
h.noHTTP11 = true
28052808
protoStr = b[n+1:]
28062809
}
@@ -3268,15 +3271,16 @@ func normalizeHeaderValue(ov, ob []byte, headerLength int) (nv, nb []byte, nhl i
32683271
lineStart := false
32693272
for read := 0; read < length; read++ {
32703273
c := ov[read]
3271-
if c == rChar || c == nChar {
3274+
switch {
3275+
case c == rChar || c == nChar:
32723276
shrunk++
32733277
if c == nChar {
32743278
lineStart = true
32753279
}
32763280
continue
3277-
} else if lineStart && c == '\t' {
3281+
case lineStart && c == '\t':
32783282
c = ' '
3279-
} else {
3283+
default:
32803284
lineStart = false
32813285
}
32823286
nv[write] = c
@@ -3335,15 +3339,16 @@ func removeNewLines(raw []byte) []byte {
33353339
foundN := bytes.IndexByte(raw, nChar)
33363340
start := 0
33373341

3338-
if foundN != -1 {
3342+
switch {
3343+
case foundN != -1:
33393344
if foundR > foundN {
33403345
start = foundN
33413346
} else if foundR != -1 {
33423347
start = foundR
33433348
}
3344-
} else if foundR != -1 {
3349+
case foundR != -1:
33453350
start = foundR
3346-
} else {
3351+
default:
33473352
return raw
33483353
}
33493354

‎header_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -1977,8 +1977,7 @@ func TestResponseHeaderCookieIssue4(t *testing.T) {
19771977
}
19781978
cookieSeen := false
19791979
h.VisitAll(func(key, _ []byte) {
1980-
switch string(key) {
1981-
case HeaderSetCookie:
1980+
if string(key) == HeaderSetCookie {
19821981
cookieSeen = true
19831982
}
19841983
})
@@ -1998,8 +1997,7 @@ func TestResponseHeaderCookieIssue4(t *testing.T) {
19981997
}
19991998
cookieSeen = false
20001999
h.VisitAll(func(key, _ []byte) {
2001-
switch string(key) {
2002-
case HeaderSetCookie:
2000+
if string(key) == HeaderSetCookie {
20032001
cookieSeen = true
20042002
}
20052003
})
@@ -2022,8 +2020,7 @@ func TestRequestHeaderCookieIssue313(t *testing.T) {
20222020
}
20232021
cookieSeen := false
20242022
h.VisitAll(func(key, _ []byte) {
2025-
switch string(key) {
2026-
case HeaderCookie:
2023+
if string(key) == HeaderCookie {
20272024
cookieSeen = true
20282025
}
20292026
})
@@ -2040,8 +2037,7 @@ func TestRequestHeaderCookieIssue313(t *testing.T) {
20402037
}
20412038
cookieSeen = false
20422039
h.VisitAll(func(key, _ []byte) {
2043-
switch string(key) {
2044-
case HeaderCookie:
2040+
if string(key) == HeaderCookie {
20452041
cookieSeen = true
20462042
}
20472043
})

‎http.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,15 @@ func (req *Request) ResetBody() {
814814
// CopyTo copies req contents to dst except of body stream.
815815
func (req *Request) CopyTo(dst *Request) {
816816
req.copyToSkipBody(dst)
817-
if req.bodyRaw != nil {
817+
switch {
818+
case req.bodyRaw != nil:
818819
dst.bodyRaw = append(dst.bodyRaw[:0], req.bodyRaw...)
819820
if dst.body != nil {
820821
dst.body.Reset()
821822
}
822-
} else if req.body != nil {
823+
case req.body != nil:
823824
dst.bodyBuffer().Set(req.body.B)
824-
} else if dst.body != nil {
825+
case dst.body != nil:
825826
dst.body.Reset()
826827
}
827828
}
@@ -846,14 +847,15 @@ func (req *Request) copyToSkipBody(dst *Request) {
846847
// CopyTo copies resp contents to dst except of body stream.
847848
func (resp *Response) CopyTo(dst *Response) {
848849
resp.copyToSkipBody(dst)
849-
if resp.bodyRaw != nil {
850+
switch {
851+
case resp.bodyRaw != nil:
850852
dst.bodyRaw = append(dst.bodyRaw, resp.bodyRaw...)
851853
if dst.body != nil {
852854
dst.body.Reset()
853855
}
854-
} else if resp.body != nil {
856+
case resp.body != nil:
855857
dst.bodyBuffer().Set(resp.body.B)
856-
} else if dst.body != nil {
858+
case dst.body != nil:
857859
dst.body.Reset()
858860
}
859861
}
@@ -1284,14 +1286,15 @@ func (req *Request) ReadBody(r *bufio.Reader, contentLength int, maxBodySize int
12841286
bodyBuf := req.bodyBuffer()
12851287
bodyBuf.Reset()
12861288

1287-
if contentLength >= 0 {
1289+
switch {
1290+
case contentLength >= 0:
12881291
bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
1289-
} else if contentLength == -1 {
1292+
case contentLength == -1:
12901293
bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)
12911294
if err == nil && len(bodyBuf.B) == 0 {
12921295
req.Header.SetContentLength(0)
12931296
}
1294-
} else {
1297+
default:
12951298
bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)
12961299
req.Header.SetContentLength(len(bodyBuf.B))
12971300
}
@@ -1427,19 +1430,20 @@ func (resp *Response) ReadBody(r *bufio.Reader, maxBodySize int) (err error) {
14271430
bodyBuf.Reset()
14281431

14291432
contentLength := resp.Header.ContentLength()
1430-
if contentLength >= 0 {
1433+
switch {
1434+
case contentLength >= 0:
14311435
bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
14321436
if err == ErrBodyTooLarge && resp.StreamBody {
14331437
resp.bodyStream = acquireRequestStream(bodyBuf, r, &resp.Header)
14341438
err = nil
14351439
}
1436-
} else if contentLength == -1 {
1440+
case contentLength == -1:
14371441
if resp.StreamBody {
14381442
resp.bodyStream = acquireRequestStream(bodyBuf, r, &resp.Header)
14391443
} else {
14401444
bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)
14411445
}
1442-
} else {
1446+
default:
14431447
bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)
14441448
resp.Header.SetContentLength(len(bodyBuf.B))
14451449
}

‎server.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,12 @@ func CompressHandlerLevel(h RequestHandler, level int) RequestHandler {
549549
func CompressHandlerBrotliLevel(h RequestHandler, brotliLevel, otherLevel int) RequestHandler {
550550
return func(ctx *RequestCtx) {
551551
h(ctx)
552-
if ctx.Request.Header.HasAcceptEncodingBytes(strBr) {
552+
switch {
553+
case ctx.Request.Header.HasAcceptEncodingBytes(strBr):
553554
ctx.Response.brotliBody(brotliLevel) //nolint:errcheck
554-
} else if ctx.Request.Header.HasAcceptEncodingBytes(strGzip) {
555+
case ctx.Request.Header.HasAcceptEncodingBytes(strGzip):
555556
ctx.Response.gzipBody(otherLevel) //nolint:errcheck
556-
} else if ctx.Request.Header.HasAcceptEncodingBytes(strDeflate) {
557+
case ctx.Request.Header.HasAcceptEncodingBytes(strDeflate):
557558
ctx.Response.deflateBody(otherLevel) //nolint:errcheck
558559
}
559560
}
@@ -2238,11 +2239,12 @@ func (s *Server) serveConn(c net.Conn) (err error) {
22382239
panic(fmt.Sprintf("BUG: error in SetReadDeadline(%v): %v", deadline, err))
22392240
}
22402241
}
2241-
if reqConf.MaxRequestBodySize > 0 {
2242+
switch {
2243+
case reqConf.MaxRequestBodySize > 0:
22422244
maxRequestBodySize = reqConf.MaxRequestBodySize
2243-
} else if s.MaxRequestBodySize > 0 {
2245+
case s.MaxRequestBodySize > 0:
22442246
maxRequestBodySize = s.MaxRequestBodySize
2245-
} else {
2247+
default:
22462248
maxRequestBodySize = DefaultMaxRequestBodySize
22472249
}
22482250
if reqConf.WriteTimeout > 0 {

‎uri.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ func normalizePath(dst, src []byte) []byte {
688688
func (u *URI) RequestURI() []byte {
689689
var dst []byte
690690
if u.DisablePathNormalizing {
691-
dst = append(u.requestURI[:0], u.PathOriginal()...)
691+
dst = u.requestURI[:0]
692+
dst = append(dst, u.PathOriginal()...)
692693
} else {
693694
dst = appendQuotedPath(u.requestURI[:0], u.Path())
694695
}

‎userdata.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func (d *userData) Set(key interface{}, value interface{}) {
4242
kv := userDataKV{}
4343
kv.key = key
4444
kv.value = value
45-
*d = append(args, kv)
45+
args = append(args, kv)
46+
*d = args
4647
}
4748

4849
func (d *userData) SetBytes(key []byte, value interface{}) {

0 commit comments

Comments
 (0)
Please sign in to comment.