-
Notifications
You must be signed in to change notification settings - Fork 2
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
False Positive #34
Comments
Hello! |
@dprotaso Could you please confirm which version of |
our CI was using golangci-lint-1.62.2-linux-amd64 |
Hm interestingly this doesn't trigger: func something() func() {
return func() {
r := struct {
Ctx context.Context
}{}
ctx := r.Ctx
ctx = context.WithValue(ctx, "key", "val")
r.Ctx = ctx
}
} But this does: type R struct {
Ctx context.Context
}
func something() func(*R) {
return func(r *R) {
ctx := r.Ctx
ctx = context.WithValue(ctx, "key", "val")
r.Ctx = ctx // triggered on this line
}
} This is indeed a false positive IMO, an extended case of this test: fatcontext/testdata/src/example.go Lines 221 to 226 in 52f7fb5
We have a test case, now we need a fix :) Do you want to work on this? |
On second thought I’ll have to experiment a bit more to check whether that’s really a false positive. I’ll post back here once I’m done. |
I guess if the function was called twice then you'd add the same values into the values linked list. I don't believe It seems like a gray area in my mind. |
I'm ok with leaving this as is and I can add a nolint directive in my code |
There is indeed no deduplication, that’s the whole point of this linter 🙂 |
I guess though if the following isn't flagged then I wouldn't expect our code to trigger it func Blah(r *R) {
ctx := r.Ctx
ctx = context.WithValue(ctx, "key", "val")
r.Ctx = ctx // triggered on this line
} |
A similar false positive is here for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.ctx == nil {
tc.ctx = context.Background()
}
err := ValidateContainerConcurrency(tc.ctx, tc.containerConcurrency)
if got, want := err.Error(), tc.expectErr.Error(); got != want {
t.Errorf("\nGot: %q\nwant: %q", got, want)
}
})
} |
Here's the "signature" of OP's code (simplified, marked as "experiment") compared to a known "fat" and a known "thin". See this commit for reproducible results: Crocmagnon/fat-contexts-article-companion@7c777c5 So from my POV, it's not a false positive.
That's a false negative IMO, we should probably add this to the linter. I agree that the current behavior is not consistent. Now for the TODO:
Feedback welcome on both PRs |
It depends on semantics - for example this is test setup code so we wouldn't be calling it twice. It's really application specific so I don't think a linter can have an opinion. We have a lot of patterns where we have a function that decorates a context. Calling that function multiple times would make it fat but we don't expect consumers to do that ¯_(ツ)_/¯ |
Completed with #36, the linter now better detects struct pointer assignations and they're hidden behind a feature flag. Follow golangci/golangci-lint#5334 for integration in golangci-lint. |
This was just flagged recently - but oddly it wasn't flagged before (maybe because we're moving to go1.23)
https://github.com/knative/pkg/blob/4ba3f1b39dbf3df9ac80de63421aafe372cb7742/webhook/testing/factory.go#L99
I think it's a false positive because it's not in a loop
The text was updated successfully, but these errors were encountered: