Class: RuboCop::Cop::Greenroom::UnsafeScienceBody

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/greenroom/unsafe_science_body.rb

Overview

Rejects the two constructs that change meaning when a method body moves into a science block.

A greenroom change moves an existing body, unchanged, into the use block. Most constructs survive that move: a trial of yield, of rescue and ensure written directly in the method, of __method__, of local variables, and of a constant read from the same lexical place, each behaved the same before and after the move. Two did not, and this cop names those two. The trial covered one form of each construct, so treat the list as measured rather than complete.

return leaves the enclosing method from inside the block, so the comparison and the publication never run, and the experiment reports nothing while appearing to work.

binding reports the block's local variables rather than the method's, so the same expression describes a different scope after the move.

This cop reports what one file shows. The gate that decides whether a change may merge reads the version before the change as well, so silence here means that this cop cannot tell, rather than that a change is accepted.

Constant Summary collapse

RETURN_MESSAGE =
"A return in a science body bypasses comparison and publication."
BINDING_MESSAGE =
"A binding in a science body observes the block scope."

Instance Method Summary collapse

Instance Method Details

#on_return(node) ⇒ Object



32
33
34
# File 'lib/rubocop/cop/greenroom/unsafe_science_body.rb', line 32

def on_return(node)
  add_offense(node, message: RETURN_MESSAGE) if unsafe_return?(node)
end

#on_send(node) ⇒ Object



36
37
38
39
40
41
# File 'lib/rubocop/cop/greenroom/unsafe_science_body.rb', line 36

def on_send(node)
  return unless node.receiver.nil? && node.method?(:binding)
  return unless science_body_ancestor(node)

  add_offense(node, message: BINDING_MESSAGE)
end