Skip to content

fix: linter errors#276

Merged
niemeyer merged 13 commits into
canonical:mainfrom
upils:fix-linter-errors
Apr 27, 2026
Merged

fix: linter errors#276
niemeyer merged 13 commits into
canonical:mainfrom
upils:fix-linter-errors

Conversation

@upils
Copy link
Copy Markdown
Collaborator

@upils upils commented Mar 16, 2026

  • Have you signed the CLA?

Reenable linter rules and fix associated errors excluded in #274.

Following the existing approach, exclusions were added to the golangci-lint
configuration instead of //nolint directives in the code. This could lead to
missing true positives as the exclusion can sometimes be unprecise.

Also remove a now useless exclusion.

@upils upils added the Polish Refactorings, etc label Mar 17, 2026
@upils upils marked this pull request as ready for review March 17, 2026 13:12
@upils upils requested a review from letFunny March 17, 2026 13:13
@upils upils marked this pull request as draft March 17, 2026 13:43
@upils upils force-pushed the fix-linter-errors branch from 29b3f8f to 444110c Compare April 8, 2026 09:29
@upils upils marked this pull request as ready for review April 8, 2026 09:31
@upils upils changed the title Fix linter errors fix: linter errors Apr 21, 2026
Copy link
Copy Markdown
Collaborator

@letFunny letFunny left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this Paul. Let's monitor this closely and see if these become a pain and then disable the checks that are too opinionated.

A couple of minor comments but I don't want to block the PR.

Comment thread cmd/chisel/main.go
name = string(opt.ShortName)
}
desc, ok := c.optDescs[name]
if !(c.optDescs == nil || ok) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not sure how Gustavo feels about this linter. In general I would refrain from re-writing boolean conditions by negating them. While it is true that the result is the same, I think the readability is not the same, and IMO we should respect the author's decision to write it this same if it is more clear / more uniform.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I see your point and I am on the fence on this one.

On one hand, I agree that we might have cases where a form could be more readable or better convey the author's intention. So being forced by the linter might be impractical.

On the other hand, we have more than 150 occurrences of if with && and/or || statements, and the linter only found 2 cases not respecting the rule. Also, maybe I am missing something but for these examples I happen to find the rework statements easier to reason about. So it seems we are already following this rule "naturally". In that case we might as well make it automatically verified and properly exclude and document cases where the form matters.

WDYT?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In these two cases I agree as well, but I find it stylistic more than anything else. I can see how someone prefers the negation. Anyway, no strong opinion on my side, we can try it out and remove if we are not happy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, Alberto has a good point, but this particular case seems like the linter did a good job. The replacement is more readable than the original, and it actually aligns with similar states before and after this one. So +1 on the linter result.

for _, item := range r.Items {
content := item.Content()
digests.WriteString(fmt.Sprintf(" %s %d %s\n", makeSha256(content), len(content), item.Path()))
fmt.Fprintf(&digests, " %s %d %s\n", makeSha256(content), len(content), item.Path())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Interesting that this is automatic, how was this suggested? I checked the API and it appears to be the same minus a possibly different error returned, but we are not using it anyway...

Copy link
Copy Markdown
Collaborator Author

@upils upils Apr 24, 2026

Choose a reason for hiding this comment

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

This is rule QF1012 from staticcheck (see https://go.dev/gopls/analyzers#qf1012-use-fmtfprintfx--instead-of-xwritefmtsprintf). The rationale is partially explained here and this may even be integrated in go vet/go fix at some point, see golang/go#76918.

I also agree that it is a relatively safe change since we are not doing anything with the returned values.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Interesting, let's keep it then

Comment thread .golangci.yaml
Comment on lines +60 to +64
text: "ST1012: error var MissErr"
- linters:
- staticcheck
path: internal/setup/setup.go
text: "ST1012: error var preferNone"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some of these like ST1012 or ST1005 are too opinionated for my taste. Why not disable them in general as you had done before? I don't want to write a new error that is not errSomething and see a linter error, while the existing ones work. If we think the rule is invalid or too opinionated then we should block it directly, not only allow current usages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

My goal here was to:

  • not force us to rename existing errors not respecting the rule (especially because MissErr is exported)
  • but also prevent us from introducing more errors not respecting it.

ST1012 enforces the naming convention recommended in https://go.dev/wiki/Errors#naming, so it does not seem too opinionated to me (at least not more than other idiomatic rules of the language). I even think preferNone could be renamed to respect the rule.

Regarding ST1005: we are already respecting the rule (also implicitly respected in examples of PL007), and this linter rule should help us catch any deviation. I added an exception for cmd/chisel/*.go because the CLI prints errors that require final punctuation to be nice messages to users, so I think the exception is fine here (I am not 100% happy about the granularity here, but this is a limitation due to not using inline comments for exceptions).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you think current exceptions could be renamed then okay and you think the rule is beneficial. What I wouldn't like happening is to add more exceptions to the rule in the future.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I will add a TODO to mention no exception should be added for these rules and existing ones should be removed when possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not fixing these right now? It's just two cases.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I expected the renaming of these errors to be a bit controversial because the unexported one was very carefully named and the other one is exported. So I did not want this to block the bulk of the cleaning. I prepared #293 to address it.

@upils upils requested a review from letFunny April 27, 2026 06:33
Copy link
Copy Markdown
Collaborator

@letFunny letFunny left a comment

Choose a reason for hiding this comment

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

Looks good to me. There are only a couple of minor things that I think need a bit more discussion with Gustavo. I already wrote my opinion but it is not a strong opinion that blocks the PR, I am more than okay with trying this out and see if it because tedious or not.

Copy link
Copy Markdown
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

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

A comment and an open question. I'm merging this right away since there isn't a blocker. We can have a follow up if necessary.

Comment thread cmd/chisel/main.go
name = string(opt.ShortName)
}
desc, ok := c.optDescs[name]
if !(c.optDescs == nil || ok) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, Alberto has a good point, but this particular case seems like the linter did a good job. The replacement is more readable than the original, and it actually aligns with similar states before and after this one. So +1 on the linter result.

Comment thread .golangci.yaml
Comment on lines +60 to +64
text: "ST1012: error var MissErr"
- linters:
- staticcheck
path: internal/setup/setup.go
text: "ST1012: error var preferNone"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not fixing these right now? It's just two cases.

@niemeyer niemeyer merged commit 058417e into canonical:main Apr 27, 2026
18 checks passed
@ROCKsBot
Copy link
Copy Markdown

Command Mean [s] Min [s] Max [s] Relative
BASE 10.732 ± 0.054 10.670 10.811 1.00
HEAD 10.803 ± 0.097 10.712 10.977 1.01 ± 0.01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Polish Refactorings, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants