| 1 |
This test verifies that gopls can remove unused parameters from methods. |
| 2 |
|
| 3 |
Specifically, check |
| 4 |
1. basic removal of unused parameters, when the receiver is named, locally and |
| 5 |
across package boundaries |
| 6 |
2. handling of unnamed receivers |
| 7 |
3. no panics related to references through interface satisfaction |
| 8 |
|
| 9 |
-- go.mod -- |
| 10 |
module example.com/rm |
| 11 |
|
| 12 |
go 1.20 |
| 13 |
|
| 14 |
-- basic.go -- |
| 15 |
package rm |
| 16 |
|
| 17 |
type Basic int |
| 18 |
|
| 19 |
func (t Basic) Foo(x int) { //@codeaction("x", "refactor.rewrite.removeUnusedParam", result=basic) |
| 20 |
} |
| 21 |
|
| 22 |
func _(b Basic) { |
| 23 |
b.Foo(1) |
| 24 |
// TODO(rfindley): methodexprs should not get rewritten as methods. |
| 25 |
Basic.Foo(1, 2) |
| 26 |
} |
| 27 |
|
| 28 |
-- basicuse/p.go -- |
| 29 |
package basicuse |
| 30 |
|
| 31 |
import "example.com/rm" |
| 32 |
|
| 33 |
func _() { |
| 34 |
x := new(rm.Basic) |
| 35 |
x.Foo(sideEffects()) |
| 36 |
rm.Basic.Foo(1,2) |
| 37 |
} |
| 38 |
|
| 39 |
func sideEffects() int |
| 40 |
|
| 41 |
type Fooer interface { |
| 42 |
Foo(int) |
| 43 |
} |
| 44 |
|
| 45 |
// Dynamic calls aren't rewritten. |
| 46 |
// Previously, this would cause a bug report or crash (golang/go#69896). |
| 47 |
func _(f Fooer) { |
| 48 |
f.Foo(1) |
| 49 |
} |
| 50 |
|
| 51 |
-- @basic/basic.go -- |
| 52 |
package rm |
| 53 |
|
| 54 |
type Basic int |
| 55 |
|
| 56 |
func (t Basic) Foo() { //@codeaction("x", "refactor.rewrite.removeUnusedParam", result=basic) |
| 57 |
} |
| 58 |
|
| 59 |
func _(b Basic) { |
| 60 |
b.Foo() |
| 61 |
// TODO(rfindley): methodexprs should not get rewritten as methods. |
| 62 |
Basic(1).Foo() |
| 63 |
} |
| 64 |
-- @basic/basicuse/p.go -- |
| 65 |
package basicuse |
| 66 |
|
| 67 |
import "example.com/rm" |
| 68 |
|
| 69 |
func _() { |
| 70 |
x := new(rm.Basic) |
| 71 |
x.Foo() |
| 72 |
rm.Basic(1).Foo() |
| 73 |
} |
| 74 |
|
| 75 |
func sideEffects() int |
| 76 |
|
| 77 |
type Fooer interface { |
| 78 |
Foo(int) |
| 79 |
} |
| 80 |
|
| 81 |
// Dynamic calls aren't rewritten. |
| 82 |
// Previously, this would cause a bug report or crash (golang/go#69896). |
| 83 |
func _(f Fooer) { |
| 84 |
f.Foo(1) |
| 85 |
} |
| 86 |
-- missingrecv.go -- |
| 87 |
package rm |
| 88 |
|
| 89 |
type Missing struct{} |
| 90 |
|
| 91 |
var R2 int |
| 92 |
|
| 93 |
func (Missing) M(a, b, c, r0 int) (r1 int) { //@codeaction("b", "refactor.rewrite.removeUnusedParam", result=missingrecv) |
| 94 |
return a + c |
| 95 |
} |
| 96 |
|
| 97 |
func _() { |
| 98 |
m := &Missing{} |
| 99 |
_ = m.M(1, 2, 3, 4) |
| 100 |
} |
| 101 |
|
| 102 |
-- missingrecvuse/p.go -- |
| 103 |
package missingrecvuse |
| 104 |
|
| 105 |
import "example.com/rm" |
| 106 |
|
| 107 |
func _() { |
| 108 |
x := rm.Missing{} |
| 109 |
x.M(1, sideEffects(), 3, 4) |
| 110 |
} |
| 111 |
|
| 112 |
func sideEffects() int |
| 113 |
|
| 114 |
-- @missingrecv/missingrecv.go -- |
| 115 |
package rm |
| 116 |
|
| 117 |
type Missing struct{} |
| 118 |
|
| 119 |
var R2 int |
| 120 |
|
| 121 |
func (Missing) M(a, c, r0 int) (r1 int) { //@codeaction("b", "refactor.rewrite.removeUnusedParam", result=missingrecv) |
| 122 |
return a + c |
| 123 |
} |
| 124 |
|
| 125 |
func _() { |
| 126 |
m := &Missing{} |
| 127 |
_ = m.M(1, 3, 4) |
| 128 |
} |
| 129 |
-- @missingrecv/missingrecvuse/p.go -- |
| 130 |
package missingrecvuse |
| 131 |
|
| 132 |
import "example.com/rm" |
| 133 |
|
| 134 |
func _() { |
| 135 |
x := rm.Missing{} |
| 136 |
x.M(1, 3, 4) |
| 137 |
} |
| 138 |
|
| 139 |
func sideEffects() int |