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
This commit is contained in:
nickajacks1 2024-03-18 06:50:40 -07:00 committed by GitHub
parent 5449b04101
commit 82070cb4c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 31 additions and 43 deletions

View File

@ -72,17 +72,12 @@ linters-settings:
gocritic: gocritic:
# TODO: Uncomment the following lines # TODO: Uncomment the following lines
# enabled-tags: enabled-tags:
# - diagnostic - diagnostic
# - style # - style
# - performance # - performance
# - experimental # - experimental
# - opinionated # - opinionated
disabled-checks:
- ifElseChain # TODO: Do not disable
# - hugeParam
# - rangeExprCopy
# - rangeValCopy
settings: settings:
captLocal: captLocal:
paramsOnly: false paramsOnly: false
@ -195,6 +190,8 @@ linters-settings:
disabled: true disabled: true
- name: unchecked-type-assertion - name: unchecked-type-assertion
disabled: true # TODO: Do not disable disabled: true # TODO: Do not disable
- name: unhandled-error
arguments: ['bytes\.Buffer\.Write']
stylecheck: stylecheck:
checks: checks:
@ -217,9 +214,6 @@ linters-settings:
testifylint: testifylint:
enable-all: true enable-all: true
# TODO: Do not disable any options
disable:
- go-require
testpackage: testpackage:
skip-regexp: "^$" skip-regexp: "^$"

3
app.go
View File

@ -988,8 +988,7 @@ func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Respons
type disableLogger struct{} type disableLogger struct{}
func (*disableLogger) Printf(_ string, _ ...any) { func (*disableLogger) Printf(string, ...any) {
// fmt.Println(fmt.Sprintf(format, args...))
} }
func (app *App) init() *App { func (app *App) init() *App {

View File

@ -1316,13 +1316,11 @@ func Test_App_Group(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(MethodPost, "/test/v1/", nil)) resp, err := app.Test(httptest.NewRequest(MethodPost, "/test/v1/", nil))
require.NoError(t, err, "app.Test(req)") require.NoError(t, err, "app.Test(req)")
require.Equal(t, 200, resp.StatusCode, "Status code") require.Equal(t, 200, resp.StatusCode, "Status code")
// require.Equal(t, "/test/v1", resp.Header.Get("Location"), "Location")
api.Get("/users", dummyHandler) api.Get("/users", dummyHandler)
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test/v1/UsErS", nil)) resp, err = app.Test(httptest.NewRequest(MethodGet, "/test/v1/UsErS", nil))
require.NoError(t, err, "app.Test(req)") require.NoError(t, err, "app.Test(req)")
require.Equal(t, 200, resp.StatusCode, "Status code") 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) { func Test_App_Route(t *testing.T) {

View File

@ -714,7 +714,6 @@ func Benchmark_Bind_Query_Comma(b *testing.B) {
} }
c.Request().SetBody([]byte(``)) c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("") 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") c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football")
q := new(Query) q := new(Query)
b.ReportAllocs() b.ReportAllocs()

View File

@ -183,11 +183,12 @@ func Test_Parser_Request_URL(t *testing.T) {
flag1, flag2, flag3 := false, false, false flag1, flag2, flag3 := false, false, false
for _, v := range values["bar"] { for _, v := range values["bar"] {
if v == "foo1" { switch v {
case "foo1":
flag1 = true flag1 = true
} else if v == "foo2" { case "foo2":
flag2 = true flag2 = true
} else if v == "foo" { case "foo":
flag3 = true flag3 = true
} }
} }

View File

@ -1435,6 +1435,7 @@ func Test_Ctx_Parsers(t *testing.T) {
}) })
t.Run("ParamsParser", func(t *testing.T) { t.Run("ParamsParser", func(t *testing.T) {
t.Skip("ParamsParser is not ready for v3") t.Skip("ParamsParser is not ready for v3")
//nolint:gocritic // TODO: uncomment
// t.Parallel() // t.Parallel()
// withValues(t, func(c Ctx, testStruct *TestStruct) error { // withValues(t, func(c Ctx, testStruct *TestStruct) error {
// c.route = &Route{Params: []string{"name", "name2", "class", "class2"}} // 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 // For the user id I will use the number 1111, so I should be able to get the number
// 1111 from the Ctx // 1111 from the Ctx
app.Get("/test/:user", func(c Ctx) error { app.Get("/test/:user", func(c Ctx) error {
// require.Equal(t, "john", c.Params("user"))
num, err := c.ParamsInt("user") num, err := c.ParamsInt("user")
// Check the number matches // 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 // In this test case, there will be a bad request where the expected number is NOT
// a number in the path // a number in the path
app.Get("/testnoint/:user", func(c Ctx) error { app.Get("/testnoint/:user", func(c Ctx) error {
// require.Equal(t, "john", c.Params("user"))
num, err := c.ParamsInt("user") num, err := c.ParamsInt("user")
// Check the number matches // 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 // 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 // 2222 from the Ctx even when the default value is specified
app.Get("/testignoredefault/:user", func(c Ctx) error { app.Get("/testignoredefault/:user", func(c Ctx) error {
// require.Equal(t, "john", c.Params("user"))
num, err := c.ParamsInt("user", 1111) num, err := c.ParamsInt("user", 1111)
// Check the number matches // 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 // 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 // a number in the path, default value of 1111 should be used instead
app.Get("/testdefault/:user", func(c Ctx) error { app.Get("/testdefault/:user", func(c Ctx) error {
// require.Equal(t, "john", c.Params("user"))
num, err := c.ParamsInt("user", 1111) num, err := c.ParamsInt("user", 1111)
// Check the number matches // Check the number matches

View File

@ -94,7 +94,6 @@ func readContent(rf io.ReaderFrom, name string) (int64, error) {
// quoteString escape special characters in a given string // quoteString escape special characters in a given string
func (app *App) quoteString(raw string) string { func (app *App) quoteString(raw string) string {
bb := bytebufferpool.Get() bb := bytebufferpool.Get()
// quoted := string(fasthttp.AppendQuotedArg(bb.B, getBytes(raw)))
quoted := app.getString(fasthttp.AppendQuotedArg(bb.B, app.getBytes(raw))) quoted := app.getString(fasthttp.AppendQuotedArg(bb.B, app.getBytes(raw)))
bytebufferpool.Put(bb) bytebufferpool.Put(bb)
return quoted return quoted
@ -462,13 +461,14 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head
// Get specificity // Get specificity
var specificity int var specificity int
// check for wildcard this could be a mime */* or a wildcard character * // 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 specificity = 1
} else if bytes.HasSuffix(spec, []byte("/*")) { case bytes.HasSuffix(spec, []byte("/*")):
specificity = 2 specificity = 2
} else if bytes.IndexByte(spec, '/') != -1 { case bytes.IndexByte(spec, '/') != -1:
specificity = 3 specificity = 3
} else { default:
specificity = 4 specificity = 4
} }

View File

@ -89,15 +89,16 @@ func TestAuthSources(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// setup the apikey for the different auth schemes // setup the apikey for the different auth schemes
if authSource == "header" { switch authSource {
case "header":
req.Header.Set(test.authTokenName, test.APIKey) req.Header.Set(test.authTokenName, test.APIKey)
} else if authSource == "cookie" { case "cookie":
req.Header.Set("Cookie", test.authTokenName+"="+test.APIKey) req.Header.Set("Cookie", test.authTokenName+"="+test.APIKey)
} else if authSource == "query" || authSource == "form" { case "query", "form":
q := req.URL.Query() q := req.URL.Query()
q.Add(test.authTokenName, test.APIKey) q.Add(test.authTokenName, test.APIKey)
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
} else if authSource == "param" { case "param":
r := req.URL.Path r := req.URL.Path
r += url.PathEscape(test.APIKey) r += url.PathEscape(test.APIKey)
req.URL.Path = r req.URL.Path = r

View File

@ -108,11 +108,12 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
var err error var err error
// Loop over template parts execute dynamic parts and add fixed parts to the buffer // Loop over template parts execute dynamic parts and add fixed parts to the buffer
for i, logFunc := range data.LogFuncChain { for i, logFunc := range data.LogFuncChain {
if logFunc == nil { switch {
case logFunc == nil:
buf.Write(data.TemplateChain[i]) buf.Write(data.TemplateChain[i])
} else if data.TemplateChain[i] == nil { case data.TemplateChain[i] == nil:
_, err = logFunc(buf, c, data, "") _, err = logFunc(buf, c, data, "")
} else { default:
_, err = logFunc(buf, c, data, utils.UnsafeString(data.TemplateChain[i])) _, err = logFunc(buf, c, data, utils.UnsafeString(data.TemplateChain[i]))
} }
if err != nil { if err != nil {

View File

@ -72,18 +72,20 @@ func (s *Store) Get(c fiber.Ctx) (*Session, error) {
if loadData { if loadData {
raw, err := s.Storage.Get(id) raw, err := s.Storage.Get(id)
// Unmarshal if we found data // Unmarshal if we found data
if raw != nil && err == nil { switch {
case err != nil:
return nil, err
case raw != nil:
mux.Lock() mux.Lock()
defer mux.Unlock() defer mux.Unlock()
_, _ = sess.byteBuffer.Write(raw) // Ignore error, this will never fail sess.byteBuffer.Write(raw)
encCache := gob.NewDecoder(sess.byteBuffer) encCache := gob.NewDecoder(sess.byteBuffer)
err := encCache.Decode(&sess.data.Data) err := encCache.Decode(&sess.data.Data)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to decode session data: %w", err) return nil, fmt.Errorf("failed to decode session data: %w", err)
} }
} else if err != nil { default:
return nil, err
} else {
// both raw and err is nil, which means id is not in the storage // both raw and err is nil, which means id is not in the storage
sess.fresh = true sess.fresh = true
} }