From 82070cb4c8ab5585fbbbc185131fce29d6e0014f Mon Sep 17 00:00:00 2001 From: nickajacks1 <128185314+nickajacks1@users.noreply.github.com> Date: Mon, 18 Mar 2024 06:50:40 -0700 Subject: [PATCH] chore: Update golangci-lint to enable more lint rules (#2923) * chore(lint): enable ifElseChange and clean up config a bit * chore(lint): enable gocritic diagnostic checks --- .golangci.yml | 14 ++++---------- app.go | 3 +-- app_test.go | 2 -- bind_test.go | 1 - client/hooks_test.go | 7 ++++--- ctx_test.go | 9 +-------- helpers.go | 10 +++++----- middleware/keyauth/keyauth_test.go | 9 +++++---- middleware/logger/default_logger.go | 7 ++++--- middleware/session/store.go | 12 +++++++----- 10 files changed, 31 insertions(+), 43 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 965b5cab..b09d48e7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -72,17 +72,12 @@ linters-settings: gocritic: # TODO: Uncomment the following lines - # enabled-tags: - # - diagnostic + enabled-tags: + - diagnostic # - style # - performance # - experimental # - opinionated - disabled-checks: - - ifElseChain # TODO: Do not disable - # - hugeParam - # - rangeExprCopy - # - rangeValCopy settings: captLocal: paramsOnly: false @@ -195,6 +190,8 @@ linters-settings: disabled: true - name: unchecked-type-assertion disabled: true # TODO: Do not disable + - name: unhandled-error + arguments: ['bytes\.Buffer\.Write'] stylecheck: checks: @@ -217,9 +214,6 @@ linters-settings: testifylint: enable-all: true - # TODO: Do not disable any options - disable: - - go-require testpackage: skip-regexp: "^$" diff --git a/app.go b/app.go index bfae60a8..eaeb4da7 100644 --- a/app.go +++ b/app.go @@ -988,8 +988,7 @@ func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Respons type disableLogger struct{} -func (*disableLogger) Printf(_ string, _ ...any) { - // fmt.Println(fmt.Sprintf(format, args...)) +func (*disableLogger) Printf(string, ...any) { } func (app *App) init() *App { diff --git a/app_test.go b/app_test.go index 68f0413b..63aaa946 100644 --- a/app_test.go +++ b/app_test.go @@ -1316,13 +1316,11 @@ func Test_App_Group(t *testing.T) { resp, err := app.Test(httptest.NewRequest(MethodPost, "/test/v1/", nil)) require.NoError(t, err, "app.Test(req)") require.Equal(t, 200, resp.StatusCode, "Status code") - // require.Equal(t, "/test/v1", resp.Header.Get("Location"), "Location") api.Get("/users", dummyHandler) resp, err = app.Test(httptest.NewRequest(MethodGet, "/test/v1/UsErS", nil)) require.NoError(t, err, "app.Test(req)") require.Equal(t, 200, resp.StatusCode, "Status code") - // require.Equal(t, "/test/v1/users", resp.Header.Get("Location"), "Location") } func Test_App_Route(t *testing.T) { diff --git a/bind_test.go b/bind_test.go index 436c2e4b..0c5b848f 100644 --- a/bind_test.go +++ b/bind_test.go @@ -714,7 +714,6 @@ func Benchmark_Bind_Query_Comma(b *testing.B) { } c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") - // c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball&hobby=football") c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football") q := new(Query) b.ReportAllocs() diff --git a/client/hooks_test.go b/client/hooks_test.go index 406ee6b8..dfea361d 100644 --- a/client/hooks_test.go +++ b/client/hooks_test.go @@ -183,11 +183,12 @@ func Test_Parser_Request_URL(t *testing.T) { flag1, flag2, flag3 := false, false, false for _, v := range values["bar"] { - if v == "foo1" { + switch v { + case "foo1": flag1 = true - } else if v == "foo2" { + case "foo2": flag2 = true - } else if v == "foo" { + case "foo": flag3 = true } } diff --git a/ctx_test.go b/ctx_test.go index 339abfeb..d52ceda5 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1435,6 +1435,7 @@ func Test_Ctx_Parsers(t *testing.T) { }) t.Run("ParamsParser", func(t *testing.T) { t.Skip("ParamsParser is not ready for v3") + //nolint:gocritic // TODO: uncomment // t.Parallel() // withValues(t, func(c Ctx, testStruct *TestStruct) error { // c.route = &Route{Params: []string{"name", "name2", "class", "class2"}} @@ -4758,8 +4759,6 @@ func TestCtx_ParamsInt(t *testing.T) { // For the user id I will use the number 1111, so I should be able to get the number // 1111 from the Ctx app.Get("/test/:user", func(c Ctx) error { - // require.Equal(t, "john", c.Params("user")) - num, err := c.ParamsInt("user") // Check the number matches @@ -4774,8 +4773,6 @@ func TestCtx_ParamsInt(t *testing.T) { // In this test case, there will be a bad request where the expected number is NOT // a number in the path app.Get("/testnoint/:user", func(c Ctx) error { - // require.Equal(t, "john", c.Params("user")) - num, err := c.ParamsInt("user") // Check the number matches @@ -4790,8 +4787,6 @@ func TestCtx_ParamsInt(t *testing.T) { // For the user id I will use the number 2222, so I should be able to get the number // 2222 from the Ctx even when the default value is specified app.Get("/testignoredefault/:user", func(c Ctx) error { - // require.Equal(t, "john", c.Params("user")) - num, err := c.ParamsInt("user", 1111) // Check the number matches @@ -4806,8 +4801,6 @@ func TestCtx_ParamsInt(t *testing.T) { // In this test case, there will be a bad request where the expected number is NOT // a number in the path, default value of 1111 should be used instead app.Get("/testdefault/:user", func(c Ctx) error { - // require.Equal(t, "john", c.Params("user")) - num, err := c.ParamsInt("user", 1111) // Check the number matches diff --git a/helpers.go b/helpers.go index a07e3253..68916482 100644 --- a/helpers.go +++ b/helpers.go @@ -94,7 +94,6 @@ func readContent(rf io.ReaderFrom, name string) (int64, error) { // quoteString escape special characters in a given string func (app *App) quoteString(raw string) string { bb := bytebufferpool.Get() - // quoted := string(fasthttp.AppendQuotedArg(bb.B, getBytes(raw))) quoted := app.getString(fasthttp.AppendQuotedArg(bb.B, app.getBytes(raw))) bytebufferpool.Put(bb) return quoted @@ -462,13 +461,14 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head // Get specificity var specificity int // check for wildcard this could be a mime */* or a wildcard character * - if string(spec) == "*/*" || string(spec) == "*" { + switch { + case string(spec) == "*/*" || string(spec) == "*": specificity = 1 - } else if bytes.HasSuffix(spec, []byte("/*")) { + case bytes.HasSuffix(spec, []byte("/*")): specificity = 2 - } else if bytes.IndexByte(spec, '/') != -1 { + case bytes.IndexByte(spec, '/') != -1: specificity = 3 - } else { + default: specificity = 4 } diff --git a/middleware/keyauth/keyauth_test.go b/middleware/keyauth/keyauth_test.go index 5b8d7d57..36607d9e 100644 --- a/middleware/keyauth/keyauth_test.go +++ b/middleware/keyauth/keyauth_test.go @@ -89,15 +89,16 @@ func TestAuthSources(t *testing.T) { require.NoError(t, err) // setup the apikey for the different auth schemes - if authSource == "header" { + switch authSource { + case "header": req.Header.Set(test.authTokenName, test.APIKey) - } else if authSource == "cookie" { + case "cookie": req.Header.Set("Cookie", test.authTokenName+"="+test.APIKey) - } else if authSource == "query" || authSource == "form" { + case "query", "form": q := req.URL.Query() q.Add(test.authTokenName, test.APIKey) req.URL.RawQuery = q.Encode() - } else if authSource == "param" { + case "param": r := req.URL.Path r += url.PathEscape(test.APIKey) req.URL.Path = r diff --git a/middleware/logger/default_logger.go b/middleware/logger/default_logger.go index 0070b720..5744fd1a 100644 --- a/middleware/logger/default_logger.go +++ b/middleware/logger/default_logger.go @@ -108,11 +108,12 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error { var err error // Loop over template parts execute dynamic parts and add fixed parts to the buffer for i, logFunc := range data.LogFuncChain { - if logFunc == nil { + switch { + case logFunc == nil: buf.Write(data.TemplateChain[i]) - } else if data.TemplateChain[i] == nil { + case data.TemplateChain[i] == nil: _, err = logFunc(buf, c, data, "") - } else { + default: _, err = logFunc(buf, c, data, utils.UnsafeString(data.TemplateChain[i])) } if err != nil { diff --git a/middleware/session/store.go b/middleware/session/store.go index c1f36ec9..dbca8018 100644 --- a/middleware/session/store.go +++ b/middleware/session/store.go @@ -72,18 +72,20 @@ func (s *Store) Get(c fiber.Ctx) (*Session, error) { if loadData { raw, err := s.Storage.Get(id) // Unmarshal if we found data - if raw != nil && err == nil { + switch { + case err != nil: + return nil, err + + case raw != nil: mux.Lock() defer mux.Unlock() - _, _ = sess.byteBuffer.Write(raw) // Ignore error, this will never fail + sess.byteBuffer.Write(raw) encCache := gob.NewDecoder(sess.byteBuffer) err := encCache.Decode(&sess.data.Data) if err != nil { return nil, fmt.Errorf("failed to decode session data: %w", err) } - } else if err != nil { - return nil, err - } else { + default: // both raw and err is nil, which means id is not in the storage sess.fresh = true }