Skip to content

Commit

Permalink
Stop mutation of #to_i to implicit self
Browse files Browse the repository at this point in the history
The mutations of all calls to `#to_i` include one that mutates the call into a
call to `Integer`. This is a great mutation in all calses where there is an
explicit receiver for the `#to_i` call. However, the way that the implicit call
to `self.to_i` is parsed results in a receiver of `nil` instead of `self`.

As such, we want to limit this particular mutation to calls to `#to_i` that have
an explicit receiver. This feels like the correct behavior because the result of
the mutation would be (if it parsed): `Integer(nil)`, which doesn't retain the
intended behavior of the original source.

Another way to approach this would be to mutate the code to `Integer(self)`, but
that doesn't feel as correct to me because if you're using the implicit `self`
with a call to `#to_i`, you likely are not implementing the strict `#to_int`
method as well, which `Integer` relies on.

This fix feels like the right mix of correctness and minimal invasiveness.

Closes mbj#738
  • Loading branch information
michaelherold committed Jun 17, 2018
1 parent 2beac2c commit f6b9b8b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/flay.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
threshold: 16
total_score: 1321
total_score: 1316
2 changes: 1 addition & 1 deletion lib/mutant/mutator/node/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def emit_drop_mutation
#
# @return [undefined]
def emit_integer_mutation
return unless selector.equal?(:to_i)
return unless receiver && selector.equal?(:to_i)

emit(s(:send, nil, :Integer, receiver))
end
Expand Down
7 changes: 7 additions & 0 deletions meta/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
mutation 'map'
end

Mutant::Meta::Example.add :send do
source 'to_i'

singleton_mutations
mutation 'to_int'
end

Mutant::Meta::Example.add :send do
source 'foo.to_s'

Expand Down

0 comments on commit f6b9b8b

Please sign in to comment.