github.com/mshitrit/go-mutesting@v0.0.0-20210528084812-ff81dcaedfea/mutator/statement/remove.go (about) 1 package statement 2 3 import ( 4 "go/ast" 5 "go/token" 6 "go/types" 7 8 "github.com/zimmski/go-mutesting/astutil" 9 "github.com/zimmski/go-mutesting/mutator" 10 ) 11 12 func init() { 13 mutator.Register("statement/remove", MutatorRemoveStatement) 14 } 15 16 func checkRemoveStatement(node ast.Stmt) bool { 17 switch n := node.(type) { 18 case *ast.AssignStmt: 19 if n.Tok != token.DEFINE { 20 return true 21 } 22 case *ast.ExprStmt, *ast.IncDecStmt: 23 return true 24 } 25 26 return false 27 } 28 29 // MutatorRemoveStatement implements a mutator to remove statements. 30 func MutatorRemoveStatement(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation { 31 var l []ast.Stmt 32 33 switch n := node.(type) { 34 case *ast.BlockStmt: 35 l = n.List 36 case *ast.CaseClause: 37 l = n.Body 38 } 39 40 var mutations []mutator.Mutation 41 42 for i, ni := range l { 43 if checkRemoveStatement(ni) { 44 li := i 45 old := l[li] 46 47 mutations = append(mutations, mutator.Mutation{ 48 Change: func() { 49 l[li] = astutil.CreateNoopOfStatement(pkg, info, old) 50 }, 51 Reset: func() { 52 l[li] = old 53 }, 54 }) 55 } 56 } 57 58 return mutations 59 }