Module: RuboCop::Cop::SafeAssignment

Included in:
Lint::AssignmentInCondition, RuboCop::Cop::Style::ParenthesesAroundCondition
Defined in:
lib/rubocop/cop/mixin/safe_assignment.rb

Overview

Common functionality for safe assignment. By safe assignment we mean putting parentheses around an assignment to indicate "I know I'm using an assignment as a condition. It's not a mistake."

Instance Method Summary collapse

Instance Method Details

#safe_assignment?(node) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/mixin/safe_assignment.rb', line 9

def safe_assignment?(node)
  return false unless node.type == :begin
  return false unless node.children.size == 1

  child = node.children.first
  case child.type
  when *Util::EQUALS_ASGN_NODES
    true
  when :send
    _receiver, method_name, _args = *child
    method_name.to_s.end_with?('=')
  else
    false
  end
end

#safe_assignment_allowed?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rubocop/cop/mixin/safe_assignment.rb', line 25

def safe_assignment_allowed?
  cop_config['AllowSafeAssignment']
end