Skip to content

Commit 35b2cdb

Browse files
committed
command: convert recursions to loops where appropriate
1 parent f4e57a5 commit 35b2cdb

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

command.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,29 @@ type Env struct {
6666
// context, it returns the context of its parent, or if e has no parent it
6767
// returns a new background context.
6868
func (e *Env) Context() context.Context {
69-
if e.ctx != nil {
70-
return e.ctx
71-
} else if e.Parent == nil {
72-
return context.Background()
69+
for cur := e; cur != nil; cur = cur.Parent {
70+
if cur.ctx != nil {
71+
return cur.ctx
72+
}
7373
}
74-
return e.Parent.Context()
74+
return context.Background()
7575
}
7676

7777
// Cancel cancels the context associated with e with the given cause.
7878
// If e does not have its own context, the cancellation is propagated to its
7979
// parent if one exists. If e has no parent and no context, Cancel does nothing
8080
// without error.
8181
func (e *Env) Cancel(cause error) {
82-
if e.cancel != nil {
83-
e.cancel(cause)
84-
} else if e.Parent != nil {
85-
e.Parent.Cancel(cause)
82+
for cur := e; cur != nil; cur = cur.Parent {
83+
if cur.cancel != nil {
84+
cur.cancel(cause)
85+
return
86+
}
8687
}
8788
}
8889

8990
// SetContext sets the context of e to ctx and returns e. If ctx == nil it
90-
// clears the context of e so that it defaults to its parent (see Context).
91+
// clears the context of e so that it defaults to its parent (see [Env.Context]).
9192
func (e *Env) SetContext(ctx context.Context) *Env {
9293
if ctx == nil {
9394
e.ctx = nil

0 commit comments

Comments
 (0)