-
Notifications
You must be signed in to change notification settings - Fork 8
/
rangebreak.go
48 lines (40 loc) · 927 Bytes
/
rangebreak.go
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
package rangebreak
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type RangeBreak struct{}
// New returns a new RangeBreak virus.
//
// It adds an early break to `range`s.
func New() *RangeBreak {
return &RangeBreak{}
}
func (v *RangeBreak) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
statement, matches := node.(*ast.RangeStmt)
if !matches {
return nil
}
originalStatementBody := statement.Body
mutatedStatementBody := &ast.BlockStmt{
Lbrace: 0,
List: []ast.Stmt{
&ast.BranchStmt{
TokPos: 0,
Tok: token.BREAK,
Label: nil,
},
},
Rbrace: 0,
}
mutatedStatementBody.List = append(mutatedStatementBody.List, originalStatementBody.List...)
return []*viruses.Infection{
viruses.NewInfection(
"Range Break",
func() { statement.Body = mutatedStatementBody },
func() { statement.Body = originalStatementBody },
),
}
}