Skip to content

Enable all golangci-lint linters by default#5445

Merged
negz merged 23 commits intocrossplane:masterfrom
negz:my-pockets-are-full
Mar 4, 2024
Merged

Enable all golangci-lint linters by default#5445
negz merged 23 commits intocrossplane:masterfrom
negz:my-pockets-are-full

Conversation

@negz
Copy link
Copy Markdown
Member

@negz negz commented Mar 1, 2024

Description of your changes

It seems like new linters are being added with each new version of golangci-lint. I like the idea of opting out of new linters we don't like, rather than opting into new linters. This way we're more likely to notice new linters, whether we like them or not.

I think there will be several that we'll want to opt out of. I'll add a commit for each with rationale.

I have:

Need help with this checklist? See the cheat sheet.

negz added 23 commits February 19, 2024 07:41
It seems like new linters are being added with each new version of
golangci-lint. I like the idea of opting out of new linters we don't
like, rather than opting into new linters.

I think there will be several that we'll want to opt out of, which I'll
do in a following commit.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
This takes all linters that are currnently returning warnings about our
codebase and puts them in one of two categories. The first are linters
we'd like to enable, but that will require updates for compliance. The
second are linters we don't want to enable.

Signed-off-by: Nic Cope <nicc@rk0n.org>
With its default configuration this linter just finds fmt.Print* calls,
which are usually debug statements left in by accident.

We had a few of these where we instead should have been using t.Log or
k.Stdout.

Signed-off-by: Nic Cope <nicc@rk0n.org>
The 'nilness' linter notices this in my editor but not in make
reviewable for some reason.

Err could never be nil here because we check err for nilness before the
for loop. The err declaration inside the for loop is scoped to that
loop.

Signed-off-by: Nic Cope <nicc@rk0n.org>
They do the same thing, but calculate cognitive complexity differently.

Notably gocognit considers simple switch statements less complex, but
nested conditionals more complex.

This removes the need for a lot of //nolint:gocyclo comments that we had
just for long switches. I notice nolintlint seems to automatically be
removing these no-op //nolint comments now too, which is great.

Signed-off-by: Nic Cope <nicc@rk0n.org>
We try to avoid using init unless it's really necessary or idiomatic
(e.g. registering Kubernetes API types with the scheme). Generally we
don't want importing our packages to have magic side effects.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
For now we only prevent importing test assertion libraries. In future we
could use it to avoid other things (e.g. MPL licensed libraries).

Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter finds duplicate words, e.g. "the the".

Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter makes sure you don't embed a context in a struct.

We allow this in our table-driven tests, where we use structs to capture
the arguments to be passed to functions.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Lots of //nolint comments for this one, but it seems worth at least
prompting folks to think about.

Signed-off-by: Nic Cope <nicc@rk0n.org>
We often use global variables to represent data that is by convention
constant, but that can't be constant because it's mutable. So there's a
few nolint comments for this, but I think it's still good to discourage
folks.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter warns for interface methods without named parameters.

Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter warns for using predeclared names, like new or append.

Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter warns when you assign a value that will never be used to a
variable.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
We do believe in what this linter is linting for, but in practice we
only seem to be returning interfaces where we need to.

Signed-off-by: Nic Cope <nicc@rk0n.org>
This linter checks that JSON struct tags are Go style camelCase. This is
mostly what we want, especially for apis.

Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
Signed-off-by: Nic Cope <nicc@rk0n.org>
@negz negz force-pushed the my-pockets-are-full branch 3 times, most recently from fd44cda to 3325955 Compare March 3, 2024 00:53
@negz negz marked this pull request as ready for review March 4, 2024 07:23
@negz negz requested review from a team, phisco and turkenh as code owners March 4, 2024 07:23
@negz negz requested a review from bobh66 March 4, 2024 07:23
Comment on lines +29 to +36
# These are linters we'd like to enable, but that will be labor intensive to
# make existing code compliant.
- wrapcheck
- varnamelen
- testpackage
- paralleltest
- nilnil
- gomnd
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these would be useful, but I'm not going to tackle them in this PR. They'd require quite a lot of work to make our existing code compliant (e.g. either wrapping or //nolint-ing all errors).

Copy link
Copy Markdown
Contributor

@phisco phisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @negz!

@negz negz merged commit a43cbf9 into crossplane:master Mar 4, 2024
@negz negz deleted the my-pockets-are-full branch March 4, 2024 22:01
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