Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade golangci-lint to v1.56.2; fix gocritic issues #1722

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

alexandear
Copy link
Contributor

@alexandear alexandear commented Feb 20, 2024

The PR upgrades golangci-lint version to the latest v1.56.2), enables a lot of gocritic's checks and fixes up issues.

Explanation of some gocritic's fixes

  • in idiomatic Go, we should use len(v) =! 0 instead of string(v) != "" when v is []byte. See https://dmitri.shuralyov.com/idiomatic-go#empty-string-check;
  • we should use v != "" instead of len(v) != when v is string;
  • we can compare byte slices with bytes.Equal instead of converting them to string;
  • rw.Write([]byte(str),... can be rewritten to rw.WriteString(str,....

The full log of running golangci-lint

❯ golangci-lint run
prefork/prefork.go:155:14: evalOrder: may want to evaluate cmd.Start() before the return statement (gocritic)
        return cmd, cmd.Start()
                    ^
bytesconv.go:38:6: emptyStringTest: replace `len(sub) > 0` with `sub != ""` (gocritic)
                if len(sub) > 0 {
                   ^
fs.go:471:5: emptyStringTest: replace `len(fs.CompressedFileSuffix) > 0` with `fs.CompressedFileSuffix != ""` (gocritic)
        if len(fs.CompressedFileSuffix) > 0 {
           ^
fs.go:105:23: emptyStringTest: replace `len(path) > 0` with `path != ""` (gocritic)
                hasTrailingSlash := len(path) > 0 && (path[len(path)-1] == '/' || path[len(path)-1] == '\\')
                                    ^
fs.go:445:6: emptyStringTest: replace `len(root) > 0` with `root != ""` (gocritic)
        for len(root) > 0 && root[len(root)-1] == os.PathSeparator {
            ^
http.go:1253:7: emptyStringTest: replace `len(req.multipartFormBoundary) > 0` with `req.multipartFormBoundary != ""` (gocritic)
                        if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
                           ^
http.go:1332:7: emptyStringTest: replace `len(req.multipartFormBoundary) > 0` with `req.multipartFormBoundary != ""` (gocritic)
                        if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
                           ^
args_test.go:452:5: stringXbytes: suggestion: len(a.Peek("foo0")) != 0 (gocritic)
        if string(a.Peek("foo0")) != "" {
           ^
args_test.go:446:6: stringXbytes: suggestion: len(a.Peek(k)) != 0 (gocritic)
                if string(a.Peek(k)) != "" {
                   ^
args_test.go:477:6: stringXbytes: suggestion: len(a.Peek(k)) != 0 (gocritic)
                if string(a.Peek(k)) != "" {
                   ^
brotli_test.go:124:5: stringXbytes: suggestion: len(ce) != 0 (gocritic)
        if string(ce) != "" {
           ^
client_test.go:459:25: regexpSimplify: can re-write `^127\.0\.0\.1:[0-9]{4,5}$` as `^127\.0\.0\.1:\d{4,5}$` (gocritic)
        if !regexp.MustCompile(`^127\.0\.0\.1:[0-9]{4,5}$`).MatchString(res.LocalAddr().String()) {
                               ^
client_test.go:2326:7: stringXbytes: can simplify `[]byte(r.s[r.n:])` to `r.s[r.n:]` (gocritic)
        n := copy(p, []byte(r.s[r.n:]))
             ^
fs_fs_test.go:257:5: stringXbytes: suggestion: len(ce) != 0 (gocritic)
        if string(ce) != "" {
           ^
fs_test.go:655:5: stringXbytes: suggestion: len(ce) != 0 (gocritic)
        if string(ce) != "" {
           ^
fuzz_test.go:47:3: assignOp: replace `max = max % (10 * 1024)` with `max %= (10 * 1024)` (gocritic)
                max = max % (10 * 1024)
                ^
fuzz_test.go:62:3: assignOp: replace `max = max % (10 * 1024)` with `max %= (10 * 1024)` (gocritic)
                max = max % (10 * 1024)
                ^
header_test.go:170:5: stringXbytes: suggestion: !bytes.Equal(h.ContentType(), h1.ContentType()) (gocritic)
        if string(h.ContentType()) != string(h1.ContentType()) {
           ^
header_test.go:225:5: stringXbytes: suggestion: !bytes.Equal(h.Host(), h1.Host()) (gocritic)
        if string(h.Host()) != string(h1.Host()) {
           ^
header_test.go:690:5: stringXbytes: suggestion: !bytes.Equal(hv, defaultContentType) (gocritic)
        if string(hv) != string(defaultContentType) {
           ^
header_test.go:2124:5: stringXbytes: suggestion: !bytes.Equal(m, m1) (gocritic)
        if string(m) != string(m1) {
           ^
header_test.go:344:6: stringXbytes: suggestion: len(h.Host()) != 0 (gocritic)
                if string(h.Host()) != "" {
                   ^
header_test.go:1522:5: stringXbytes: suggestion: len(h1.contentType) != 0 (gocritic)
        if string(h1.contentType) != "" {
           ^
http_test.go:1326:5: stringXbytes: suggestion: !bytes.Equal(formData, r.Body()) (gocritic)
        if string(formData) != string(r.Body()) {
           ^
http_test.go:1213:5: stringXbytes: suggestion: !bytes.Equal(r.Body(), body) (gocritic)
        if string(r.Body()) != string(body) {
           ^
server_test.go:2015:5: stringXbytes: suggestion: len(ce) != 0 (gocritic)
        if string(ce) != "" {
           ^
server_test.go:2113:5: stringXbytes: suggestion: len(ce) != 0 (gocritic)
        if string(ce) != "" {
           ^
server_test.go:2117:5: stringXbytes: suggestion: len(vary) != 0 (gocritic)
        if string(vary) != "" {
           ^
fasthttpadaptor/adaptor_test.go:163:18: preferStringWriter: rw.WriteString("bar") should be preferred to the rw.Write([]byte("bar")) (gocritic)
                                if _, err := rw.Write([]byte("bar")); err != nil {
                                             ^

@alexandear alexandear force-pushed the upgrade-golangci-lint branch 2 times, most recently from 51b6587 to d4d7dce Compare February 20, 2024 16:06
@erikdubbelboer erikdubbelboer merged commit 190204c into valyala:master Feb 21, 2024
16 of 18 checks passed
@erikdubbelboer
Copy link
Collaborator

Thanks!

@alexandear alexandear deleted the upgrade-golangci-lint branch February 21, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants