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

fix(cmd): Handle imbalanced LHS and RHS in defer assignment #100

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Fixed
- cmd/errtrace: Don't exit with a non-zero status when `-h` is used.
- cmd/errtrace: Don't panic on imbalanced assignments inside defer blocks.

## v0.3.0 - 2023-12-22

Expand Down
31 changes: 27 additions & 4 deletions cmd/errtrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,6 @@ func (t *walker) deferStmt(n *ast.DeferStmt) {
if !ok {
return true
}

for i, lhs := range assign.Lhs {
ident, ok := lhs.(*ast.Ident)
if !ok {
Expand All @@ -920,9 +919,33 @@ func (t *walker) deferStmt(n *ast.DeferStmt) {
continue // not an error assignment
}

// Assigning to a named error return value.
// Wrap the rhs in-place.
t.wrapExpr(1, assign.Rhs[i])
// Assignment to an error return value.
// This will take one of the following forms:
//
// (1) x, y, err = f1(), f2(), f3()
// (2) x, y, err = f() // returns multiple values
// (3) x, err, z = f() // returns multiple values
//
// For (1), we can wrap just the function
// that returns the error. (f3 in this case)
//
// For (2), we can use a WrapN function
// to wrap the entire function call.
//
// For (3), we could use an inline function call,
// but that's not implemented yet.

if i < len(assign.Rhs) && len(assign.Lhs) == len(assign.Rhs) {
// Case (1):
// Wrap the function that returns the error.
t.wrapExpr(1, assign.Rhs[i])
} else if i == len(assign.Lhs)-1 && len(assign.Rhs) == 1 {
// Case (2):
// Wrap the entire function call.
t.wrapExpr(len(assign.Lhs), assign.Rhs[0])
} else {
t.logf(assign.Pos(), "skipping assignment: error is not the last return value")
}
}

return true
Expand Down
20 changes: 20 additions & 0 deletions cmd/errtrace/testdata/golden/tuple_rhs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

func multipleValueErrAssignment() (err error) {
defer func() {
_, err = fmt.Println("Hello, World!")

// Handles too few lhs variables
err = fmt.Println("Hello, World!")

// Handles too many lhs variables
_, err, _ = fmt.Println("Hello, World!") // want:"skipping assignment: error is not the last return value"

// Handles misplaced err
err, _ = fmt.Println("Hello, World!") // want:"skipping assignment: error is not the last return value"
}()

return nil
}
20 changes: 20 additions & 0 deletions cmd/errtrace/testdata/golden/tuple_rhs.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"; import "braces.dev/errtrace"

func multipleValueErrAssignment() (err error) {
defer func() {
_, err = errtrace.Wrap2(fmt.Println("Hello, World!"))

// Handles too few lhs variables
err = errtrace.Wrap(fmt.Println("Hello, World!"))

// Handles too many lhs variables
_, err, _ = fmt.Println("Hello, World!") // want:"skipping assignment: error is not the last return value"

// Handles misplaced err
err, _ = fmt.Println("Hello, World!") // want:"skipping assignment: error is not the last return value"
}()

return nil
}
Loading