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
Add linting with golangci-lint #722
Conversation
0da0d80
to
b04a972
Compare
@deepmap-marcinr mind having a look? I need to clean up the commit history (hence this still being a draft) but would be good to get this through to start giving us a bit more control + confidence in our code style, as well as closing #58. |
@@ -122,12 +122,12 @@ func ValidateRequestFromContext(c *gin.Context, router routers.Router, options * | |||
|
|||
// Pass the gin context into the request validator, so that any callbacks | |||
// which it invokes make it available. | |||
requestContext := context.WithValue(context.Background(), GinContextKey, c) | |||
requestContext := context.WithValue(context.Background(), GinContextKey, c) //nolint:staticcheck |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
@@ -99,7 +99,7 @@ func MarshalForm(ptr interface{}, encodings map[string]RequestBodyEncoding) (url | |||
tag = strings.Split(tag, ",")[0] // extract the name of the tag | |||
if encoding, ok := encodings[tag]; ok && encoding.ContentType != "" { | |||
if strings.HasPrefix(encoding.ContentType, jsonContentType) { | |||
if data, err := json.Marshal(field); err != nil { | |||
if data, err := json.Marshal(field); err != nil { //nolint:staticcheck |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SA9005: struct type 'reflect.Value' doesn't have any exported fields, nor custom marshaling
@@ -242,7 +242,7 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error { | |||
tm, err = time.Parse(time.RFC3339Nano, pathValues.value) | |||
if err != nil { | |||
// Fall back to parsing it as a date. | |||
tm, err = time.Parse(types.DateFormat, pathValues.value) | |||
tm, err = time.Parse(types.DateFormat, pathValues.value) //nolint:ineffassign,staticcheck |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ineffectual assignment to tm (ineffassign)
8cec783
to
dc33c3c
Compare
I rebased your branch, and fixed up a bunch of merge conflicts, however, you're making breaking API changes due to initialisms - people depending on the testutils or runtime packages will see code break. Our hands are kinda tied there until a breaking release. |
1c3f13d
to
677718e
Compare
Ah that's fair, I'll ignore the initialisms check, for now then 👍 |
Hi @jamietanna I added |
Right now, we can ignore the return values, as we're hardcoding the response of an `err` to always return, which isn't ideal.
As we've added them on purpose, to show that they don't get (un)marshalled.
As it's probably a bit late to change this, in case it changes expected behaviour, via SA1029.
Via SA1019
f36b5a7
to
f92f881
Compare
Or, least show that we've considered whether to ignore them, which we do to follow existing style of the file to ignore everything.
As we'll come back to it later. pkg/runtime/deepobject.go:245:5: ineffectual assignment to tm (ineffassign) tm, err = time.Parse(types.DateFormat, pathValues.value)
pkg/runtime/bindform.go:102:34: SA9005: struct type 'reflect.Value' doesn't have any exported fields, nor custom marshaling (staticcheck) if data, err := json.Marshal(field); err != nil {
To give a bit more consistency across the project with the Go ecosystem, as well as picking up on some common errors that new contributions can bring, enable golangci-lint, with default configuration.
f92f881
to
5a103c5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few nits but the the main issue is just using _ = function()
to quell error not checked, we should never do that.
@@ -26,7 +26,8 @@ func TestClient_WhenPathHasColon_RequestHasCorrectPath(t *testing.T) { | |||
assert.Equal(t, "http://host/pets:validate", req.URL.String()) | |||
}) | |||
|
|||
client.ValidatePetsWithResponse(context.Background(), ValidatePetsJSONRequestBody{ | |||
// TODO: this err should be checked, but it defaults to returning the "something went wrong", so we should refactor accordingly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should just check the errors, its easy enough, also check the errors for NewClient above
@@ -28,14 +28,14 @@ func walkSwagger(swagger *openapi3.T, doFn func(RefWrapper) (bool, error)) error | |||
|
|||
for _, p := range swagger.Paths { | |||
for _, param := range p.Parameters { | |||
walkParameterRef(param, doFn) | |||
_ = walkParameterRef(param, doFn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should never assign to _
just to silence a lint error, either check the error if its genuine or add a //nolint: errcheck
comment to the line when its not such as a .Close()
on a io.ReadCloser()
, lots more below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although yes, I 100% agree, the file so far has a lot of these, and I'd prefer us to come back and sort them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see a two options:
- Disable
errcheck
linter in.golangci-lint.yaml
for now. - Use ci: enable golangci-lint and fix errors #845 which already fixes all of these.
Great to see this merged! I've rebased my golangci-lint changes on top of it, hopefully we can get that in too. |
Add linting with golangci-lint
No description provided.