Skip to content

ci: golangci-linter add unparam#3111

Open
leongross wants to merge 2 commits intou-root:mainfrom
leongross:ci/linter
Open

ci: golangci-linter add unparam#3111
leongross wants to merge 2 commits intou-root:mainfrom
leongross:ci/linter

Conversation

@leongross
Copy link
Member

While working on PR #3109 I ran into the issue, that a function was changed in such a way, that the signature was still taking an unused parameter. The Github CI did not pick this up. We should add detection to enforce a cleaner codebase.

@binjip978
Copy link
Contributor

I think it's a good linter and it can find some real issues, but I propose to fix issues first to avoid confusion from other contributors. I can help with that.

@leongross
Copy link
Member Author

I agree that PR #3106 got quite messy, but I think this PR does it better. Adding the linter is jus +1 and the other fixes are linter fixes only. Or what do you propose?

@leongross leongross force-pushed the ci/linter branch 7 times, most recently from 2d006bc to b8acc96 Compare September 26, 2024 10:33
Copy link
Member Author

@leongross leongross left a comment

Choose a reason for hiding this comment

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

The linter issues all seem to be resolved, I would appreciate a review @binjip978 @rminnich @RiSKeD

@leongross
Copy link
Member Author

After we finish this, I propose adding errcheck. Because this PR also adds error checks that were not here before it makes sense to wait until this is merged to prevent unnecessary double work.

@codecov
Copy link

codecov bot commented Sep 26, 2024

Codecov Report

Attention: Patch coverage is 63.46154% with 19 lines in your changes missing coverage. Please review.

Project coverage is 60.53%. Comparing base (a0070cb) to head (99a993e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3111      +/-   ##
==========================================
- Coverage   60.67%   60.53%   -0.15%     
==========================================
  Files         598      598              
  Lines       39890    39888       -2     
==========================================
- Hits        24204    24145      -59     
- Misses      15686    15743      +57     
Flag Coverage Δ
.-amd64 90.69% <ø> (ø)
cmds/...-amd64 53.10% <65.11%> (+0.02%) ⬆️
integration/generic-tests/...-amd64 19.82% <ø> (ø)
integration/generic-tests/...-arm 11.63% <ø> (ø)
integration/generic-tests/...-arm64 16.38% <ø> (ø)
integration/gotests/...-amd64 63.18% <77.50%> (-0.26%) ⬇️
integration/gotests/...-arm 64.23% <77.50%> (+0.01%) ⬆️
integration/gotests/...-arm64 64.36% <77.50%> (+0.01%) ⬆️
pkg/...-amd64 60.27% <22.22%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
everything 65.51% <72.09%> (-0.18%) ⬇️
cmds/exp 33.21% <22.22%> (-0.01%) ⬇️

RiSKeD
RiSKeD previously approved these changes Sep 26, 2024
Copy link
Contributor

@RiSKeD RiSKeD left a comment

Choose a reason for hiding this comment

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

LGTM!
IMO it makes sense to leave some nolint cases behind for functions we don't want to refactor right now, as we get the benefits of this linter for any new commits 👍

@leongross
Copy link
Member Author

@binjip978 @rminnich

binjip978
binjip978 previously approved these changes Sep 27, 2024
Copy link
Contributor

@binjip978 binjip978 left a comment

Choose a reason for hiding this comment

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

Fine by me, lets see what @rminnich thinks about it

@leongross leongross requested a review from binjip978 October 2, 2024 10:01
@leongross leongross added the Awaiting reviewer Waiting for a reviewer. label Oct 2, 2024

if pnote.PID == 0 {
s.WriteString("-")
if _, err = s.WriteString("-"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear to me how WriteString can fail here?

// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s), nil
}

I would rather have something like _, _ = s.WriteString("-")

Copy link
Member Author

Choose a reason for hiding this comment

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

This sounds reasonable. This could also be a good solution for the flag parsing issues @rminnich stated.

Copy link
Member

@rminnich rminnich left a comment

Choose a reason for hiding this comment

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

I would want to know if we can not replace all those ugly no lint directives with _ parameters, to start.

return nil
}

//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

rather than the no lint: thing, you ought to be able to say
func reply(_ io.Writer, r byte) {...}

If golangci-lint does not understand this, then it does not understand go.

// hardcoding seems not be a good idea since we may need this function for
// future testing
//
//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

Again, the common way to indicate an unused parameter is
func f(_ int, something int)
does golangci-lint not understand this?
I really don't like these no lint: things everywhere. It means, when code changes, people also need to understand to change the nolint, and they will forget. And then we'll have a bogus no lint directive. It's why these extraterritorial things are such a problem.
Does golangci-lint understand _ parameters? If not, it should.

Copy link
Member

Choose a reason for hiding this comment

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

Just confirmed: _ the param and golangci-lint will not complain.
remove this nolint: there is a Go-standard way to do this.

return int(clktck), nil
}

//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

and if we have all these unused parameters, why don't we just fix the code?

// TODO: I am not sure what the status block is passed here and if it is really
// necessary, this should be audited
//
//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

so we have code we don't understand that we're patching up with no lint directives? Kinda nasty?

@@ -106,11 +106,7 @@ func (c cmd) Modify(ns Namespace, b *Builder) error {
}
return ns.Import(host, remotepath, mountpoint, c.flag)
Copy link
Member

Choose a reason for hiding this comment

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

This looks good. This one change is a case for making this into more than one PR. It's SO BIT, it's exhausting to read.

}

s.WriteString(pnote.Name)
_, _ = s.WriteString(pnote.Name)
Copy link
Member

Choose a reason for hiding this comment

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

this is just so ugly, but I guess we have no choice, but yuck.

@rminnich
Copy link
Member

So far, golangci-lint mainly seems to make code unnecessarily ugly, and seems to be a way to avoid fixing things ...

Copy link
Member

@rminnich rminnich left a comment

Choose a reason for hiding this comment

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

every single nolint: here is making things worse. Let's fix things, in a series of PR, and we can even have a golangci-lint test that is not required (at first) to show us what we need to continue to fix.
I really dislike things like nolint: in code. Over time, they get stale and cause trouble.

// disable the noparam linter to fail because we always return nil.
//
//nolint:unparam
func (c *cmd) run() error {
Copy link
Member

Choose a reason for hiding this comment

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

This is the wrong fix.
What should be done is to use errors.Join
so this command and the nolint: actually make things worse.
This is why I am so concerned about this PR. It is guiding people to do the wrong thing.

// TODO: rework 'what' parameter, maybe remove it?
//
//nolint:unparam
func getArg(b *bufio.Reader, what string) string {
Copy link
Member

Choose a reason for hiding this comment

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

OK, I just now tested this change:

-func getArg(b *bufio.Reader, what string) string {
+func getArg(b *bufio.Reader, _ string) string {

rminnich@sidecore:~/go/src/github.com/u-root/u-root$ golangci-lint run cmds/exp/rush/parse.go
rminnich@sidecore:~/go/src/github.com/u-root/u-root$ 

golangci-lint is fine with _ parameters.

So, the fix in the PR is the wrong fix. Adding the nolint: makes things worse.

Either remove what, which is the better solution; or make it _ string.

I would bet that just about every nolint: in this PR could be done a better way.


// TODO: refactor to return error instead of logger.Fatalf
//
//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

If you do a TODO, it will never get done. Let's fix this right.

// hardcoding seems not be a good idea since we may need this function for
// future testing
//
//nolint:unparam
Copy link
Member

Choose a reason for hiding this comment

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

Just confirmed: _ the param and golangci-lint will not complain.
remove this nolint: there is a Go-standard way to do this.

add linter option 'unparam' to ci

Signed-off-by: leongross <leon.gross@9elements.com>
* fix unparam issues, remove unused error returns
* fix changed tests when no error is returned anymore
* add TODOs and //nolint:unparam tags for future rework/refactoring

Signed-off-by: leongross <leon.gross@9elements.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Awaiting reviewer Waiting for a reviewer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants