| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package fillswitch identifies switches with missing cases. |
| 6 | // |
| 7 | // It reports a diagnostic for each type switch or 'enum' switch that |
| 8 | // has missing cases, and suggests a fix to fill them in. |
| 9 | // |
| 10 | // The possible cases are: for a type switch, each accessible named |
| 11 | // type T or pointer *T that is assignable to the interface type; and |
| 12 | // for an 'enum' switch, each accessible named constant of the same |
| 13 | // type as the switch value. |
| 14 | // |
| 15 | // For an 'enum' switch, it will suggest cases for all possible values of the |
| 16 | // type. |
| 17 | // |
| 18 | // type Suit int8 |
| 19 | // const ( |
| 20 | // Spades Suit = iota |
| 21 | // Hearts |
| 22 | // Diamonds |
| 23 | // Clubs |
| 24 | // ) |
| 25 | // |
| 26 | // var s Suit |
| 27 | // switch s { |
| 28 | // case Spades: |
| 29 | // } |
| 30 | // |
| 31 | // It will report a diagnostic with a suggested fix to fill in the remaining |
| 32 | // cases: |
| 33 | // |
| 34 | // var s Suit |
| 35 | // switch s { |
| 36 | // case Spades: |
| 37 | // case Hearts: |
| 38 | // case Diamonds: |
| 39 | // case Clubs: |
| 40 | // default: |
| 41 | // panic(fmt.Sprintf("unexpected Suit: %v", s)) |
| 42 | // } |
| 43 | // |
| 44 | // For a type switch, it will suggest cases for all types that implement the |
| 45 | // interface. |
| 46 | // |
| 47 | // var stmt ast.Stmt |
| 48 | // switch stmt.(type) { |
| 49 | // case *ast.IfStmt: |
| 50 | // } |
| 51 | // |
| 52 | // It will report a diagnostic with a suggested fix to fill in the remaining |
| 53 | // cases: |
| 54 | // |
| 55 | // var stmt ast.Stmt |
| 56 | // switch stmt.(type) { |
| 57 | // case *ast.IfStmt: |
| 58 | // case *ast.ForStmt: |
| 59 | // case *ast.RangeStmt: |
| 60 | // case *ast.AssignStmt: |
| 61 | // case *ast.GoStmt: |
| 62 | // ... |
| 63 | // default: |
| 64 | // panic(fmt.Sprintf("unexpected ast.Stmt: %T", stmt)) |
| 65 | // } |
| 66 | package fillswitch |