Skip to content
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

chore: explicit revive rules #8290

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mmorel-35
Copy link
Contributor

@mmorel-35 mmorel-35 commented Jan 24, 2025

Description

This list all default rules and a bit more.
It keeps disabled the rules that are not yet applied

@mmorel-35 mmorel-35 force-pushed the golangci-lint/revive branch 4 times, most recently from 0e5f797 to 40f4c06 Compare January 24, 2025 13:07
@mmorel-35 mmorel-35 marked this pull request as ready for review January 24, 2025 13:33
Copy link
Collaborator

@knqyf263 knqyf263 left a comment

Choose a reason for hiding this comment

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

While it looks good to me, I cannot accept it alone since it affects the whole project. I like this change as I prefer less nesting, but this depends on the developer's preference. What do you think? @aquasecurity/trivy

One thing to note is that early return may not be equivalent to if-else. For example, in the following case, pkg is scoped in the if-else statement.

if pkg, ok := pkgs[vuln.PkgID+vuln.PkgPath]; !ok {
	continue
} else {
	r.Results[i].Vulnerabilities[j].PkgIdentifier = pkg.Identifier
}

However, pkg will be alive outside the if scope in this case.

pkg, ok := pkgs[vuln.PkgID+vuln.PkgPath]
if !ok {
	continue
}
r.Results[i].Vulnerabilities[j].PkgIdentifier = pkg.Identifier

As far as I could review, none of the changes seemed to be affected by this change in scope, but in some cases, it may need to disable the linter with nolint. Just sharing.

@mmorel-35
Copy link
Contributor Author

There is a preserveScope argument to normally adress your concern except if I'm missing the point

Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 894 to 897
data, err := structpb.NewValue(res.Data)
if err != nil {

} else {
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 related to the linter
But it looks like there's a bug here and there should be a warning, like in other cases. For example:

trivy/pkg/rpc/convert.go

Lines 111 to 113 in 40f4c06

if err != nil {
log.Warn("Custom resource conversion error", log.Err(err))
}

maybe we can fix this right away?
cc. @knqyf263

Copy link
Collaborator

Choose a reason for hiding this comment

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

I also noticed and accepted the change as the function doesn't return an error. But yes, you're right. I forgot about the idea of showing warnings. We should warn it.

Comment on lines 44 to 45
if ver, err := sh.Output("git", "describe", "--tags", "--always"); err != nil {
ver, err := sh.Output("git", "describe", "--tags", "--always")
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I actually prefer inlining if conditions to reduce nesting and lines of code, however this is subjective. But as @knqyf263 pointed out, (not this particular instance) it may lead to subtle errors. And as you mentioned, to suppress them either we'd have to add preserveScope argument or /nolint.

Based on that is it still worth to add this rule?

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer not to reuse the variable name inside if-else (for me it is more readable code)
e.g.:

pkg := Package{name: "foo"}

if p, ok := pkgs["bar"]; ok{ // for this case i don't use `pkg`. I will use `p`, `foundPkg`, etc.
...
}

so in my case these changes will not lead to consequences.
but this is just my opinion

@mmorel-35 mmorel-35 marked this pull request as draft January 29, 2025 20:15
@mmorel-35 mmorel-35 force-pushed the golangci-lint/revive branch from 40f4c06 to 58b5b7a Compare January 29, 2025 20:19
@mmorel-35
Copy link
Contributor Author

mmorel-35 commented Jan 29, 2025

Let’s narrow this to list rules and disable those not already applied. so each rule can be discussed and enabled one at a time.

Signed-off-by: Matthieu MOREL <[email protected]>
@mmorel-35 mmorel-35 force-pushed the golangci-lint/revive branch from 58b5b7a to 26048b0 Compare January 29, 2025 20:29
@mmorel-35 mmorel-35 marked this pull request as ready for review January 29, 2025 21:02
@knqyf263
Copy link
Collaborator

There is a preserveScope argument to normally adress your concern except if I'm missing the point

It's nice! Thanks for sharing.
Actually, I was wondering if the linter already takes scope into account since no scope change had an adverse effect despite the numerous changes in your PR.

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.

4 participants