| 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: B{b: 1}} // want "embedded field type can be removed from struct literal" |
| 70 | _ = A{a: 1, B: B{b: 1, C: C{c: 1}}} // want "embedded field type can be removed from struct literal" |
| 71 | _ = E{F: F{1}} // nope: cannot promote unkeyed fields |
| 72 | _ = D{E: E{F: F{1}}, d: 1} // want "embedded field type can be removed from struct literal" |
| 73 | _ = D{E: E{e: 2, F: F{1}}} // want "embedded field type can be removed from struct literal" |
| 74 | _ = A{a: 10, B: B{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: I{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: B{ |
| 80 | b: 1, |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | _ = A{a: 1, B: B{}} // nope: empty composite lit |
| 85 | |
| 86 | // don't suggest a fix if it's too tricky to preserve comments |
| 87 | _ = A{ // nope: comments within range to delete |
| 88 | B: B{ // one |
| 89 | C: C{ // two |
| 90 | c: 1, // three |
| 91 | }, // four |
| 92 | }, // five |
| 93 | } |
| 94 | _ = A{ // nope: comments within range to delete |
| 95 | B: B{b: 1 /* comment, with comma */}, |
| 96 | a: 2, |
| 97 | } |
| 98 | _ = A{ // nope: comments within range to delete |
| 99 | B: B{ |
| 100 | b: 1, // comment, with comma |
| 101 | }, |
| 102 | a: 2, |
| 103 | } |
| 104 | _ = A{B: /* comment */ B{b: 1}} // nope: comment in range to delete |
| 105 | _ = A{B: B{b: 1} /* comment */, a: 2} // want "embedded field type can be removed from struct literal" |
| 106 | _ = K{L: L{zero: 0}} // nope: cannot promote slice elements |
| 107 | _ = K{L: L{0: 100}} // nope: cannot promote slice elements |
| 108 | |
| 109 | _ = A{ // want "embedded field type can be removed from struct literal" |
| 110 | B: B{ |
| 111 | C: C{ |
| 112 | c: 1, |
| 113 | }, |
| 114 | b: 2, |
| 115 | }, |
| 116 | a: 3, |
| 117 | } |
| 118 | |
| 119 | _ = A{B: B{C: C{c: 1}}} // want "embedded field type can be removed from struct literal" |
| 120 | |
| 121 | _ = A{B: B{ // want "embedded field type can be removed from struct literal" |
| 122 | C: C{ |
| 123 | c: 1, |
| 124 | }, |
| 125 | }} |
| 126 | |
| 127 | _ = A{ // want "embedded field type can be removed from struct literal" |
| 128 | B: B{C: C{c: 1}}, |
| 129 | } |
| 130 | |
| 131 | _ = A{ // want "embedded field type can be removed from struct literal" |
| 132 | B: B{ // comment here |
| 133 | C: C{ |
| 134 | c: 1, |
| 135 | }, |
| 136 | }, |
| 137 | } |
| 138 | ) |
| 139 | |
| 140 | func _() { |
| 141 | t1 := A{a: 1} // want "embedded field assignment can be moved to struct literal" |
| 142 | t1.b = 2 |
| 143 | |
| 144 | var t2 A |
| 145 | t2 = A{a: 1} // want "embedded field assignment can be moved to struct literal" |
| 146 | t2.b = 2 |
| 147 | |
| 148 | var t3 = A{a: 1} // want "embedded field assignment can be moved to struct literal" |
| 149 | t3.b = 2 |
| 150 | |
| 151 | t4 := T{1, 2, U{x: 3}} // nope: can't mix keyed and unkeyed elements |
| 152 | t4.x = 4 |
| 153 | |
| 154 | t5 := A{a: 1} |
| 155 | _ = t5 // nope: intervening statement |
| 156 | t5.b = 2 |
| 157 | |
| 158 | t6 := A{a: 1} // nope: value assigned depends on t6 itself |
| 159 | t6.b = t6.a + 1 |
| 160 | |
| 161 | t7 := A{a: 1} // want "embedded field assignment can be moved to struct literal" |
| 162 | t7.b = foo() |
| 163 | |
| 164 | // Only apply edits from pattern A first even though both patterns apply. |
| 165 | t8 := A{ // want "embedded field type can be removed from struct literal" |
| 166 | B: B{b: 1}, |
| 167 | } |
| 168 | t8.a = 2 |
| 169 | |
| 170 | t9 := A{} // nope: multiple assignments not yet supported |
| 171 | t9.a, t9.b = 1, 2 |
| 172 | |
| 173 | t10 := A{} // want "embedded field assignment can be moved to struct literal" |
| 174 | t10.a = 1 // this comment is preserved |
| 175 | t10.b = 2 // this one too |
| 176 | |
| 177 | t11 := A{a: 1} // want "embedded field assignment can be moved to struct literal" |
| 178 | t11.b = 2 |
| 179 | t11.c = 3 |
| 180 | |
| 181 | t12 := A{} // want "embedded field assignment can be moved to struct literal" |
| 182 | t12.B = B{ |
| 183 | b: 1, // this comment is preserved |
| 184 | } |
| 185 | |
| 186 | t13 := A{} // want "embedded field assignment can be moved to struct literal" |
| 187 | t13.a = 1 |
| 188 | // comment between assignments |
| 189 | t13.b = 2 |
| 190 | |
| 191 | t14 := A{} // want "embedded field assignment can be moved to struct literal" |
| 192 | t14.a = 1 |
| 193 | t14.b = |
| 194 | foo() + |
| 195 | 1 |
| 196 | |
| 197 | t15 := A{a: 1} // nope: += in field assignment |
| 198 | t15.b += 2 |
| 199 | |
| 200 | t16 := A{ // want "embedded field assignment can be moved to struct literal" |
| 201 | a: 1, // comment, with a comma |
| 202 | } |
| 203 | t16.b = 2 |
| 204 | } |
| 205 | |
| 206 | func foo() int { |
| 207 | return 0 |
| 208 | } |