-
Notifications
You must be signed in to change notification settings - Fork 435
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
contrib/olivere/elastic: fix ineffectual assignment #1010
Conversation
`snip, err = ioutil.ReadAll(gzr)` was ineffectual assignment.
gzr, err := gzip.NewReader(bytes.NewReader(snip)) | ||
if err != nil { | ||
gzr, err2 := gzip.NewReader(bytes.NewReader(snip)) | ||
if err2 != nil { |
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.
@mrkm4ntr maybe I haven't had enough coffee yet, but I don't understand why the previous assignment would have been "ineffectual"? See https://play.golang.org/p/hUfSbzBrIoG
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.
Actual ineffectual assignment is here.
snip, err = ioutil.ReadAll(gzr) |
The scope of err
is in this block. So I changed the name of err
in this scope to err2
.
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.
Got it. Subtle. LGTM!
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.
I don't see how this changes the logic. Whether the variable is named err
or err2
, it is block-scoped and never used outside the block. Likewise, we're not using the outer err
inside the block, so shadowing is not an issue.
If there is a bug here, please add a test that exposes the bug which is fixed by this change.
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.
Again, the problematic part is here.
dd-trace-go/contrib/olivere/elastic/elastictrace.go
Lines 143 to 145 in f86a82b
snip, err = ioutil.ReadAll(gzr) | |
} | |
return string(snip), rc2, err |
if
ioutil.ReadAll(gzr)
returns error, that error is assigned to scoped err
not err
that function returns. So this function cannot return error of ioutil.ReadAll(gzr)
.
The test is still required...? I think it is preferred to introduce linters to detect these kind of bugs automatically.
https://github.com/gordonklaus/ineffassign
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.
@mrkm4ntr Thank you for the clarification. I did not look closely enough my first time through.
A test case is always welcome, but I think we can merge this as-is.
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.
LGTM. Thanks for the fix!
gzr, err := gzip.NewReader(bytes.NewReader(snip)) | ||
if err != nil { | ||
gzr, err2 := gzip.NewReader(bytes.NewReader(snip)) | ||
if err2 != nil { |
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.
Got it. Subtle. LGTM!
gzr, err := gzip.NewReader(bytes.NewReader(snip)) | ||
if err != nil { | ||
gzr, err2 := gzip.NewReader(bytes.NewReader(snip)) | ||
if err2 != nil { |
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.
I don't see how this changes the logic. Whether the variable is named err
or err2
, it is block-scoped and never used outside the block. Likewise, we're not using the outer err
inside the block, so shadowing is not an issue.
If there is a bug here, please add a test that exposes the bug which is fixed by this change.
gzr, err := gzip.NewReader(bytes.NewReader(snip)) | ||
if err != nil { | ||
gzr, err2 := gzip.NewReader(bytes.NewReader(snip)) | ||
if err2 != nil { |
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.
@mrkm4ntr Thank you for the clarification. I did not look closely enough my first time through.
A test case is always welcome, but I think we can merge this as-is.
snip, err = ioutil.ReadAll(gzr)
was ineffectual assignment.