Class: Mutineer::Mutators::ReturnNil

Inherits:
Base
  • Object
show all
Defined in:
lib/mutineer/mutators/return_nil.rb

Overview

Return-value-nil operator (Tier 2, OFF by default). Two rules:

1. an explicit `return <expr>` -> `return nil`, unless the value is
 already nil (no-op guard).
2. a method body whose final expression is neither a ReturnNode nor a
 NilNode -> that expression becomes `nil`.

Nested defs are their own subjects, so we never descend into them (R10).

Clean-room: from the spec's operator description, not the mutant gem.

Instance Method Summary collapse

Instance Method Details

#mutations_for(subject, source) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/mutineer/mutators/return_nil.rb', line 16

def mutations_for(subject, source)
  @source = source
  @mutations = []
  body = subject.def_node.body
  if body
    body.accept(self)       # rule 1 (explicit return nodes in this body)
    final_expression_nil(body) # rule 2 (method's final expression)
  end
  @mutations
end

#visit_def_node(node) ⇒ Object

Nested method definitions are discovered as their own subjects; do not recurse into them (prevents double-counting their statements).



40
# File 'lib/mutineer/mutators/return_nil.rb', line 40

def visit_def_node(node); end

#visit_return_node(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/mutineer/mutators/return_nil.rb', line 27

def visit_return_node(node)
  args = node.arguments
  if args
    values = args.arguments
    unless values.size == 1 && values.first.is_a?(Prism::NilNode)
      emit(args.location)
    end
  end
  super
end