| 1 |
//go:build go1.27 |
| 2 |
|
| 3 |
package embedlit |
| 4 |
|
| 5 |
type A struct { |
| 6 |
a int |
| 7 |
B |
| 8 |
} |
| 9 |
|
| 10 |
type B struct { |
| 11 |
b int |
| 12 |
C |
| 13 |
} |
| 14 |
|
| 15 |
type C struct { |
| 16 |
c int |
| 17 |
D |
| 18 |
} |
| 19 |
|
| 20 |
type D struct { |
| 21 |
d int |
| 22 |
E |
| 23 |
} |
| 24 |
|
| 25 |
type E struct { |
| 26 |
e int |
| 27 |
F |
| 28 |
} |
| 29 |
|
| 30 |
type F struct { |
| 31 |
f int |
| 32 |
} |
| 33 |
|
| 34 |
type G struct { |
| 35 |
f int |
| 36 |
} |
| 37 |
|
| 38 |
type H struct { |
| 39 |
F |
| 40 |
G |
| 41 |
} |
| 42 |
|
| 43 |
type I struct { |
| 44 |
i int |
| 45 |
} |
| 46 |
|
| 47 |
type J struct { |
| 48 |
F |
| 49 |
G |
| 50 |
I |
| 51 |
} |
| 52 |
|
| 53 |
const zero = 0 |
| 54 |
|
| 55 |
type K struct{ L } |
| 56 |
type L []int |
| 57 |
|
| 58 |
type T struct { |
| 59 |
a int |
| 60 |
b int |
| 61 |
U |
| 62 |
} |
| 63 |
|
| 64 |
type U struct { |
| 65 |
x int |
| 66 |
} |
| 67 |
|
| 68 |
var ( |
| 69 |
_ = A{b: 1} // want "embedded field type can be removed from struct literal" |
| 70 |
_ = A{a: 1, b: 1, c: 1} // want "embedded field type can be removed from struct literal" |
| 71 |
_ = E{F: F{1}} // nope: cannot promote unkeyed fields |
| 72 |
_ = D{F: F{1}, d: 1} // want "embedded field type can be removed from struct literal" |
| 73 |
_ = D{e: 2, F: F{1}} // want "embedded field type can be removed from struct literal" |
| 74 |
_ = A{a: 10, C: C{1, D{d: 1}}} // want "embedded field type can be removed from struct literal" |
| 75 |
_ = H{F: F{f: 1}} // nope: cannot promote ambiguous fields |
| 76 |
_ = J{i: 1, F: F{f: 1}, G: G{f: 1}} // want "embedded field type can be removed from struct literal" |
| 77 |
// multi-line with commas |
| 78 |
_ = A{ // want "embedded field type can be removed from struct literal" |
| 79 |
b: 1, |
| 80 |
} |
| 81 |
|
| 82 |
_ = A{a: 1, B: B{}} // nope: empty composite lit |
| 83 |
|
| 84 |
// don't suggest a fix if it's too tricky to preserve comments |
| 85 |
_ = A{ // nope: comments within range to delete |
| 86 |
B: B{ // one |
| 87 |
C: C{ // two |
| 88 |
c: 1, // three |
| 89 |
}, // four |
| 90 |
}, // five |
| 91 |
} |
| 92 |
_ = A{ // nope: comments within range to delete |
| 93 |
B: B{b: 1 /* comment, with comma */}, |
| 94 |
a: 2, |
| 95 |
} |
| 96 |
_ = A{ // nope: comments within range to delete |
| 97 |
B: B{ |
| 98 |
b: 1, // comment, with comma |
| 99 |
}, |
| 100 |
a: 2, |
| 101 |
} |
| 102 |
_ = A{B: /* comment */ B{b: 1}} // nope: comment in range to delete |
| 103 |
_ = A{b: 1 /* comment */, a: 2} // want "embedded field type can be removed from struct literal" |
| 104 |
_ = K{L: L{zero: 0}} // nope: cannot promote slice elements |
| 105 |
_ = K{L: L{0: 100}} // nope: cannot promote slice elements |
| 106 |
|
| 107 |
_ = A{ // want "embedded field type can be removed from struct literal" |
| 108 |
c: 1, |
| 109 |
b: 2, |
| 110 |
a: 3, |
| 111 |
} |
| 112 |
|
| 113 |
_ = A{c: 1} // want "embedded field type can be removed from struct literal" |
| 114 |
|
| 115 |
_ = A{ // want "embedded field type can be removed from struct literal" |
| 116 |
c: 1} |
| 117 |
|
| 118 |
_ = A{ // want "embedded field type can be removed from struct literal" |
| 119 |
c: 1, |
| 120 |
} |
| 121 |
|
| 122 |
_ = A{ // want "embedded field type can be removed from struct literal" |
| 123 |
// comment here |
| 124 |
c: 1, |
| 125 |
} |
| 126 |
) |
| 127 |
|
| 128 |
func _() { |
| 129 |
t1 := A{a: 1, // want "embedded field assignment can be moved to struct literal" |
| 130 |
b: 2} |
| 131 |
|
| 132 |
var t2 A |
| 133 |
t2 = A{a: 1, // want "embedded field assignment can be moved to struct literal" |
| 134 |
b: 2} |
| 135 |
|
| 136 |
var t3 = A{a: 1, // want "embedded field assignment can be moved to struct literal" |
| 137 |
b: 2} |
| 138 |
|
| 139 |
t4 := T{1, 2, U{x: 3}} // nope: can't mix keyed and unkeyed elements |
| 140 |
t4.x = 4 |
| 141 |
|
| 142 |
t5 := A{a: 1} |
| 143 |
_ = t5 // nope: intervening statement |
| 144 |
t5.b = 2 |
| 145 |
|
| 146 |
t6 := A{a: 1} // nope: value assigned depends on t6 itself |
| 147 |
t6.b = t6.a + 1 |
| 148 |
|
| 149 |
t7 := A{a: 1, // want "embedded field assignment can be moved to struct literal" |
| 150 |
b: foo()} |
| 151 |
|
| 152 |
// Only apply edits from pattern A first even though both patterns apply. |
| 153 |
t8 := A{ // want "embedded field type can be removed from struct literal" |
| 154 |
b: 1, |
| 155 |
} |
| 156 |
t8.a = 2 |
| 157 |
|
| 158 |
t9 := A{} // nope: multiple assignments not yet supported |
| 159 |
t9.a, t9.b = 1, 2 |
| 160 |
|
| 161 |
t10 := A{ // want "embedded field assignment can be moved to struct literal" |
| 162 |
a: 1, // this comment is preserved |
| 163 |
b: 2} // this one too |
| 164 |
|
| 165 |
t11 := A{a: 1, // want "embedded field assignment can be moved to struct literal" |
| 166 |
b: 2, |
| 167 |
c: 3} |
| 168 |
|
| 169 |
t12 := A{ // want "embedded field assignment can be moved to struct literal" |
| 170 |
B: B{ |
| 171 |
b: 1, // this comment is preserved |
| 172 |
}} |
| 173 |
|
| 174 |
t13 := A{ // want "embedded field assignment can be moved to struct literal" |
| 175 |
a: 1, |
| 176 |
// comment between assignments |
| 177 |
b: 2} |
| 178 |
|
| 179 |
t14 := A{ // want "embedded field assignment can be moved to struct literal" |
| 180 |
a: 1, |
| 181 |
b: foo() + |
| 182 |
1} |
| 183 |
|
| 184 |
t15 := A{a: 1} // nope: += in field assignment |
| 185 |
t15.b += 2 |
| 186 |
|
| 187 |
t16 := A{ // want "embedded field assignment can be moved to struct literal" |
| 188 |
a: 1, // comment, with a comma |
| 189 |
|
| 190 |
b: 2} |
| 191 |
} |
| 192 |
|
| 193 |
func foo() int { |
| 194 |
return 0 |
| 195 |
} |
| 196 |
|