Skip to content

Commit

Permalink
Upgrade golangci-lint to v1.56.2; fix gocritic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Feb 20, 2024
1 parent aefd080 commit 51b6587
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2
version: v1.56.2
args: --verbose
18 changes: 16 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,22 @@ linters-settings:
"-ST1000", # at least one file in a package should have a package comment
]
gocritic:
enabled-checks:
- emptyStringTest
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- deferInLoop
- httpNoBody
- importShadow
- initClause
- paramTypeCombine
- sloppyReassign
- typeUnparen
- unnamedResult
- whyNoLint

issues:
# Show all issues from a linter.
Expand Down
6 changes: 3 additions & 3 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,13 @@ func TestArgsSetGetDel(t *testing.T) {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if string(a.Peek(k)) != "" {
if len(a.Peek(k)) != 0 {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}

a.Parse("aaa=xxx&bb=aa")
if string(a.Peek("foo0")) != "" {
if len(a.Peek("foo0")) != 0 {
t.Fatalf("Unexpected value %q", a.Peek("foo0"))
}
if string(a.Peek("aaa")) != "xxx" {
Expand All @@ -474,7 +474,7 @@ func TestArgsSetGetDel(t *testing.T) {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if string(a.Peek(k)) != "" {
if len(a.Peek(k)) != 0 {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}
Expand Down
12 changes: 6 additions & 6 deletions brotli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func testBrotliCompressSingleCase(s string) error {
func TestCompressHandlerBrotliLevel(t *testing.T) {
t.Parallel()

expectedBody := string(createFixedBody(2e4))
expectedBody := createFixedBody(2e4)
h := CompressHandlerBrotliLevel(func(ctx *RequestCtx) {
ctx.WriteString(expectedBody) //nolint:errcheck
ctx.Write(expectedBody) //nolint:errcheck
}, CompressBrotliDefaultCompression, CompressDefaultCompression)

var ctx RequestCtx
Expand All @@ -121,11 +121,11 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
body := resp.Body()
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}

Expand All @@ -148,7 +148,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}

Expand All @@ -171,7 +171,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
}
2 changes: 1 addition & 1 deletion bytesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func AppendHTMLEscape(dst []byte, s string) []byte {
case '\'':
sub = "'" // "'" is shorter than "'" and apos was not in HTML until HTML5.
}
if len(sub) > 0 {
if sub != "" {
dst = append(dst, s[prev:i]...)
dst = append(dst, sub...)
prev = i + 1
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestClientParseConn(t *testing.T) {
t.Fatalf("req RemoteAddr parse addr fail: %q, hope: %q", res.RemoteAddr().String(), host)
}

if !regexp.MustCompile(`^127\.0\.0\.1:[0-9]{4,5}$`).MatchString(res.LocalAddr().String()) {
if !regexp.MustCompile(`^127\.0\.0\.1:\d{4,5}$`).MatchString(res.LocalAddr().String()) {
t.Fatalf("res LocalAddr addr match fail: %q, hope match: %q", res.LocalAddr().String(), "^127.0.0.1:[0-9]{4,5}$")
}
}
Expand Down Expand Up @@ -2323,7 +2323,7 @@ func (r *singleReadConn) Read(p []byte) (int, error) {
if len(r.s) == r.n {
return 0, io.EOF
}
n := copy(p, []byte(r.s[r.n:]))
n := copy(p, r.s[r.n:])
r.n += n
return n, nil
}
Expand Down
2 changes: 1 addition & 1 deletion fasthttpadaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestHijack(t *testing.T) {
if c, rw, err := f.Hijack(); err != nil {
t.Error(err)
} else {
if _, err := rw.Write([]byte("bar")); err != nil {
if _, err := rw.WriteString("bar"); err != nil {
t.Error(err)
}

Expand Down
6 changes: 3 additions & 3 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func ServeFile(ctx *RequestCtx, path string) {

if path == "" || !filepath.IsAbs(path) {
// extend relative path to absolute path
hasTrailingSlash := len(path) > 0 && (path[len(path)-1] == '/' || path[len(path)-1] == '\\')
hasTrailingSlash := path != "" && (path[len(path)-1] == '/' || path[len(path)-1] == '\\')

var err error
path = filepath.FromSlash(path)
Expand Down Expand Up @@ -442,7 +442,7 @@ func (fs *FS) normalizeRoot(root string) string {
}

// strip trailing slashes from the root path
for len(root) > 0 && root[len(root)-1] == os.PathSeparator {
for root != "" && root[len(root)-1] == os.PathSeparator {
root = root[:len(root)-1]
}
return root
Expand All @@ -468,7 +468,7 @@ func (fs *FS) initRequestHandler() {
}
}

if len(fs.CompressedFileSuffix) > 0 {
if fs.CompressedFileSuffix != "" {
compressedFileSuffixes["gzip"] = fs.CompressedFileSuffix
compressedFileSuffixes["br"] = FSCompressedFileSuffixes["br"]
}
Expand Down
2 changes: 1 addition & 1 deletion fs_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func testFSFSCompress(t *testing.T, h RequestHandler, filePath string) {
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Errorf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
}
body := string(resp.Body())
Expand Down
2 changes: 1 addition & 1 deletion fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Errorf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
}
body := string(resp.Body())
Expand Down
4 changes: 2 additions & 2 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func FuzzResponseReadLimitBody(f *testing.F) {

f.Fuzz(func(t *testing.T, body []byte, max int) {
// Don't do bodies bigger than 10kb.
max = max % (10 * 1024)
max %= (10 * 1024)

var res Response

Expand All @@ -59,7 +59,7 @@ func FuzzRequestReadLimitBody(f *testing.F) {

f.Fuzz(func(t *testing.T, body []byte, max int) {
// Don't do bodies bigger than 10kb.
max = max % (10 * 1024)
max %= (10 * 1024)

var req Request

Expand Down
14 changes: 7 additions & 7 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestResponseHeaderEmptyValueFromHeader(t *testing.T) {
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(h.ContentType()) != string(h1.ContentType()) {
if !bytes.Equal(h.ContentType(), h1.ContentType()) {
t.Fatalf("unexpected content-type: %q. Expecting %q", h.ContentType(), h1.ContentType())
}
v1 := h.Peek("EmptyValue1")
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestRequestHeaderEmptyValueFromHeader(t *testing.T) {
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(h.Host()) != string(h1.Host()) {
if !bytes.Equal(h.Host(), h1.Host()) {
t.Fatalf("unexpected host: %q. Expecting %q", h.Host(), h1.Host())
}
v1 := h.Peek("EmptyValue1")
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestRequestRawHeaders(t *testing.T) {
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(h.Host()) != "" {
if len(h.Host()) != 0 {
t.Fatalf("unexpected host: %q. Expecting %q", h.Host(), "")
}
v1 := h.Peek("NoKey")
Expand Down Expand Up @@ -687,11 +687,11 @@ func TestResponseHeaderDel(t *testing.T) {
t.Fatalf("non-zero value: %q", hv)
}
hv = h.Peek(HeaderContentType)
if string(hv) != string(defaultContentType) {
if !bytes.Equal(hv, defaultContentType) {
t.Fatalf("unexpected content-type: %q. Expecting %q", hv, defaultContentType)
}
hv = h.Peek(HeaderContentEncoding)
if string(hv) != ("gzip") {
if string(hv) != "gzip" {
t.Fatalf("unexpected content-encoding: %q. Expecting %q", hv, "gzip")
}
hv = h.Peek(HeaderServer)
Expand Down Expand Up @@ -1519,7 +1519,7 @@ func TestRequestContentTypeNoDefault(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

if string(h1.contentType) != "" {
if len(h1.contentType) != 0 {
t.Fatalf("unexpected Content-Type %q. Expecting %q", h1.contentType, "")
}
}
Expand Down Expand Up @@ -2121,7 +2121,7 @@ func testRequestHeaderMethod(t *testing.T, expectedMethod string) {
t.Fatalf("unexpected error: %v", err)
}
m1 := h1.Method()
if string(m) != string(m1) {
if !bytes.Equal(m, m1) {
t.Fatalf("unexpected method: %q. Expecting %q", m, m1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ func (req *Request) ContinueReadBody(r *bufio.Reader, maxBodySize int, preParseM
// This way we limit memory usage for large file uploads, since their contents
// is streamed into temporary files if file size exceeds defaultMaxInMemoryFileSize.
req.multipartFormBoundary = string(req.Header.MultipartFormBoundary())
if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
if req.multipartFormBoundary != "" && len(req.Header.peek(strContentEncoding)) == 0 {
req.multipartForm, err = readMultipartForm(r, req.multipartFormBoundary, contentLength, defaultMaxInMemoryFileSize)
if err != nil {
req.Reset()
Expand Down Expand Up @@ -1329,7 +1329,7 @@ func (req *Request) ContinueReadBodyStream(r *bufio.Reader, maxBodySize int, pre
// This way we limit memory usage for large file uploads, since their contents
// is streamed into temporary files if file size exceeds defaultMaxInMemoryFileSize.
req.multipartFormBoundary = b2s(req.Header.MultipartFormBoundary())
if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
if req.multipartFormBoundary != "" && len(req.Header.peek(strContentEncoding)) == 0 {
req.multipartForm, err = readMultipartForm(r, req.multipartFormBoundary, contentLength, defaultMaxInMemoryFileSize)
if err != nil {
req.Reset()
Expand Down
4 changes: 2 additions & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ func TestRequestReadGzippedBody(t *testing.T) {
if r.Header.ContentLength() != len(body) {
t.Fatalf("unexpected content-length: %d. Expecting %d", r.Header.ContentLength(), len(body))
}
if string(r.Body()) != string(body) {
if !bytes.Equal(r.Body(), body) {
t.Fatalf("unexpected body: %q. Expecting %q", r.Body(), body)
}

Expand Down Expand Up @@ -1323,7 +1323,7 @@ func TestRequestContinueReadBodyDisablePrereadMultipartForm(t *testing.T) {
t.Fatalf("The multipartForm of the Request must be nil")
}

if string(formData) != string(r.Body()) {
if !bytes.Equal(formData, r.Body()) {
t.Fatalf("The body given must equal the body in the Request")
}
}
Expand Down
3 changes: 2 additions & 1 deletion prefork/prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (p *Prefork) doCommand() (*exec.Cmd, error) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = p.files
return cmd, cmd.Start()
err := cmd.Start()
return cmd, err
}

func (p *Prefork) prefork(addr string) (err error) {
Expand Down
6 changes: 3 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ func TestCompressHandler(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
body := resp.Body()
Expand Down Expand Up @@ -2110,11 +2110,11 @@ func TestCompressHandlerVary(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
vary := resp.Header.Peek("Vary")
if string(vary) != "" {
if len(vary) != 0 {
t.Fatalf("unexpected Vary: %q. Expecting %q", vary, "")
}
body := resp.Body()
Expand Down

0 comments on commit 51b6587

Please sign in to comment.