-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/gopls: extract function on if err != nil
is unidiomatic
#66289
Labels
gopls
Issues related to the Go language server, gopls.
Refactoring
Issues related to refactoring tools
Tools
This label describes issues relating to any tools in the x/tools repository.
Milestone
Comments
golopot
added
gopls
Issues related to the Go language server, gopls.
Tools
This label describes issues relating to any tools in the x/tools repository.
labels
Mar 13, 2024
Agreed. We are working on improving refactoring, and will fix this. |
Change https://go.dev/cl/627537 mentions this issue: |
Change https://go.dev/cl/627775 mentions this issue: |
gopherbot
pushed a commit
to golang/tools
that referenced
this issue
Nov 14, 2024
With multiple return statements, a slice append would overwrite the return values of earlier returns. Fix by using slices.Concat. For golang/go#66289 Change-Id: Ib23bcb9ff297aa1ce9511c7ae54e692b14facca7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/627537 Reviewed-by: Hongxiang Jiang <hxjiang@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
gopherbot
pushed a commit
to golang/tools
that referenced
this issue
Nov 14, 2024
Use field names or type names for result variables names, if available. Otherwise, use the same conventions for var naming as completion. If all else fails, use 'result' rather than 'returnValue'. For golang/go#66289 Change-Id: Ife1d5435f00d2c4930fad48e7373e987668139ef Reviewed-on: https://go-review.googlesource.com/c/tools/+/627775 Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
dennypenta
pushed a commit
to dennypenta/tools
that referenced
this issue
Dec 3, 2024
With multiple return statements, a slice append would overwrite the return values of earlier returns. Fix by using slices.Concat. For golang/go#66289 Change-Id: Ib23bcb9ff297aa1ce9511c7ae54e692b14facca7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/627537 Reviewed-by: Hongxiang Jiang <hxjiang@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
dennypenta
pushed a commit
to dennypenta/tools
that referenced
this issue
Dec 3, 2024
Use field names or type names for result variables names, if available. Otherwise, use the same conventions for var naming as completion. If all else fails, use 'result' rather than 'returnValue'. For golang/go#66289 Change-Id: Ife1d5435f00d2c4930fad48e7373e987668139ef Reviewed-on: https://go-review.googlesource.com/c/tools/+/627775 Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
gopls
Issues related to the Go language server, gopls.
Refactoring
Issues related to refactoring tools
Tools
This label describes issues relating to any tools in the x/tools repository.
gopls version
golang.org/x/tools/gopls v0.15.2
go env
.
What did you do?
Perform extract function code action on the block enclosed by
// selection start
and// selection end
What did you see happen?
What did you expect to see?
The heuristic is to special case when every return statement in selection is an error handling return.
The detailed heuristic:
Let f be the function in scope which has an error return value as the last return value. special case if every return statement is of the form
return zeros..., expr
, whereexpr
is known to be non-nil. In this case the modified call site isvalues..., err := newFunction(...); if err != nil { return zeros..., err }
, and the extracted function should add areturn values..., nil
at the end.This refactor would return the same value as before, because when it does return, the value is correct, and when it does not return, the
if err != nil
would ensure that return return statement is not executed.Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: