class RuboCop::Cop::Style::IdenticalConditionalBranches

This cop checks for identical lines at the end of each branch of a conditional statement.

@example

@bad
if condition
  do_x
  do_z
else
  do_y
  do_z
end

@good
if condition
  do_x
else
  do_y
end
do_z

Constants

MSG

Public Instance Methods

on_case(node) click to toggle source
# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 43
def on_case(node)
  return unless node.loc.else
  _condition, *when_branches, else_branch = *node
  return unless else_branch # empty else
  when_branches = expand_when_branches(when_branches)

  check_node(when_branches.push(else_branch))
end
on_if(node) click to toggle source
# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 31
def on_if(node)
  return if elsif?(node)
  _condition, if_branch, else_branch = *node
  branches = expand_elses(else_branch).unshift(if_branch)

  # return if any branch is empty. An empty branch can be an `if`
  # without an `else`, or a branch that contains only comments.
  return if branches.any?(&:nil?)

  check_node(branches)
end

Private Instance Methods

check_node(branches) click to toggle source
# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 54
def check_node(branches)
  branches = branches.map { |branch| tail(branch) }

  return unless branches.all? { |branch| branch == branches[0] }
  branches.each do |branch|
    add_offense(branch, :expression, format(MSG, branch.source))
  end
end
expand_elses(branch) click to toggle source

`elsif` branches show up in the if node as nested `else` branches. We need to recursively iterate over all `else` branches.

# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 65
def expand_elses(branch)
  if branch.nil?
    [nil]
  elsif branch.if_type?
    _condition, elsif_branch, else_branch = *branch
    expand_elses(else_branch).unshift(elsif_branch)
  else
    [branch]
  end
end
expand_when_branches(when_branches) click to toggle source

`when` nodes contain the entire branch including the condition. We only need the contents of the branch, not the condition.

# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 78
def expand_when_branches(when_branches)
  when_branches.map { |branch| branch.children[1] }
end
tail(node) click to toggle source
# File lib/rubocop/cop/style/identical_conditional_branches.rb, line 82
def tail(node)
  if node && node.begin_type?
    node.children.last
  else
    node
  end
end