Skip to content

Commit f03a131

Browse files
vihangmcopybaranaut
authored andcommitted
Disallow named returns in golang
Summary: named returns are a nice bit of syntactic sugar but their usage tends to be error prone. Our current usage of named returns is rare and all over the place. So instead just disallow named returns and update existing code to not user them Test Plan: All existing tests continue to pass Reviewers: zasgar, michelle Reviewed By: zasgar, michelle Signed-off-by: Vihang Mehta <vihang@pixielabs.ai> Differential Revision: https://phab.corp.pixielabs.ai/D11538 GitOrigin-RevId: ca52515
1 parent 8f5e198 commit f03a131

21 files changed

Lines changed: 37 additions & 30 deletions

File tree

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ linters:
2525
- misspell
2626
- nakedret
2727
- nolintlint
28+
- nonamedreturns
2829
- predeclared
2930
- staticcheck
3031
- structcheck

src/cloud/api/ptproxy/request_proxyer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func newRequestProxyer(vzmgr vzmgrClient, nc *nats.Conn, debugMode bool, r Clust
126126
return p, nil
127127
}
128128

129-
func (p requestProxyer) validateRequestAndFetchCreds(ctx context.Context, debugMode bool, vzmgr vzmgrClient) (signingKey string, err error) {
129+
func (p requestProxyer) validateRequestAndFetchCreds(ctx context.Context, debugMode bool, vzmgr vzmgrClient) (string, error) {
130+
var signingKey string
130131
clusterIDProto := utils.ProtoFromUUID(p.clusterID)
131132

132133
eg := errgroup.Group{}

src/cloud/api/ptproxy/vizier_pt_proxy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func createTestState(t *testing.T) (*testState, func(t *testing.T)) {
104104
}
105105

106106
func createDialer(lis *bufconn.Listener) func(ctx context.Context, url string) (net.Conn, error) {
107-
return func(ctx context.Context, url string) (conn net.Conn, e error) {
107+
return func(ctx context.Context, url string) (net.Conn, error) {
108108
return lis.Dial()
109109
}
110110
}

src/cloud/artifact_tracker/controllers/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func TestServer_GetDownloadLink(t *testing.T) {
310310
},
311311
}
312312

313-
controllers.URLSigner = func(bucket, name string, opts *storage.SignedURLOptions) (s string, err error) {
313+
controllers.URLSigner = func(bucket, name string, opts *storage.SignedURLOptions) (string, error) {
314314
return "the-url", nil
315315
}
316316
// Only testing error cases for now because the storage API is hard to mock.

src/cloud/autocomplete/autocomplete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func parseRunCommand(parsedCmd *ebnf.ParsedCmd, cmd *Command, s Suggester, orgID
403403
}
404404

405405
// ToFormatString converts a command to a formatted string with tab indexes, such as: ${1:run} ${2: px/svc_info}
406-
func (cmd *Command) ToFormatString(action cloudpb.AutocompleteActionType, s Suggester, orgID uuid.UUID, clusterUID string) (formattedInput string, suggestions []*cloudpb.TabSuggestion) {
406+
func (cmd *Command) ToFormatString(action cloudpb.AutocompleteActionType, s Suggester, orgID uuid.UUID, clusterUID string) (string, []*cloudpb.TabSuggestion) {
407407
curTabStop, nextInvalidTabStop, invalidTabs := cmd.processTabStops()
408408

409409
// Move the cursor according to the action that was taken.
@@ -452,7 +452,7 @@ func (cmd *Command) ToFormatString(action cloudpb.AutocompleteActionType, s Sugg
452452

453453
// Construct the formatted string and tab suggestions.
454454
fStr := ""
455-
suggestions = make([]*cloudpb.TabSuggestion, len(cmd.TabStops))
455+
suggestions := make([]*cloudpb.TabSuggestion, len(cmd.TabStops))
456456
for i, t := range cmd.TabStops {
457457
// The tab index of this tabstop is ((idx - (curTabStop + 1)) % numTabStops) + 1.
458458
idx := (((i - (curTabStop + 1)) + len(cmd.TabStops)) % len(cmd.TabStops)) + 1

src/cloud/vzconn/bridge/grpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func createTestState(t *testing.T, ctrl *gomock.Controller) (*testState, func(t
115115
}
116116

117117
func createDialer(lis *bufconn.Listener) func(ctx context.Context, url string) (net.Conn, error) {
118-
return func(ctx context.Context, url string) (conn net.Conn, e error) {
118+
return func(ctx context.Context, url string) (net.Conn, error) {
119119
return lis.Dial()
120120
}
121121
}

src/pixie_cli/pkg/components/input_field.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,15 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p
351351
}
352352

353353
// MouseHandler returns the mouse handler for this primitive.
354-
func (i *InputField) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
355-
return i.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
354+
func (i *InputField) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (bool, tview.Primitive) {
355+
return i.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (bool, tview.Primitive) {
356356
x, y := event.Position()
357357
_, rectY, _, _ := i.GetInnerRect()
358358
if !i.InRect(x, y) {
359359
return false, nil
360360
}
361361

362+
consumed := false
362363
// Process mouse event.
363364
if action == tview.MouseLeftClick && y == rectY {
364365
// Determine where to place the cursor.
@@ -377,7 +378,7 @@ func (i *InputField) MouseHandler() func(action tview.MouseAction, event *tcell.
377378
consumed = true
378379
}
379380

380-
return
381+
return consumed, nil
381382
})
382383
}
383384

src/shared/services/events/track.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var once sync.Once
3939

4040
type dummyClient struct{}
4141

42-
func (d *dummyClient) Enqueue(msg analytics.Message) (err error) {
43-
if err = msg.Validate(); err != nil {
44-
return
42+
func (d *dummyClient) Enqueue(msg analytics.Message) error {
43+
if err := msg.Validate(); err != nil {
44+
return err
4545
}
4646

4747
log.WithField("msg", msg).Debug("Dummy analytics client, dropping message...")

src/shared/services/server/grpc_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func createGRPCAuthFunc(env env.Env, opts *GRPCServerOptions) func(context.Conte
107107
// CreateGRPCServer creates a GRPC server with default middleware for our services.
108108
func CreateGRPCServer(env env.Env, serverOpts *GRPCServerOptions) *grpc.Server {
109109
logrusOpts := []grpc_logrus.Option{
110-
grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
110+
grpc_logrus.WithDurationField(func(duration time.Duration) (string, interface{}) {
111111
return "time", duration
112112
}),
113113
grpc_logrus.WithLevels(grpc_logrus.DefaultClientCodeToLevel),

src/shared/services/server/grpc_server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func startTestGRPCServer(opts *server.GRPCServerOptions) (*bufconn.Listener, fun
104104
}
105105

106106
func createDialer(lis *bufconn.Listener) func(ctx context.Context, url string) (net.Conn, error) {
107-
return func(ctx context.Context, url string) (conn net.Conn, e error) {
107+
return func(ctx context.Context, url string) (net.Conn, error) {
108108
return lis.Dial()
109109
}
110110
}

0 commit comments

Comments
 (0)