Skip to content

Commit f4f670f

Browse files
committed
fix: filter non-arithmetic operators in ArithmeticMutator.CanMutate
ArithmeticMutator.CanMutate returned true for all BinaryExpr nodes including conditional (==, !=) and logical (&&, ||) operators. While getArithmeticMutations returned nil for these, the lack of filtering caused unnecessary processing and could lead to bugs when adding new mutators. Add isArithmeticOp and isArithmeticAssignOp helper methods to properly filter only arithmetic operators, consistent with how ConditionalMutator and LogicalMutator handle their own operators.
1 parent 37ac995 commit f4f670f

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

internal/mutation/arithmetic.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,36 @@ func (m *ArithmeticMutator) Name() string {
2525

2626
// CanMutate returns true if the node can be mutated by this mutator.
2727
func (m *ArithmeticMutator) CanMutate(node ast.Node) bool {
28-
switch node.(type) {
28+
switch n := node.(type) {
2929
case *ast.BinaryExpr:
30-
return true
30+
return m.isArithmeticOp(n.Op)
3131
case *ast.AssignStmt:
32-
return true
32+
return m.isArithmeticAssignOp(n.Tok)
3333
case *ast.IncDecStmt:
3434
return true
3535
}
3636

3737
return false
3838
}
3939

40+
func (m *ArithmeticMutator) isArithmeticOp(op token.Token) bool {
41+
switch op {
42+
case token.ADD, token.SUB, token.MUL, token.QUO, token.REM:
43+
return true
44+
default:
45+
return false
46+
}
47+
}
48+
49+
func (m *ArithmeticMutator) isArithmeticAssignOp(op token.Token) bool {
50+
switch op {
51+
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN:
52+
return true
53+
default:
54+
return false
55+
}
56+
}
57+
4058
// Mutate generates mutants for the given node.
4159
func (m *ArithmeticMutator) Mutate(node ast.Node, fset *token.FileSet) []Mutant {
4260
var mutants []Mutant

0 commit comments

Comments
 (0)