class RuboCop::Cop::Style::RedundantSelfAssignmentBranch
Checks for places where conditional branch makes redundant self-assignment.
It only detects local variable because it may replace state of instance variable, class variable, and global variable that have state across methods with `nil`.
@example
# bad foo = condition ? bar : foo # good foo = bar if condition # bad foo = condition ? foo : bar # good foo = bar unless condition
Constants
- MSG
Public Instance Methods
on_lvasgn(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 36 def on_lvasgn(node) variable, expression = *node return unless use_if_and_else_branch?(expression) if_branch = expression.if_branch else_branch = expression.else_branch return if inconvertible_to_modifier?(if_branch, else_branch) if self_assign?(variable, if_branch) register_offense(expression, if_branch, else_branch, 'unless') elsif self_assign?(variable, else_branch) register_offense(expression, else_branch, if_branch, 'if') end end
Private Instance Methods
inconvertible_to_modifier?(if_branch, else_branch)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 59 def inconvertible_to_modifier?(if_branch, else_branch) multiple_statements?(if_branch) || multiple_statements?(else_branch) || (else_branch.respond_to?(:elsif?) && else_branch.elsif?) end
multiple_statements?(branch)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 64 def multiple_statements?(branch) branch && branch.children.compact.count > 1 end
register_offense(if_node, offense_branch, opposite_branch, keyword)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 72 def register_offense(if_node, offense_branch, opposite_branch, keyword) add_offense(offense_branch) do |corrector| assignment_value = opposite_branch ? opposite_branch.source : 'nil' replacement = "#{assignment_value} #{keyword} #{if_node.condition.source}" corrector.replace(if_node, replacement) end end
self_assign?(variable, branch)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 68 def self_assign?(variable, branch) variable.to_s == branch&.source end
use_if_and_else_branch?(expression)
click to toggle source
# File lib/rubocop/cop/style/redundant_self_assignment_branch.rb, line 53 def use_if_and_else_branch?(expression) return false unless expression&.if_type? !expression.ternary? || !expression.else? end