-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathremove.go
More file actions
65 lines (53 loc) · 1.19 KB
/
remove.go
File metadata and controls
65 lines (53 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package statement
import (
"github.com/avito-tech/go-mutesting/internal/processor/annotation"
"go/ast"
"go/token"
"go/types"
"github.com/avito-tech/go-mutesting/astutil"
"github.com/avito-tech/go-mutesting/mutator"
)
func init() {
mutator.Register("statement/remove", MutatorRemoveStatement)
}
func checkRemoveStatement(node ast.Stmt) bool {
skip := annotation.HandleBlockStmt(node)
if skip {
return false
}
switch n := node.(type) {
case *ast.AssignStmt:
if n.Tok != token.DEFINE {
return true
}
case *ast.ExprStmt, *ast.IncDecStmt:
return true
}
return false
}
// MutatorRemoveStatement implements a mutator to remove statements.
func MutatorRemoveStatement(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation {
var l []ast.Stmt
switch n := node.(type) {
case *ast.BlockStmt:
l = n.List
case *ast.CaseClause:
l = n.Body
}
var mutations []mutator.Mutation
for i, ni := range l {
if checkRemoveStatement(ni) {
li := i
old := l[li]
mutations = append(mutations, mutator.Mutation{
Change: func() {
l[li] = astutil.CreateNoopOfStatement(pkg, info, old)
},
Reset: func() {
l[li] = old
},
})
}
}
return mutations
}