Skip to content

Commit b04917c

Browse files
authored
chore: upgrade golangci-lint and fix golangci-lint error (gin-gonic#3278)
1 parent 1b5ba25 commit b04917c

12 files changed

+74
-60
lines changed

.github/workflows/gin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Setup golangci-lint
2222
uses: golangci/[email protected]
2323
with:
24-
version: v1.45.0
24+
version: v1.48.0
2525
args: --verbose
2626
test:
2727
needs: lint

context.go

+40-30
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ func (c *Context) Handler() HandlerFunc {
153153

154154
// FullPath returns a matched route full path. For not found routes
155155
// returns an empty string.
156-
// router.GET("/user/:id", func(c *gin.Context) {
157-
// c.FullPath() == "/user/:id" // true
158-
// })
156+
//
157+
// router.GET("/user/:id", func(c *gin.Context) {
158+
// c.FullPath() == "/user/:id" // true
159+
// })
159160
func (c *Context) FullPath() string {
160161
return c.fullPath
161162
}
@@ -382,10 +383,11 @@ func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)
382383

383384
// Param returns the value of the URL param.
384385
// It is a shortcut for c.Params.ByName(key)
385-
// router.GET("/user/:id", func(c *gin.Context) {
386-
// // a GET request to /user/john
387-
// id := c.Param("id") // id == "john"
388-
// })
386+
//
387+
// router.GET("/user/:id", func(c *gin.Context) {
388+
// // a GET request to /user/john
389+
// id := c.Param("id") // id == "john"
390+
// })
389391
func (c *Context) Param(key string) string {
390392
return c.Params.ByName(key)
391393
}
@@ -402,11 +404,12 @@ func (c *Context) AddParam(key, value string) {
402404
// Query returns the keyed url query value if it exists,
403405
// otherwise it returns an empty string `("")`.
404406
// It is shortcut for `c.Request.URL.Query().Get(key)`
405-
// GET /path?id=1234&name=Manu&value=
406-
// c.Query("id") == "1234"
407-
// c.Query("name") == "Manu"
408-
// c.Query("value") == ""
409-
// c.Query("wtf") == ""
407+
//
408+
// GET /path?id=1234&name=Manu&value=
409+
// c.Query("id") == "1234"
410+
// c.Query("name") == "Manu"
411+
// c.Query("value") == ""
412+
// c.Query("wtf") == ""
410413
func (c *Context) Query(key string) (value string) {
411414
value, _ = c.GetQuery(key)
412415
return
@@ -415,10 +418,11 @@ func (c *Context) Query(key string) (value string) {
415418
// DefaultQuery returns the keyed url query value if it exists,
416419
// otherwise it returns the specified defaultValue string.
417420
// See: Query() and GetQuery() for further information.
418-
// GET /?name=Manu&lastname=
419-
// c.DefaultQuery("name", "unknown") == "Manu"
420-
// c.DefaultQuery("id", "none") == "none"
421-
// c.DefaultQuery("lastname", "none") == ""
421+
//
422+
// GET /?name=Manu&lastname=
423+
// c.DefaultQuery("name", "unknown") == "Manu"
424+
// c.DefaultQuery("id", "none") == "none"
425+
// c.DefaultQuery("lastname", "none") == ""
422426
func (c *Context) DefaultQuery(key, defaultValue string) string {
423427
if value, ok := c.GetQuery(key); ok {
424428
return value
@@ -430,10 +434,11 @@ func (c *Context) DefaultQuery(key, defaultValue string) string {
430434
// if it exists `(value, true)` (even when the value is an empty string),
431435
// otherwise it returns `("", false)`.
432436
// It is shortcut for `c.Request.URL.Query().Get(key)`
433-
// GET /?name=Manu&lastname=
434-
// ("Manu", true) == c.GetQuery("name")
435-
// ("", false) == c.GetQuery("id")
436-
// ("", true) == c.GetQuery("lastname")
437+
//
438+
// GET /?name=Manu&lastname=
439+
// ("Manu", true) == c.GetQuery("name")
440+
// ("", false) == c.GetQuery("id")
441+
// ("", true) == c.GetQuery("lastname")
437442
func (c *Context) GetQuery(key string) (string, bool) {
438443
if values, ok := c.GetQueryArray(key); ok {
439444
return values[0], ok
@@ -500,9 +505,10 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
500505
// form or multipart form when it exists `(value, true)` (even when the value is an empty string),
501506
// otherwise it returns ("", false).
502507
// For example, during a PATCH request to update the user's email:
503-
// [email protected] --> ("[email protected]", true) := GetPostForm("email") // set email to "[email protected]"
504-
// email= --> ("", true) := GetPostForm("email") // set email to ""
505-
// --> ("", false) := GetPostForm("email") // do nothing with email
508+
//
509+
// [email protected] --> ("[email protected]", true) := GetPostForm("email") // set email to "[email protected]"
510+
// email= --> ("", true) := GetPostForm("email") // set email to ""
511+
// --> ("", false) := GetPostForm("email") // do nothing with email
506512
func (c *Context) GetPostForm(key string) (string, bool) {
507513
if values, ok := c.GetPostFormArray(key); ok {
508514
return values[0], ok
@@ -607,8 +613,10 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
607613

608614
// Bind checks the Method and Content-Type to select a binding engine automatically,
609615
// Depending on the "Content-Type" header different bindings are used, for example:
610-
// "application/json" --> JSON binding
611-
// "application/xml" --> XML binding
616+
//
617+
// "application/json" --> JSON binding
618+
// "application/xml" --> XML binding
619+
//
612620
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
613621
// It decodes the json payload into the struct specified as a pointer.
614622
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
@@ -651,7 +659,7 @@ func (c *Context) BindHeader(obj any) error {
651659
// It will abort the request with HTTP 400 if any error occurs.
652660
func (c *Context) BindUri(obj any) error {
653661
if err := c.ShouldBindUri(obj); err != nil {
654-
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) // nolint: errcheck
662+
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
655663
return err
656664
}
657665
return nil
@@ -662,16 +670,18 @@ func (c *Context) BindUri(obj any) error {
662670
// See the binding package.
663671
func (c *Context) MustBindWith(obj any, b binding.Binding) error {
664672
if err := c.ShouldBindWith(obj, b); err != nil {
665-
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) // nolint: errcheck
673+
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
666674
return err
667675
}
668676
return nil
669677
}
670678

671679
// ShouldBind checks the Method and Content-Type to select a binding engine automatically,
672680
// Depending on the "Content-Type" header different bindings are used, for example:
673-
// "application/json" --> JSON binding
674-
// "application/xml" --> XML binding
681+
//
682+
// "application/json" --> JSON binding
683+
// "application/xml" --> XML binding
684+
//
675685
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
676686
// It decodes the json payload into the struct specified as a pointer.
677687
// Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.
@@ -1112,7 +1122,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
11121122
c.TOML(code, data)
11131123

11141124
default:
1115-
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) // nolint: errcheck
1125+
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) //nolint: errcheck
11161126
}
11171127
}
11181128

context_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestContextReset(t *testing.T) {
152152
c.index = 2
153153
c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
154154
c.Params = Params{Param{}}
155-
c.Error(errors.New("test")) // nolint: errcheck
155+
c.Error(errors.New("test")) //nolint: errcheck
156156
c.Set("foo", "bar")
157157
c.reset()
158158

@@ -1376,12 +1376,12 @@ func TestContextError(t *testing.T) {
13761376
assert.Empty(t, c.Errors)
13771377

13781378
firstErr := errors.New("first error")
1379-
c.Error(firstErr) // nolint: errcheck
1379+
c.Error(firstErr) //nolint: errcheck
13801380
assert.Len(t, c.Errors, 1)
13811381
assert.Equal(t, "Error #01: first error\n", c.Errors.String())
13821382

13831383
secondErr := errors.New("second error")
1384-
c.Error(&Error{ // nolint: errcheck
1384+
c.Error(&Error{ //nolint: errcheck
13851385
Err: secondErr,
13861386
Meta: "some data 2",
13871387
Type: ErrorTypePublic,
@@ -1403,13 +1403,13 @@ func TestContextError(t *testing.T) {
14031403
t.Error("didn't panic")
14041404
}
14051405
}()
1406-
c.Error(nil) // nolint: errcheck
1406+
c.Error(nil) //nolint: errcheck
14071407
}
14081408

14091409
func TestContextTypedError(t *testing.T) {
14101410
c, _ := CreateTestContext(httptest.NewRecorder())
1411-
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic) // nolint: errcheck
1412-
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate) // nolint: errcheck
1411+
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic) //nolint: errcheck
1412+
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate) //nolint: errcheck
14131413

14141414
for _, err := range c.Errors.ByType(ErrorTypePublic) {
14151415
assert.Equal(t, ErrorTypePublic, err.Type)
@@ -1424,7 +1424,7 @@ func TestContextAbortWithError(t *testing.T) {
14241424
w := httptest.NewRecorder()
14251425
c, _ := CreateTestContext(w)
14261426

1427-
c.AbortWithError(http.StatusUnauthorized, errors.New("bad input")).SetMeta("some input") // nolint: errcheck
1427+
c.AbortWithError(http.StatusUnauthorized, errors.New("bad input")).SetMeta("some input") //nolint: errcheck
14281428

14291429
assert.Equal(t, http.StatusUnauthorized, w.Code)
14301430
assert.Equal(t, abortIndex, c.index)

errors.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ func (a errorMsgs) Last() *Error {
124124

125125
// Errors returns an array with all the error messages.
126126
// Example:
127-
// c.Error(errors.New("first"))
128-
// c.Error(errors.New("second"))
129-
// c.Error(errors.New("third"))
130-
// c.Errors.Errors() // == []string{"first", "second", "third"}
127+
//
128+
// c.Error(errors.New("first"))
129+
// c.Error(errors.New("second"))
130+
// c.Error(errors.New("third"))
131+
// c.Errors.Errors() // == []string{"first", "second", "third"}
131132
func (a errorMsgs) Errors() []string {
132133
if len(a) == 0 {
133134
return nil

errors_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestError(t *testing.T) {
3535
jsonBytes, _ := json.Marshal(err)
3636
assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
3737

38-
err.SetMeta(H{ // nolint: errcheck
38+
err.SetMeta(H{ //nolint: errcheck
3939
"status": "200",
4040
"data": "some data",
4141
})
@@ -45,7 +45,7 @@ func TestError(t *testing.T) {
4545
"data": "some data",
4646
}, err.JSON())
4747

48-
err.SetMeta(H{ // nolint: errcheck
48+
err.SetMeta(H{ //nolint: errcheck
4949
"error": "custom error",
5050
"status": "200",
5151
"data": "some data",
@@ -60,7 +60,7 @@ func TestError(t *testing.T) {
6060
status string
6161
data string
6262
}
63-
err.SetMeta(customError{status: "200", data: "other data"}) // nolint: errcheck
63+
err.SetMeta(customError{status: "200", data: "other data"}) //nolint: errcheck
6464
assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())
6565
}
6666

ginS/gins.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func StaticFile(relativePath, filepath string) gin.IRoutes {
108108
// of the Router's NotFound handler.
109109
// To use the operating system's file system implementation,
110110
// use :
111-
// router.Static("/static", "/var/www")
111+
//
112+
// router.Static("/static", "/var/www")
112113
func Static(relativePath, root string) gin.IRoutes {
113114
return engine().Static(relativePath, root)
114115
}

logger_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,13 @@ func TestErrorLogger(t *testing.T) {
358358
router := New()
359359
router.Use(ErrorLogger())
360360
router.GET("/error", func(c *Context) {
361-
c.Error(errors.New("this is an error")) // nolint: errcheck
361+
c.Error(errors.New("this is an error")) //nolint: errcheck
362362
})
363363
router.GET("/abort", func(c *Context) {
364-
c.AbortWithError(http.StatusUnauthorized, errors.New("no authorized")) // nolint: errcheck
364+
c.AbortWithError(http.StatusUnauthorized, errors.New("no authorized")) //nolint: errcheck
365365
})
366366
router.GET("/print", func(c *Context) {
367-
c.Error(errors.New("this is an error")) // nolint: errcheck
367+
c.Error(errors.New("this is an error")) //nolint: errcheck
368368
c.String(http.StatusInternalServerError, "hola!")
369369
})
370370

middleware_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func TestMiddlewareFailHandlersChain(t *testing.T) {
211211
router := New()
212212
router.Use(func(context *Context) {
213213
signature += "A"
214-
context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) // nolint: errcheck
214+
context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) //nolint: errcheck
215215
})
216216
router.Use(func(context *Context) {
217217
signature += "B"

mode.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ const (
3535
// Note that both Logger and Recovery provides custom ways to configure their
3636
// output io.Writer.
3737
// To support coloring in Windows use:
38-
// import "github.com/mattn/go-colorable"
39-
// gin.DefaultWriter = colorable.NewColorableStdout()
38+
//
39+
// import "github.com/mattn/go-colorable"
40+
// gin.DefaultWriter = colorable.NewColorableStdout()
4041
var DefaultWriter io.Writer = os.Stdout
4142

4243
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors

path.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ package gin
1010
//
1111
// The following rules are applied iteratively until no further processing can
1212
// be done:
13-
// 1. Replace multiple slashes with a single slash.
14-
// 2. Eliminate each . path name element (the current directory).
15-
// 3. Eliminate each inner .. path name element (the parent directory)
16-
// along with the non-.. element that precedes it.
17-
// 4. Eliminate .. elements that begin a rooted path:
18-
// that is, replace "/.." by "/" at the beginning of a path.
13+
// 1. Replace multiple slashes with a single slash.
14+
// 2. Eliminate each . path name element (the current directory).
15+
// 3. Eliminate each inner .. path name element (the parent directory)
16+
// along with the non-.. element that precedes it.
17+
// 4. Eliminate .. elements that begin a rooted path:
18+
// that is, replace "/.." by "/" at the beginning of a path.
1919
//
2020
// If the result of this process is an empty string, "/" is returned.
2121
func cleanPath(p string) string {

recovery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
9191
}
9292
if brokenPipe {
9393
// If the connection is dead, we can't write a status to it.
94-
c.Error(err.(error)) // nolint: errcheck
94+
c.Error(err.(error)) //nolint: errcheck
9595
c.Abort()
9696
} else {
9797
handle(c, err)

routergroup.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ func (group *RouterGroup) staticFileHandler(relativePath string, handler Handler
182182
// of the Router's NotFound handler.
183183
// To use the operating system's file system implementation,
184184
// use :
185-
// router.Static("/static", "/var/www")
185+
//
186+
// router.Static("/static", "/var/www")
186187
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
187188
return group.StaticFS(relativePath, Dir(root, false))
188189
}

0 commit comments

Comments
 (0)