class RuboCop::Cop::Lint::SelfAssignment
Checks for self-assignments.
@example
# bad foo = foo foo, bar = foo, bar Foo = Foo # good foo = bar foo, bar = bar, foo Foo = Bar
Constants
- ASSIGNMENT_TYPE_TO_RHS_TYPE
- MSG
Public Instance Methods
on_casgn(node)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 41 def on_casgn(node) lhs_scope, lhs_name, rhs = *node return unless rhs&.const_type? rhs_scope, rhs_name = *rhs add_offense(node) if lhs_scope == rhs_scope && lhs_name == rhs_name end
on_lvasgn(node)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 29 def on_lvasgn(node) lhs, rhs = *node return unless rhs rhs_type = ASSIGNMENT_TYPE_TO_RHS_TYPE[node.type] add_offense(node) if rhs.type == rhs_type && rhs.source == lhs.to_s end
on_masgn(node)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 49 def on_masgn(node) add_offense(node) if multiple_self_assignment?(node) end
on_or_asgn(node)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 53 def on_or_asgn(node) lhs, rhs = *node add_offense(node) if rhs_matches_lhs?(rhs, lhs) end
Also aliased as: on_and_asgn
Private Instance Methods
multiple_self_assignment?(node)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 61 def multiple_self_assignment?(node) lhs, rhs = *node return false unless rhs.array_type? return false unless lhs.children.size == rhs.children.size lhs.children.zip(rhs.children).all? do |lhs_item, rhs_item| rhs_matches_lhs?(rhs_item, lhs_item) end end
rhs_matches_lhs?(rhs, lhs)
click to toggle source
# File lib/rubocop/cop/lint/self_assignment.rb, line 71 def rhs_matches_lhs?(rhs, lhs) rhs.type == ASSIGNMENT_TYPE_TO_RHS_TYPE[lhs.type] && rhs.children.first == lhs.children.first end