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
Linter for incorrect var capture #104835
Linter for incorrect var capture #104835
Conversation
Hi @uthark. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
I'm not entirely qualified so take everything I say with a grain of salt, but the way I understand things is - exportloopref may be an over-sensitive solution to this (#98213) problem. iiuc sometimes it's reasonable to pass Same thing can be said about closures capturing the loop var and then being ran only after the loop var is already changed on the next iterations - which is what happens with Ginkgo - it "collects" all the closures created by the loop and runs them only after the loop is done, and at that point the loop var contains the final iterable value, and thus the same value is being used by all the closures that were created. But in contrast, when using the built-in go testing package - there's a similar common pattern of creating test-case closures inside a loop with the closures referencing the loop var. But with the built-in go testing package this is (mostly*) not a problem because the closures get to run before the loop iteration is over. They're not being collected like in Ginkgo. So, if I understand correctly, exportloopref would incorrectly flag those legitimate uses as linter violations. *I think the built in go testing package allows running tests in parallel, I think that may actually start to cause problems, not sure about this though |
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.
SGTM, but up to the /hack maintainers to decide.
/ok-to-test
- unused | ||
- varcheck | ||
- ineffassign | ||
- exportloopref |
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.
exportloopref
seems to be a new addition?
-E unused \ | ||
-E varcheck \ | ||
-E ineffassign | ||
golangci-lint run --config "${KUBE_ROOT}/hack/.golangci.yaml" |
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.
my take: the benefit is minimal at this point, but having a config vs flags seems better in general.
@@ -121,6 +121,7 @@ func CheckAuditList(el auditinternal.EventList, expected []AuditEvent) (missing | |||
expectations := newAuditEventTracker(expected) | |||
|
|||
for _, e := range el.Items { | |||
e := e |
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.
these "add local var in a loop" changes seem fine.
great if there aren't more.
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.
hm, we pass e by ref on line below. And this may result in bad behavior if testEventFromInternal
will run any goroutine or will keep the ref for later use.
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.
/hold
This may lead to an issue, #106371, is the change forced by linter?
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.
that issue fixed exactly this problem, we have to capture the variable or the reference will only use the last value, or am I getting wrong your concern?
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.
Oh, I read the PR backwards. Adding it makes a lot of sense. Sorry for the noise
/release-note-none |
@@ -0,0 +1,12 @@ | |||
--- |
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.
maybe we should put a comment at the top pointing to the upstream docs for this config format?
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.
this got split out in another PR working on some other class of lints, upon rebase the change in this file should just be adding the new linter 😅
/approve |
/triage accepted |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: BenTheElder, uthark The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
xref another golangci PR that will have merge conflicts #103723 |
@uthark: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
there are a few more issues that need to be fixed
|
@uthark: PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
this PR has been on hold for about 2 weeks. Can you please see how to unblock this by reviewing comments etc? or please close this out (Trying to declutter our open list of PRs) |
This PR is older than 4 weeks and needs as rebase. Please rebase the PR against latest master and reopen if still needed. /close |
@dims: Closed this PR. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
What type of PR is this?
/kind bug
What this PR does / why we need it:
See #98213
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Just enabling linter and fixing few flakes.
Thinking of a better way to introduce an allowlist for existing violations. Seems like golangci doesn't support it (See upstream issue golangci/golangci-lint#913)
Does this PR introduce a user-facing change?
No.
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: