|
| 1 | +// Fixtures for the builder-method heuristic that emits #[allow(unused_value)] |
| 2 | +// when a pointer-receiver method's body returns the receiver itself or |
| 3 | +// delegates via a method call on the receiver. |
| 4 | +package builder_methods |
| 5 | + |
| 6 | +// Self-return on pointer receiver: classic fluent setter shape. |
| 7 | +type Config struct { |
| 8 | + name string |
| 9 | + tags []string |
| 10 | +} |
| 11 | + |
| 12 | +func (c *Config) WithName(n string) *Config { c.name = n; return c } |
| 13 | +func (c *Config) WithTag(k, v string) {} // no return: not a builder |
| 14 | +func (c *Config) Name() string { return c.name } |
| 15 | + |
| 16 | +// Interface self-return on a real fluent setter (mutates state, returns self). |
| 17 | +type Router interface { |
| 18 | + Get(path string) Router |
| 19 | + Post(path string) Router |
| 20 | +} |
| 21 | + |
| 22 | +type Server struct{ routes []string } |
| 23 | + |
| 24 | +func (s *Server) Get(path string) Router { s.routes = append(s.routes, "GET "+path); return s } |
| 25 | +func (s *Server) Post(path string) Router { s.routes = append(s.routes, "POST "+path); return s } |
| 26 | + |
| 27 | +// Delegation: body returns a method call on the receiver. Common in Fiber's |
| 28 | +// `func (app *App) Get(...) Router { return app.Add(...) }` shape. |
| 29 | +type App struct{ server *Server } |
| 30 | + |
| 31 | +func (a *App) Add(method, path string) *App { |
| 32 | + a.server.routes = append(a.server.routes, method+" "+path) |
| 33 | + return a |
| 34 | +} |
| 35 | +func (a *App) GetRoute(path string) *App { return a.Add("GET", path) } |
| 36 | +func (a *App) PostRoute(path string) *App { return a.Add("POST", path) } |
| 37 | + |
| 38 | +// Pointer receiver returning a *different* concrete type — must not be marked. |
| 39 | +type Group struct{} |
| 40 | + |
| 41 | +func (a *App) NewGroup() *Group { return &Group{} } |
| 42 | + |
| 43 | +// Value receiver returning same type — must not be marked (immutable update). |
| 44 | +type Coord struct{ X, Y int } |
| 45 | + |
| 46 | +func (c Coord) Translate(dx, dy int) Coord { return Coord{c.X + dx, c.Y + dy} } |
| 47 | + |
| 48 | +// Clone/Copy return a new value — must not be marked even though the body |
| 49 | +// returns the receiver name in some path; the Clone/Copy name exclusion catches |
| 50 | +// the common case before AST inspection runs. |
| 51 | +type Cfg struct{ tags []string } |
| 52 | + |
| 53 | +func (c *Cfg) Clone() *Cfg { return &Cfg{tags: append([]string(nil), c.tags...)} } |
| 54 | +func (c *Cfg) Copy() *Cfg { return c.Clone() } |
| 55 | + |
| 56 | +// Trivial single-statement return-self getter — must not be marked. This is |
| 57 | +// the structural shape of interface-method implementations like |
| 58 | +// `func (b *BasicType) Basic() *BasicType { return b }` in go/debug/dwarf. |
| 59 | +type TypeLike interface { |
| 60 | + Underlying() TypeLike |
| 61 | + Self() TypeLike |
| 62 | +} |
| 63 | + |
| 64 | +type Box struct{} |
| 65 | + |
| 66 | +func (b *Box) Underlying() TypeLike { return b } |
| 67 | +func (b *Box) Self() TypeLike { return b } |
| 68 | + |
| 69 | +// Copy-and-modify pattern (slog.Logger.With shape) — must not be marked. |
| 70 | +// Body assigns to a local and returns the local. |
| 71 | +type Logger struct{ attrs []string } |
| 72 | + |
| 73 | +func (l *Logger) With(attr string) *Logger { |
| 74 | + clone := &Logger{attrs: append([]string(nil), l.attrs...)} |
| 75 | + clone.attrs = append(clone.attrs, attr) |
| 76 | + return clone |
| 77 | +} |
| 78 | + |
| 79 | +// Multi-path method where one return path is non-receiver (go/types Origin |
| 80 | +// shape) — must not be marked because not ALL returns are receiver-like. |
| 81 | +type Var struct{ origin *Var } |
| 82 | + |
| 83 | +func (v *Var) Origin() *Var { |
| 84 | + if v.origin != nil { |
| 85 | + return v.origin |
| 86 | + } |
| 87 | + return v |
| 88 | +} |
0 commit comments