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
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: