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

test/integration: defer close after successful open #11865

Merged
merged 1 commit into from
Jan 13, 2023
Merged

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Jan 13, 2023

The pattern,

f, err := os.Open(..)
defer f.Close()
if err != nil { return }

Isn't quite right because the contract
for functions that return err, unless otherwise specified,
is that if err != nil, then the other results are invalid.

So if os.Open fails, the f should be considered invalid,
and we should not defer close on it because that could panic.
os.File.Close guards against this,
but it's still preferable to defer the close after the error check.

f, err := os.Open(..)
if err != nil { return }
defer f.Close()

Issue caught by staticcheck:

integration/appdash_test.go:17:2: SA5001: should check returned error before deferring traceFile.Close() (staticcheck)

Refs #11808

The pattern,

    f, err := os.Open(..)
    defer f.Close()
    if err != nil { return }

Isn't quite right because the contract
for functions that return err, unless otherwise specified,
is that if `err != nil`, then the other results are invalid.

So if `os.Open` fails, the `f` should be considered invalid,
and we should not defer close on it because that could panic.
[`os.File.Close` guards against this][1],
but it's still preferable to defer the close after the error check.

    f, err := os.Open(..)
    if err != nil { return }
    defer f.Close()

  [1]: https://cs.opensource.google/go/go/+/refs/tags/go1.19.5:src/os/file_posix.go;l=21

Issue caught by staticcheck:

```
integration/appdash_test.go:17:2: SA5001: should check returned error before deferring traceFile.Close() (staticcheck)
```

Refs #11808
@abhinav abhinav added the impact/no-changelog-required This issue doesn't require a CHANGELOG update label Jan 13, 2023
@pulumi-bot
Copy link
Contributor

Changelog

[uncommitted] (2023-01-13)

@abhinav abhinav added impact/no-changelog-required This issue doesn't require a CHANGELOG update and removed impact/no-changelog-required This issue doesn't require a CHANGELOG update labels Jan 13, 2023
@abhinav
Copy link
Contributor Author

abhinav commented Jan 13, 2023

bors merge

abhinav added a commit that referenced this pull request Jan 13, 2023
Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.

This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.

Notably, we're opting out of:

- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)

Besides that, other issues addressed in this change are:

```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```

Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, 11867, #11868

Resolves #11808
abhinav added a commit that referenced this pull request Jan 13, 2023
Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.

This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.

Notably, we're opting out of:

- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)

Besides that, other issues addressed in this change are:

```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```

Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, #11867, #11868

Resolves #11808
@bors
Copy link
Contributor

bors bot commented Jan 13, 2023

Build succeeded:

@bors bors bot merged commit 99fd24f into master Jan 13, 2023
@bors bors bot deleted the abhinav/defer-close branch January 13, 2023 22:33
abhinav added a commit that referenced this pull request Jan 14, 2023
Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.

This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.

Notably, we're opting out of:

- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)

Besides that, other issues addressed in this change are:

```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```

Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, #11867, #11868

Resolves #11808
abhinav added a commit that referenced this pull request Jan 15, 2023
Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.

This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.

Notably, we're opting out of:

- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)

Besides that, other issues addressed in this change are:

```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```

Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, #11867, #11868

Resolves #11808
bors bot added a commit that referenced this pull request Jan 15, 2023
11871: golangci-lint: Enable staticcheck r=RobbieMcKinstry a=abhinav

Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.

This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.

Notably, we're opting out of:

- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)

Besides that, other issues addressed in this change are:

```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```

Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, #11867, #11868

Resolves #11808

---

**NOTE**: This PR's base branch is currently #11868.
The base branch will be updated when that lands.

Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/no-changelog-required This issue doesn't require a CHANGELOG update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants