Class: Rubocop::Cop::Style::SelfAssignment
- Includes:
- AST::Sexp
- Defined in:
- lib/rubocop/cop/style/self_assignment.rb
Overview
This cop enforces the use the shorthand for self-assignment.
Constant Summary collapse
- OPS =
[:+, :-, :*, :**, :/, :|, :&]
Constants included from Util
Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offenses, #processed_source
Instance Method Summary collapse
- #check(node, var_type) ⇒ Object
- #check_boolean_node(node, rhs, var_name, var_type) ⇒ Object
- #check_send_node(node, rhs, var_name, var_type) ⇒ Object
- #on_cvasgn(node) ⇒ Object
- #on_ivasgn(node) ⇒ Object
- #on_lvasgn(node) ⇒ Object
Methods inherited from Cop
#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #debug?, #display_cop_names?, #exclude_file?, #include_file?, inherited, #initialize, lint?, #message, non_rails, rails?, #relevant_file?, #support_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes
Methods included from PathUtil
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#check(node, var_type) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 32 def check(node, var_type) var_name, rhs = *node return unless rhs if rhs.type == :send check_send_node(node, rhs, var_name, var_type) elsif [:and, :or].include?(rhs.type) check_boolean_node(node, rhs, var_name, var_type) end end |
#check_boolean_node(node, rhs, var_name, var_type) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 58 def check_boolean_node(node, rhs, var_name, var_type) first_operand, _second_operand = *rhs target_node = s(var_type, var_name) if first_operand == target_node operator = rhs.loc.operator.source add_offense(node, :expression, "Use self-assignment shorthand `#{operator}=`.") end end |
#check_send_node(node, rhs, var_name, var_type) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 44 def check_send_node(node, rhs, var_name, var_type) receiver, method_name, *_args = *rhs return unless OPS.include?(method_name) target_node = s(var_type, var_name) if receiver == target_node add_offense(node, :expression, "Use self-assignment shorthand `#{method_name}=`.") end end |
#on_cvasgn(node) ⇒ Object
28 29 30 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 28 def on_cvasgn(node) check(node, :cvar) end |
#on_ivasgn(node) ⇒ Object
24 25 26 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 24 def on_ivasgn(node) check(node, :ivar) end |
#on_lvasgn(node) ⇒ Object
20 21 22 |
# File 'lib/rubocop/cop/style/self_assignment.rb', line 20 def on_lvasgn(node) check(node, :lvar) end |