mutineer 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +733 -0
  3. data/README.md +85 -50
  4. data/lib/mutineer/changed_lines.rb +2 -1
  5. data/lib/mutineer/child_cause.rb +149 -0
  6. data/lib/mutineer/child_stdout.rb +12 -1
  7. data/lib/mutineer/cli.rb +540 -59
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/concern_blocks.rb +95 -24
  10. data/lib/mutineer/config.rb +112 -6
  11. data/lib/mutineer/coverage_map.rb +1345 -156
  12. data/lib/mutineer/daemon_backend.rb +41 -18
  13. data/lib/mutineer/daemon_client.rb +104 -21
  14. data/lib/mutineer/daemon_server.rb +85 -14
  15. data/lib/mutineer/database_urls.rb +189 -0
  16. data/lib/mutineer/external_backend.rb +93 -6
  17. data/lib/mutineer/fs_path.rb +125 -0
  18. data/lib/mutineer/isolation.rb +380 -25
  19. data/lib/mutineer/job_plan.rb +352 -41
  20. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  21. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  22. data/lib/mutineer/minitest_integration.rb +26 -58
  23. data/lib/mutineer/mutant_id.rb +4 -1
  24. data/lib/mutineer/mutation.rb +10 -4
  25. data/lib/mutineer/mutator_registry.rb +6 -3
  26. data/lib/mutineer/mutators/collection_method.rb +195 -6
  27. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  28. data/lib/mutineer/mutators/range_literal.rb +78 -29
  29. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  30. data/lib/mutineer/mutators/return_nil.rb +23 -6
  31. data/lib/mutineer/output_filter.rb +451 -0
  32. data/lib/mutineer/pairing.rb +463 -31
  33. data/lib/mutineer/parser.rb +53 -0
  34. data/lib/mutineer/project_path.rb +15 -8
  35. data/lib/mutineer/rails_worker_db.rb +366 -6
  36. data/lib/mutineer/reach_probe.rb +229 -0
  37. data/lib/mutineer/redaction.rb +301 -41
  38. data/lib/mutineer/reload_guards.rb +985 -0
  39. data/lib/mutineer/reporter.rb +467 -58
  40. data/lib/mutineer/result.rb +6 -7
  41. data/lib/mutineer/run_seed.rb +16 -0
  42. data/lib/mutineer/runner.rb +194 -55
  43. data/lib/mutineer/source_bytes.rb +187 -0
  44. data/lib/mutineer/statement_lines.rb +108 -14
  45. data/lib/mutineer/test_runners/rspec.rb +118 -10
  46. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  47. data/lib/mutineer/test_runners.rb +3 -2
  48. data/lib/mutineer/triage.rb +55 -10
  49. data/lib/mutineer/version.rb +1 -1
  50. data/lib/mutineer.rb +1 -0
  51. data/skills/mutineer/SKILL.md +12 -4
  52. metadata +13 -1
@@ -8,6 +8,13 @@ module Mutineer
8
8
  #
9
9
  # Swaps the method-name token of a call to its semantic opposite, one
10
10
  # mutation per occurrence, exactly like the arithmetic/comparison operators.
11
+ #
12
+ # `each` and `map` are the exception. `each` returns its receiver and
13
+ # `map` returns a new array. When that value is discarded, no test can
14
+ # tell the mutant from the original, so the swap is declined. The other
15
+ # swaps stay: `all?` and `any?` stop on different elements, so a block
16
+ # can show the change even when the boolean is discarded.
17
+ # See docs/equivalent-skips.md.
11
18
  class CollectionMethod < Base
12
19
  # Method-name swaps. All targets are core-Ruby Enumerable/Array methods so
13
20
  # the mutant exercises real behaviour rather than always raising.
@@ -22,7 +29,50 @@ module Mutineer
22
29
  select: "reject", reject: "select"
23
30
  }.freeze
24
31
 
32
+ # Names whose swap changes the returned object and not the walk.
33
+ # A discarded call is declined. The check is the syntax tree only:
34
+ # it does not know the receiver's class.
35
+ UNUSED_RESULT_NAMES = %i[each map].freeze
36
+
37
+ # Parents that pass a child's value through. The child is observable
38
+ # exactly when that parent is. An assignment, an argument, a receiver,
39
+ # or an explicit `return` / `break` / `next` is not in this list: those
40
+ # parents use the value even when their own result is discarded.
41
+ # Interpolation is not in this list. A string or symbol calls `to_s`
42
+ # on the value, a backtick command runs, and a regexp is compiled,
43
+ # even when the expression's own result is thrown away. A hash
44
+ # association is not in this list either: its key is hashed on
45
+ # insertion, and its value is handled in {#result_used?}.
46
+ VALUE_FLOWS = [
47
+ Prism::ParenthesesNode,
48
+ Prism::BeginNode,
49
+ Prism::ElseNode,
50
+ Prism::AndNode,
51
+ Prism::OrNode,
52
+ Prism::RescueModifierNode,
53
+ Prism::ArrayNode,
54
+ Prism::HashNode,
55
+ Prism::KeywordHashNode,
56
+ Prism::AssocSplatNode,
57
+ Prism::SplatNode,
58
+ Prism::RangeNode,
59
+ Prism::EmbeddedStatementsNode
60
+ ].freeze
61
+
62
+ # Records parents, then walks the body. The result check needs the
63
+ # parent of each call, and Prism nodes do not store one.
64
+ #
65
+ # @param subject [Mutineer::Subject] subject whose body is visited.
66
+ # @param source [String] full source text for byte-based slicing.
67
+ # @return [Array<Mutineer::Mutation>] collected mutations.
68
+ def mutations_for(subject, source)
69
+ @parents = {}.compare_by_identity
70
+ index_parents(subject.def_node.body, nil)
71
+ super
72
+ end
73
+
25
74
  # Visits call nodes and emits collection-method mutations.
75
+ # An {UNUSED_RESULT_NAMES} call whose value is discarded is declined.
26
76
  #
27
77
  # @param node [Prism::CallNode] call node to inspect.
28
78
  # @return [void]
@@ -30,15 +80,154 @@ module Mutineer
30
80
  replacement = REPLACEMENTS[node.name]
31
81
  loc = node.message_loc
32
82
  if replacement && loc
33
- @mutations << Mutation.new(
34
- start_offset: loc.start_offset,
35
- end_offset: loc.end_offset,
36
- replacement: replacement,
37
- operator: :collection_method
38
- )
83
+ if unused_result?(node)
84
+ decline
85
+ else
86
+ @mutations << Mutation.new(
87
+ start_offset: loc.start_offset,
88
+ end_offset: loc.end_offset,
89
+ replacement: replacement,
90
+ operator: :collection_method
91
+ )
92
+ end
39
93
  end
40
94
  super
41
95
  end
96
+
97
+ private
98
+
99
+ # Stores the parent of every node under `node`.
100
+ #
101
+ # @param node [Prism::Node, nil] subtree to index.
102
+ # @param parent [Prism::Node, nil] parent of `node`, or nil at the root.
103
+ # @return [void]
104
+ def index_parents(node, parent)
105
+ return unless node.is_a?(Prism::Node)
106
+
107
+ @parents[node] = parent
108
+ node.compact_child_nodes.each { |child| index_parents(child, node) }
109
+ end
110
+
111
+ # Returns whether this call is an `each` or `map` whose value is discarded.
112
+ #
113
+ # Interpolation is not discarded: a string or symbol calls `to_s`, a
114
+ # backtick command runs, and a regexp is compiled. A hash key is not
115
+ # discarded either: insertion calls `hash` and `eql?`. Those nodes are
116
+ # absent from {VALUE_FLOWS}, so {#result_used?} keeps them. A receiver
117
+ # chain that calls `lazy` is kept too. `Enumerator::Lazy#each` runs the
118
+ # block and `Lazy#map` does not, and the syntax tree can see the `lazy`
119
+ # call. A lazy enumerator stored in a variable has no `lazy` call left.
120
+ # `defined?` does not evaluate its operand, so a call inside it is
121
+ # discarded even when the chain calls `lazy` or the call sits in an
122
+ # interpolation.
123
+ #
124
+ # @param node [Prism::CallNode] call node to inspect.
125
+ # @return [Boolean] true when the swap would be equivalent.
126
+ def unused_result?(node)
127
+ return false unless UNUSED_RESULT_NAMES.include?(node.name)
128
+ return true if inside_defined?(node)
129
+
130
+ !result_used?(node) && !lazy_chain?(node)
131
+ end
132
+
133
+ # Returns whether `node` is inside a `defined?` operand.
134
+ #
135
+ # `defined?` does not run that operand, so an `each` or `map` there
136
+ # does not call `to_s`, run a command, or compile a regexp.
137
+ #
138
+ # @param node [Prism::Node] node to inspect.
139
+ # @return [Boolean] true when an ancestor is a DefinedNode.
140
+ def inside_defined?(node)
141
+ current = node
142
+ while (current = @parents[current])
143
+ return true if current.is_a?(Prism::DefinedNode)
144
+ end
145
+ false
146
+ end
147
+
148
+ # Returns whether a `lazy` call sits in this call's receiver chain.
149
+ #
150
+ # `xs.lazy.each` and `xs.lazy.map { }.each` both count. A lazy
151
+ # enumerator stored in a variable does not: the tree no longer shows
152
+ # the `lazy` call.
153
+ #
154
+ # @param node [Prism::CallNode] `each` or `map` call to inspect.
155
+ # @return [Boolean] true when the chain calls `lazy`.
156
+ def lazy_chain?(node)
157
+ receiver = node.receiver
158
+ while receiver.is_a?(Prism::CallNode)
159
+ return true if receiver.name == :lazy
160
+
161
+ receiver = receiver.receiver
162
+ end
163
+ false
164
+ end
165
+
166
+ # Returns whether any syntax reads this node's value.
167
+ #
168
+ # The method body, a block body, and a lambda body count as read: a
169
+ # caller may use the method's return value, and the method that takes
170
+ # a block may use the block's result. The operator does not guess
171
+ # which methods do. A condition, an assignment, an argument, and a
172
+ # receiver count as read even when the outer expression is discarded.
173
+ # A `while` or `until` body does not, and neither does `ensure` or
174
+ # `defined?`. A `for` body does not either: `for` returns the
175
+ # collection, not the body. A hash key is read on insertion. The value
176
+ # of a hash pair follows the hash. A later `rescue` clause follows the
177
+ # `begin`, the same way the first clause does.
178
+ #
179
+ # @param node [Prism::Node] node whose value may flow outward.
180
+ # @return [Boolean] true when the value is observable.
181
+ def result_used?(node)
182
+ parent = @parents[node]
183
+ return true if parent.nil?
184
+
185
+ case parent
186
+ when Prism::StatementsNode
187
+ return false unless parent.body.last.equal?(node)
188
+
189
+ result_used?(parent)
190
+ when Prism::IfNode, Prism::UnlessNode
191
+ return true if parent.predicate.equal?(node)
192
+
193
+ result_used?(parent)
194
+ when Prism::WhileNode, Prism::UntilNode
195
+ parent.predicate.equal?(node)
196
+ when Prism::ForNode
197
+ # `for` returns the collection, not the body.
198
+ !parent.statements.equal?(node)
199
+ when Prism::EnsureNode, Prism::DefinedNode
200
+ false
201
+ when Prism::AssocNode
202
+ return true if parent.key.equal?(node)
203
+
204
+ result_used?(parent)
205
+ when Prism::RescueNode
206
+ if parent.statements.equal?(node) || parent.subsequent.equal?(node)
207
+ result_used?(parent)
208
+ else
209
+ true
210
+ end
211
+ when Prism::WhenNode, Prism::InNode
212
+ return result_used?(parent) if parent.statements.equal?(node)
213
+
214
+ true
215
+ when Prism::CaseNode, Prism::CaseMatchNode
216
+ return true if parent.predicate.equal?(node)
217
+
218
+ result_used?(parent)
219
+ when Prism::SingletonClassNode
220
+ return true if parent.expression.equal?(node)
221
+
222
+ result_used?(parent)
223
+ when Prism::BlockNode, Prism::LambdaNode, Prism::DefNode
224
+ true
225
+ else
226
+ return result_used?(parent) if VALUE_FLOWS.include?(parent.class)
227
+
228
+ true
229
+ end
230
+ end
42
231
  end
43
232
  end
44
233
  end
@@ -20,19 +20,49 @@ module Mutineer
20
20
  super
21
21
  end
22
22
 
23
+ # Visits an interpolated string. In a `"..."` string, `""` over a text
24
+ # part closes and reopens the string, so it empties the part. Any other
25
+ # text part is handled as a string with no quotes of its own.
26
+ #
27
+ # @param node [Prism::InterpolatedStringNode] node to inspect.
28
+ # @return [void]
29
+ def visit_interpolated_string_node(node)
30
+ double_quoted = node.opening_loc&.slice == '"'
31
+ node.parts.each do |part|
32
+ next part.accept(self) unless part.is_a?(Prism::StringNode)
33
+
34
+ mutate_string(part, quoted: double_quoted || part.opening_loc)
35
+ end
36
+ end
37
+
23
38
  # Visits string literals.
24
39
  #
25
40
  # @param node [Prism::StringNode] node to inspect.
26
41
  # @return [void]
27
42
  def visit_string_node(node)
28
- loc = node.location
29
- token = @source.byteslice(loc.start_offset...loc.end_offset)
30
- emit(loc, '""') unless token == '""' || token == "''" || node.unescaped.empty?
43
+ mutate_string(node, quoted: node.opening_loc)
31
44
  super
32
45
  end
33
46
 
34
47
  private
35
48
 
49
+ # Empties a non-empty string. A string with no quotes of its own (a
50
+ # heredoc line, a `%(...)` or `%Q(...)` part, a `%w[]` element, a regex,
51
+ # backtick or interpolated-symbol part) cannot be emptied with `""`: the edit inserts text
52
+ # and can break the parse (#469). Such a string is declined.
53
+ #
54
+ # @param node [Prism::StringNode] string to inspect.
55
+ # @param quoted [Object] truthy when `""` empties the string.
56
+ # @return [void]
57
+ def mutate_string(node, quoted:)
58
+ return if node.unescaped.empty?
59
+ return decline unless quoted
60
+
61
+ loc = node.location
62
+ token = @source.byteslice(loc.start_offset...loc.end_offset)
63
+ emit(loc, '""') unless token == '""' || token == "''"
64
+ end
65
+
36
66
  # Emits a literal mutation.
37
67
  #
38
68
  # @param loc [Prism::Location] source location.
@@ -12,19 +12,22 @@ module Mutineer
12
12
  #
13
13
  # An endless range (`1..`, `1..nil`) is skipped only when it is a direct
14
14
  # index argument of a call in {SLICE_CALLS} or of an index write
15
- # (`a[1..] += x`). There `(1..)` and `(1...)` select the same elements, so
16
- # the mutant is equivalent, and each skipped range counts as declined.
17
- # Every other endless range is swapped, because the two ranges differ on
18
- # `==`, `hash`, `exclude_end?` and `to_s`, so a test can kill it. This
19
- # includes the value of `h[:k] = 1..` and a call on a class, such as
20
- # `Array[1..]`, which builds a value and does not slice.
15
+ # (`"ab"[1..] += x`) and the receiver is an array or string literal.
16
+ # There `(1..)` and `(1...)` select the same elements, so the mutant is
17
+ # equivalent, and each skipped range counts as declined. A variable, a
18
+ # call, or a constant is swapped. The syntax does not record that
19
+ # receiver's class, and a Hash, a Proc, or a custom `[]` can tell the two
20
+ # ranges apart. Every other endless range is swapped, because the two
21
+ # ranges differ on `==`, `hash`, `exclude_end?` and `to_s`, so a test can
22
+ # kill it. This includes the value of `h[:k] = 1..` and a call on a class,
23
+ # such as `Array[1..]`, which builds a value and does not slice.
21
24
  class RangeLiteral < Base
22
25
  # Maps each range operator to its opposite.
23
26
  SWAPS = { ".." => "...", "..." => ".." }.freeze
24
27
 
25
- # Calls whose direct endless-range index arguments are not mutated. They
26
- # select the same elements for `(1..)` and `(1...)`. The check uses the
27
- # name, and a class receiver is not skipped. The assigned value of `[]=`
28
+ # Calls whose direct endless-range index arguments are not mutated when
29
+ # the receiver is an array or string literal. Those calls select the
30
+ # same elements for `(1..)` and `(1...)`. The assigned value of `[]=`
28
31
  # is not an index, so it is mutated.
29
32
  SLICE_CALLS = %i[[] []= slice slice! byteslice bytesplice values_at].freeze
30
33
 
@@ -38,8 +41,8 @@ module Mutineer
38
41
  super
39
42
  end
40
43
 
41
- # Visits a call. Records the range arguments of a call in {SLICE_CALLS},
42
- # unless the receiver is a class.
44
+ # Visits a call. Records the range arguments of a call in {SLICE_CALLS}
45
+ # when the receiver is an array or string literal.
43
46
  #
44
47
  # @param node [Prism::CallNode] call node to inspect.
45
48
  # @return [void]
@@ -48,7 +51,8 @@ module Mutineer
48
51
  super
49
52
  end
50
53
 
51
- # Visits `a[1..] += x`. Its index arguments are slice arguments.
54
+ # Visits `a[1..] += x`. Records its index arguments when the receiver is
55
+ # an array or string literal.
52
56
  #
53
57
  # @param node [Prism::IndexOperatorWriteNode] node to inspect.
54
58
  # @return [void]
@@ -57,7 +61,8 @@ module Mutineer
57
61
  super
58
62
  end
59
63
 
60
- # Visits `a[1..] ||= x`. Its index arguments are slice arguments.
64
+ # Visits `a[1..] ||= x`. Records its index arguments when the receiver is
65
+ # an array or string literal.
61
66
  #
62
67
  # @param node [Prism::IndexOrWriteNode] node to inspect.
63
68
  # @return [void]
@@ -66,7 +71,8 @@ module Mutineer
66
71
  super
67
72
  end
68
73
 
69
- # Visits `a[1..] &&= x`. Its index arguments are slice arguments.
74
+ # Visits `a[1..] &&= x`. Records its index arguments when the receiver is
75
+ # an array or string literal.
70
76
  #
71
77
  # @param node [Prism::IndexAndWriteNode] node to inspect.
72
78
  # @return [void]
@@ -76,7 +82,7 @@ module Mutineer
76
82
  end
77
83
 
78
84
  # Visits an index used as an assignment target, as in `a[1..], b = x, y`.
79
- # Its index arguments are slice arguments.
85
+ # Records its index arguments when the receiver is an array or string literal.
80
86
  #
81
87
  # @param node [Prism::IndexTargetNode] node to inspect.
82
88
  # @return [void]
@@ -100,33 +106,76 @@ module Mutineer
100
106
 
101
107
  private
102
108
 
103
- # Records each range that is a direct argument of the node. For a `[]=`
104
- # call the last argument is the assigned value, not an index, so it is
105
- # left out. Nothing is recorded when the receiver is a class, as in
106
- # `Array[1..]` or `Registry[1..] ||= x`.
109
+ # Records each range that is a direct argument of the node, when the
110
+ # receiver is an array or string literal. For a `[]=` call the last
111
+ # argument is the assigned value, not an index, so it is left out.
112
+ # A variable, call, or constant is not recorded: its class is not fixed
113
+ # by the syntax, so the range is swapped.
107
114
  #
108
115
  # @param node [Prism::Node] call or index node that responds to `arguments`.
109
116
  # @return [void]
110
117
  def record_slice_args(node)
111
- return if class_receiver?(node)
118
+ return unless sequence_receiver?(node.receiver)
112
119
 
113
120
  args = node.arguments&.arguments || []
114
121
  args = args[0...-1] if node.is_a?(Prism::CallNode) && node.name == :[]=
115
122
  args.each { |arg| @slice_args[arg] = true if arg.is_a?(Prism::RangeNode) }
116
123
  end
117
124
 
118
- # Whether the receiver of a call or index write names a class or module, as in
119
- # `Array[1..]` or `Foo::Bar[1..]`. Such a call builds a value, so it is not
120
- # a slice. A constant in capitals only, as in `ARGV[1..]`, holds a value
121
- # that is sliced, so it does not count.
125
+ # Whether the receiver's syntax is an Array or a String. A quoted string
126
+ # or an array literal is. Parentheses are unwrapped to the value they
127
+ # return, so `("ab")[1..]` is still a string. A `begin` counts only when
128
+ # every branch that can supply its value is one of those literals: a
129
+ # rescue or an else can return a Hash instead. Backticks are a call to
130
+ # `` ` ``, which can be overridden, so they are not literals. A variable,
131
+ # call, or constant is not either: it can hold a Hash, a Proc, or a
132
+ # custom object, and an earlier assignment is not followed.
122
133
  #
123
- # @param node [Prism::Node] call or index node that responds to `receiver`.
134
+ # @param node [Prism::Node, nil] receiver of a slice call or index write.
124
135
  # @return [Boolean]
125
- def class_receiver?(node)
126
- receiver = node.receiver
127
- return false unless receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
136
+ def sequence_receiver?(node)
137
+ case node
138
+ when Prism::ArrayNode, Prism::StringNode, Prism::InterpolatedStringNode
139
+ true
140
+ when Prism::ParenthesesNode
141
+ sequence_receiver?(statement_value(node.body))
142
+ when Prism::BeginNode
143
+ begin_is_sequence?(node)
144
+ else
145
+ false
146
+ end
147
+ end
128
148
 
129
- receiver.name.to_s.match?(/[a-z]/)
149
+ # Whether every value a `begin` can return is an array or string literal.
150
+ # The value is the else, or the body when there is no else. Each rescue
151
+ # can replace it. `ensure` runs but does not supply the value.
152
+ #
153
+ # @param node [Prism::BeginNode] begin expression used as a receiver.
154
+ # @return [Boolean]
155
+ def begin_is_sequence?(node)
156
+ success = if node.else_clause
157
+ statement_value(node.else_clause.statements)
158
+ else
159
+ statement_value(node.statements)
160
+ end
161
+ return false unless sequence_receiver?(success)
162
+
163
+ rescue_clause = node.rescue_clause
164
+ while rescue_clause
165
+ return false unless sequence_receiver?(statement_value(rescue_clause.statements))
166
+
167
+ rescue_clause = rescue_clause.subsequent
168
+ end
169
+ true
170
+ end
171
+
172
+ # The value of a statement list, which is its last statement. Any other
173
+ # node is itself.
174
+ #
175
+ # @param node [Prism::Node, nil] statements, or a single expression.
176
+ # @return [Prism::Node, nil]
177
+ def statement_value(node)
178
+ node.is_a?(Prism::StatementsNode) ? node.body.last : node
130
179
  end
131
180
 
132
181
  # Whether the range has no end: `1..` or `1..nil`.
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Mutineer
6
+ module Mutators
7
+ # Rescue-body mutator (Tier-2, #418).
8
+ #
9
+ # Replaces the whole body of each `rescue` clause with `nil`, and the
10
+ # value of a rescue modifier (`x rescue y` becomes `x rescue nil`). The
11
+ # mutant survives when no test checks what happens after the error.
12
+ # `statement_removal` never touches the last statement of a list, so
13
+ # without this operator a one-statement `rescue` body gets no mutant.
14
+ #
15
+ # A re-raise or a `retry` body is mutated too: `nil` swallows the error,
16
+ # which a test that expects the error kills. An empty body and a body
17
+ # that is already `nil`, `(nil)` or `()` give no mutant, because the edit
18
+ # would change nothing. An `ensure` body is not a rescue body. On the
19
+ # Fizzy copy (#418), 8 of 35 candidates give no mutant this way: 3 empty
20
+ # bodies, 3 `nil` bodies and 2 `nil` modifier values.
21
+ #
22
+ # The operator declines a body whose value is discarded and that only
23
+ # holds literals and variable reads (see docs/equivalent-skips.md).
24
+ class RescueBody < Base
25
+ # Statements that change no state and raise nothing, so dropping one
26
+ # whose value is discarded changes nothing.
27
+ INERT = [Prism::IntegerNode, Prism::FloatNode, Prism::RationalNode, Prism::ImaginaryNode,
28
+ Prism::SymbolNode, Prism::StringNode, Prism::TrueNode, Prism::FalseNode, Prism::NilNode,
29
+ Prism::SelfNode, Prism::LocalVariableReadNode, Prism::InstanceVariableReadNode].freeze
30
+
31
+ # Collects rescue-body mutations for a subject.
32
+ #
33
+ # @param subject [Mutineer::Subject] subject to inspect.
34
+ # @param source [String] full source text.
35
+ # @return [Array<Mutineer::Mutation>] collected mutations.
36
+ def mutations_for(subject, source)
37
+ @discarded = {}.compare_by_identity
38
+ @value_discarded = false
39
+ super
40
+ end
41
+
42
+ # Marks every statement but the last as discarded: nothing reads its value.
43
+ #
44
+ # @param node [Prism::StatementsNode] statement list to inspect.
45
+ # @return [void]
46
+ def visit_statements_node(node)
47
+ node.body[0...-1].each { |statement| @discarded[statement] = true }
48
+ super
49
+ end
50
+
51
+ # Tracks whether this `begin` block's value, and so its rescue values, is discarded.
52
+ #
53
+ # @param node [Prism::BeginNode] begin block, or a def or block body with `rescue`.
54
+ # @return [void]
55
+ def visit_begin_node(node)
56
+ outer = @value_discarded
57
+ @value_discarded = @discarded.key?(node)
58
+ super
59
+ ensure
60
+ @value_discarded = outer
61
+ end
62
+
63
+ # Visits rescue clauses and emits a body removal.
64
+ #
65
+ # @param node [Prism::RescueNode] rescue clause to inspect.
66
+ # @return [void]
67
+ def visit_rescue_node(node)
68
+ emit(node.statements, @value_discarded)
69
+ super # the next clause and nested rescues each get their own mutation
70
+ end
71
+
72
+ # Visits rescue modifiers and emits a value removal.
73
+ #
74
+ # @param node [Prism::RescueModifierNode] rescue modifier to inspect.
75
+ # @return [void]
76
+ def visit_rescue_modifier_node(node)
77
+ emit(node.rescue_expression, @discarded.key?(node))
78
+ super
79
+ end
80
+
81
+ private
82
+
83
+ # Emits a mutation that replaces the node with `nil`.
84
+ #
85
+ # @param node [Prism::Node, nil] rescue body or rescue-modifier value.
86
+ # @param discarded [Boolean] whether nothing reads the value.
87
+ # @return [void]
88
+ def emit(node, discarded)
89
+ return if node.nil? || nil_value?(node)
90
+ return decline if discarded && inert?(node)
91
+
92
+ @mutations << Mutation.new(
93
+ start_offset: node.location.start_offset,
94
+ end_offset: node.location.end_offset,
95
+ replacement: "nil",
96
+ operator: :rescue_body
97
+ )
98
+ end
99
+
100
+ # Returns whether the node is `nil`, alone or in parentheses, or `()`.
101
+ #
102
+ # @param node [Prism::Node] rescue body or rescue-modifier value.
103
+ # @return [Boolean] true when replacing it with `nil` changes nothing.
104
+ def nil_value?(node)
105
+ case node
106
+ when Prism::NilNode then true
107
+ when Prism::StatementsNode then node.body.size == 1 && nil_value?(node.body.first)
108
+ when Prism::ParenthesesNode then node.body.nil? || nil_value?(node.body)
109
+ else false
110
+ end
111
+ end
112
+
113
+ # Returns whether every statement in the node is a literal or a variable read.
114
+ #
115
+ # @param node [Prism::Node] rescue body or rescue-modifier value.
116
+ # @return [Boolean] true when evaluating the node has no effect.
117
+ def inert?(node)
118
+ case node
119
+ when Prism::StatementsNode then node.body.all? { |statement| inert?(statement) }
120
+ when Prism::ParenthesesNode then node.body.nil? || inert?(node.body)
121
+ else INERT.any? { |klass| node.is_a?(klass) }
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -45,15 +45,32 @@ module Mutineer
45
45
 
46
46
  # Mutates a method's final expression to nil when eligible.
47
47
  #
48
- # @param body [Prism::Node] method body node.
48
+ # A def-level `rescue`, `else` or `ensure` makes the body a `BeginNode`
49
+ # (#418). The method then returns the `else` value, or the main body's
50
+ # value when there is no `else`, or the value of the `rescue` clause that
51
+ # ran. Each of those final expressions becomes nil. The `ensure` value is
52
+ # discarded, so it is not a final expression. A final explicit
53
+ # `begin ... end` statement is handled the same way, so RuboCop's
54
+ # Style/RedundantBegin autocorrect does not change the mutants (#470).
55
+ #
56
+ # @param body [Prism::Node, nil] method body node, or a statement list in it.
49
57
  # @return [void]
50
58
  def final_expression_nil(body)
51
- return unless body.is_a?(Prism::StatementsNode)
52
-
53
- last = body.body.last
54
- return if last.nil? || last.is_a?(Prism::ReturnNode) || last.is_a?(Prism::NilNode)
59
+ case body
60
+ when Prism::StatementsNode
61
+ last = body.body.last
62
+ return if last.nil? || last.is_a?(Prism::ReturnNode) || last.is_a?(Prism::NilNode)
63
+ return final_expression_nil(last) if last.is_a?(Prism::BeginNode)
55
64
 
56
- emit(last.location)
65
+ emit(last.location)
66
+ when Prism::BeginNode
67
+ final_expression_nil(body.else_clause ? body.else_clause.statements : body.statements)
68
+ clause = body.rescue_clause
69
+ while clause
70
+ final_expression_nil(clause.statements)
71
+ clause = clause.subsequent # Prism 2 removed the `consequent` alias.
72
+ end
73
+ end
57
74
  end
58
75
 
59
76
  # Emits a return-nil mutation.