|
| 1 | +package deferr |
| 2 | + |
| 3 | +func x() { |
| 4 | + var ( |
| 5 | + t t1 |
| 6 | + tt t2 |
| 7 | + varReturnNothing = func() {} |
| 8 | + varReturnInt = func() int { return 0 } |
| 9 | + varReturnFunc = func() func() { return func() {} } |
| 10 | + varReturnFuncInt = func(int) func(int) int { return func(int) int { return 0 } } |
| 11 | + varReturnMulti = func() (int, func()) { return 0, func() {} } |
| 12 | + |
| 13 | + namedReturnNothing = named(func() {}) |
| 14 | + namedReturnFunc = namedReturn(func() func() { return func() {} }) |
| 15 | + ) |
| 16 | + |
| 17 | + // Correct. |
| 18 | + defer returnNothing() |
| 19 | + defer varReturnNothing() |
| 20 | + defer namedReturnNothing() |
| 21 | + defer t.returnNothing() |
| 22 | + defer tt.returnNothing() |
| 23 | + defer tt.t.returnNothing() |
| 24 | + defer func() {}() |
| 25 | + defer close(make(chan int)) |
| 26 | + |
| 27 | + defer returnInt() |
| 28 | + defer varReturnInt() |
| 29 | + defer t.returnInt() |
| 30 | + defer tt.returnInt() |
| 31 | + defer tt.t.returnInt() |
| 32 | + defer func() int { return 0 }() |
| 33 | + |
| 34 | + defer returnFunc()() |
| 35 | + defer varReturnFunc()() |
| 36 | + defer namedReturnFunc()() |
| 37 | + defer t.returnFunc()() |
| 38 | + defer tt.returnFunc()() |
| 39 | + defer tt.t.returnFunc()() |
| 40 | + defer func() func() { return func() {} }()() |
| 41 | + |
| 42 | + defer returnFuncInt(0)(0) |
| 43 | + defer varReturnFuncInt(0)(0) |
| 44 | + defer t.returnFuncInt(0)(0) |
| 45 | + defer tt.returnFuncInt(0)(0) |
| 46 | + defer tt.t.returnFuncInt(0)(0) |
| 47 | + defer func(int) func(int) int { return func(int) int { return 0 } }(0)(0) |
| 48 | + |
| 49 | + defer returnMulti() |
| 50 | + defer varReturnMulti() |
| 51 | + defer t.returnMulti() |
| 52 | + defer tt.returnMulti() |
| 53 | + defer tt.t.returnMulti() |
| 54 | + defer func() (int, func()) { return 0, func() {} }() |
| 55 | + |
| 56 | + // Wrong. |
| 57 | + defer returnFunc() //@ diag(`defered return function not called`) |
| 58 | + defer varReturnFunc() //@ diag(`defered return function not called`) |
| 59 | + defer namedReturnFunc() //@ diag(`defered return function not called`) |
| 60 | + defer t.returnFunc() //@ diag(`defered return function not called`) |
| 61 | + defer tt.returnFunc() //@ diag(`defered return function not called`) |
| 62 | + defer tt.t.returnFunc() //@ diag(`defered return function not called`) |
| 63 | + defer func() func() { return func() {} }() //@ diag(`defered return function not called`) |
| 64 | + defer returnFuncInt(0) //@ diag(`defered return function not called`) |
| 65 | + defer t.returnFuncInt(0) //@ diag(`defered return function not called`) |
| 66 | + defer tt.returnFuncInt(0) //@ diag(`defered return function not called`) |
| 67 | + defer tt.t.returnFuncInt(0) //@ diag(`defered return function not called`) |
| 68 | + defer func(int) func(int) int { return func(int) int { return 0 } }(0) //@ diag(`defered return function not called`) |
| 69 | + |
| 70 | + // Function returns a function which returns another function. This is |
| 71 | + // getting silly and is not checked. |
| 72 | + defer silly1()() |
| 73 | + defer func() func() func() { |
| 74 | + return func() func() { |
| 75 | + return func() {} |
| 76 | + } |
| 77 | + }()() |
| 78 | +} |
| 79 | + |
| 80 | +func returnNothing() {} |
| 81 | +func returnInt() int { return 0 } |
| 82 | +func returnFunc() func() { return func() {} } |
| 83 | +func returnFuncInt(int) func(int) int { return func(int) int { return 0 } } |
| 84 | +func returnMulti() (int, func()) { return 0, func() {} } |
| 85 | + |
| 86 | +type ( |
| 87 | + t1 struct{} |
| 88 | + t2 struct{ t t1 } |
| 89 | + named func() |
| 90 | + namedReturn func() func() |
| 91 | +) |
| 92 | + |
| 93 | +func (t1) returnNothing() {} |
| 94 | +func (t1) returnInt() int { return 0 } |
| 95 | +func (t1) returnFunc() func() { return func() {} } |
| 96 | +func (t1) returnFuncInt(int) func(int) int { return func(int) int { return 0 } } |
| 97 | +func (t1) returnMulti() (int, func()) { return 0, func() {} } |
| 98 | + |
| 99 | +func (*t2) returnNothing() {} |
| 100 | +func (*t2) returnInt() int { return 0 } |
| 101 | +func (*t2) returnFunc() func() { return func() {} } |
| 102 | +func (*t2) returnFuncInt(int) func(int) int { return func(int) int { return 0 } } |
| 103 | +func (*t2) returnMulti() (int, func()) { return 0, func() {} } |
| 104 | + |
| 105 | +func silly1() func() func() { |
| 106 | + return func() func() { |
| 107 | + return func() {} |
| 108 | + } |
| 109 | +} |
0 commit comments