| 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 | var ( |
| 59 | _ = A{B: B{b: 1}} // want "embedded field type can be removed from struct literal" |
| 60 | _ = A{a: 1, B: B{b: 1, C: C{c: 1}}} // want "embedded field type can be removed from struct literal" |
| 61 | _ = E{F: F{1}} // nope: cannot promote unkeyed fields |
| 62 | _ = D{E: E{F: F{1}}, d: 1} // want "embedded field type can be removed from struct literal" |
| 63 | _ = D{E: E{e: 2, F: F{1}}} // want "embedded field type can be removed from struct literal" |
| 64 | _ = A{a: 10, B: B{C: C{1, D{d: 1}}}} // want "embedded field type can be removed from struct literal" |
| 65 | _ = H{F: F{f: 1}} // nope: cannot promote ambiguous fields |
| 66 | _ = J{I: I{i: 1}, F: F{f: 1}, G: G{f: 1}} // want "embedded field type can be removed from struct literal" |
| 67 | // multi-line with commas |
| 68 | _ = A{ // want "embedded field type can be removed from struct literal" |
| 69 | B: B{ |
| 70 | b: 1, |
| 71 | }, |
| 72 | } |
| 73 | // empty composite lit |
| 74 | _ = A{a: 1, B: B{}} // want "embedded field type can be removed from struct literal" |
| 75 | // don't suggest a fix if it's too tricky to preserve comments |
| 76 | _ = A{ // nope: comments within range to delete |
| 77 | B: B{ // one |
| 78 | C: C{ // two |
| 79 | c: 1, // three |
| 80 | }, // four |
| 81 | }, // five |
| 82 | } |
| 83 | _ = A{ // nope: comments within range to delete |
| 84 | B: B{b: 1 /* comment, with comma */}, |
| 85 | a: 2, |
| 86 | } |
| 87 | _ = A{ // nope: comments within range to delete |
| 88 | B: B{ |
| 89 | b: 1, // comment, with comma |
| 90 | }, |
| 91 | a: 2, |
| 92 | } |
| 93 | _ = A{B: B{b: 1} /* comment */, a: 2} // want "embedded field type can be removed from struct literal" |
| 94 | _ = K{L: L{zero: 0}} // nope: cannot promote slice elements |
| 95 | ) |