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

contrib/olivere/elastic: fix ineffectual assignment #1010

Merged
merged 3 commits into from
Sep 21, 2021

Conversation

mrkm4ntr
Copy link
Contributor

snip, err = ioutil.ReadAll(gzr) was ineffectual assignment.

`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 {
Copy link
Member

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

Copy link
Contributor Author

@mrkm4ntr mrkm4ntr Sep 16, 2021

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.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Subtle. LGTM!

Copy link
Contributor

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.

Copy link
Contributor Author

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.

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

Copy link
Contributor

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.

Copy link
Member

@felixge felixge left a 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 {
Copy link
Member

Choose a reason for hiding this comment

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

Got it. Subtle. LGTM!

@felixge felixge added this to the 1.34.0 milestone Sep 16, 2021
gzr, err := gzip.NewReader(bytes.NewReader(snip))
if err != nil {
gzr, err2 := gzip.NewReader(bytes.NewReader(snip))
if err2 != nil {
Copy link
Contributor

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 {
Copy link
Contributor

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.

@knusbaum knusbaum changed the title Fix ineffectual assignment contrib/olivere/elastic: fix ineffectual assignment Sep 21, 2021
@knusbaum knusbaum merged commit f3f95f7 into DataDog:v1 Sep 21, 2021
@mrkm4ntr mrkm4ntr deleted the fix-ineffectual-assignment branch September 24, 2021 02:37
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.

3 participants