class RuboCop::Cop::Style::RedundantCondition

Checks for unnecessary conditional expressions.

@example

# bad
a = b ? b : c

# good
a = b || c

@example

# bad
if b
  b
else
  c
end

# good
b || c

# good
if b
  b
elsif cond
  c
end

Constants

MSG
REDUNDANT_CONDITION

Public Instance Methods

on_if(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 40
def on_if(node)
  return if node.elsif_conditional?
  return unless offense?(node)

  message = message(node)

  add_offense(range_of_offense(node), message: message) do |corrector|
    if node.ternary? && !branches_have_method?(node)
      correct_ternary(corrector, node)
    elsif redundant_condition?(node)
      corrector.replace(node, node.if_branch.source)
    else
      corrected = make_ternary_form(node)

      corrector.replace(node, corrected)
    end
  end
end

Private Instance Methods

asgn_type?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 139
def asgn_type?(node)
  node.lvasgn_type? || node.ivasgn_type? || node.cvasgn_type? || node.gvasgn_type?
end
branches_have_assignment?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 129
def branches_have_assignment?(node)
  _condition, if_branch, else_branch = *node

  return false unless if_branch && else_branch

  asgn_type?(if_branch) && (if_branch_variable_name = if_branch.name) &&
    asgn_type?(else_branch) && (else_branch_variable_name = else_branch.name) &&
    if_branch_variable_name == else_branch_variable_name
end
branches_have_method?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 143
def branches_have_method?(node)
  _condition, if_branch, else_branch = *node

  return false unless if_branch && else_branch

  single_argument_method?(if_branch) && single_argument_method?(else_branch) &&
    same_method?(if_branch, else_branch)
end
correct_ternary(corrector, node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 225
def correct_ternary(corrector, node)
  corrector.replace(range_of_offense(node), '||')

  return unless node.else_branch.range_type?

  corrector.wrap(node.else_branch, '(', ')')
end
else_source(else_branch, arithmetic_operation) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 172
def else_source(else_branch, arithmetic_operation) # rubocop:disable Metrics/AbcSize
  if arithmetic_operation
    "#{else_branch.first_argument.source})"
  elsif branches_have_method?(else_branch.parent)
    else_source_if_has_method(else_branch)
  elsif require_parentheses?(else_branch)
    "(#{else_branch.source})"
  elsif without_argument_parentheses_method?(else_branch)
    "#{else_branch.method_name}(#{else_branch.arguments.map(&:source).join(', ')})"
  elsif branches_have_assignment?(else_branch.parent)
    else_source_if_has_assignment(else_branch)
  else
    else_branch.source
  end
end
else_source_if_has_assignment(else_branch) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 198
def else_source_if_has_assignment(else_branch)
  if require_parentheses?(else_branch.expression)
    "(#{else_branch.expression.source})"
  elsif require_braces?(else_branch.expression)
    "{ #{else_branch.expression.source} }"
  else
    else_branch.expression.source
  end
end
else_source_if_has_method(else_branch) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 188
def else_source_if_has_method(else_branch)
  if require_parentheses?(else_branch.first_argument)
    "(#{else_branch.first_argument.source})"
  elsif require_braces?(else_branch.first_argument)
    "{ #{else_branch.first_argument.source} }"
  else
    else_branch.first_argument.source
  end
end
if_source(if_branch, arithmetic_operation) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 160
def if_source(if_branch, arithmetic_operation)
  if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
    if_branch.source.delete_suffix(')')
  elsif arithmetic_operation
    argument_source = if_branch.first_argument.source

    "#{if_branch.receiver.source} #{if_branch.method_name} (#{argument_source}"
  else
    if_branch.source
  end
end
make_ternary_form(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 208
def make_ternary_form(node)
  _condition, if_branch, else_branch = *node
  arithmetic_operation = use_arithmetic_operation?(if_branch)

  ternary_form = [
    if_source(if_branch, arithmetic_operation),
    else_source(else_branch, arithmetic_operation)
  ].join(' || ')
  ternary_form += ')' if branches_have_method?(node) && if_branch.parenthesized?

  if node.parent&.send_type?
    "(#{ternary_form})"
  else
    ternary_form
  end
end
message(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 61
def message(node)
  if redundant_condition?(node)
    REDUNDANT_CONDITION
  else
    MSG
  end
end
offense?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 76
def offense?(node)
  _condition, _if_branch, else_branch = *node

  return false if use_if_branch?(else_branch) || use_hash_key_assignment?(else_branch)

  synonymous_condition_and_branch?(node) && !node.elsif? &&
    (node.ternary? || !else_branch.instance_of?(AST::Node) || else_branch.single_line?)
end
range_of_offense(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 69
def range_of_offense(node)
  return node.loc.expression unless node.ternary?
  return node.loc.expression if node.ternary? && branches_have_method?(node)

  range_between(node.loc.question.begin_pos, node.loc.colon.end_pos)
end
redundant_condition?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 85
def redundant_condition?(node)
  node.modifier_form? || !node.else_branch
end
require_braces?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 240
def require_braces?(node)
  node.hash_type? && !node.braces?
end
require_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 233
def require_parentheses?(node)
  (node.basic_conditional? && node.modifier_form?) ||
    node.range_type? ||
    node.rescue_type? ||
    (node.respond_to?(:semantic_operator?) && node.semantic_operator?)
end
same_method?(if_branch, else_branch) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 156
def same_method?(if_branch, else_branch)
  if_branch.method?(else_branch.method_name) && if_branch.receiver == else_branch.receiver
end
single_argument_method?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 152
def single_argument_method?(node)
  node.send_type? && !node.method?(:[]) && node.arguments.one?
end
synonymous_condition_and_branch?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 101
def synonymous_condition_and_branch?(node)
  condition, if_branch, _else_branch = *node
  # e.g.
  #   if var
  #     var
  #   else
  #     'foo'
  #   end
  return true if condition == if_branch

  # e.g.
  #   if foo
  #     @value = foo
  #   else
  #     @value = another_value?
  #   end
  return true if branches_have_assignment?(node) && condition == if_branch.expression

  # e.g.
  #   if foo
  #     test.value = foo
  #   else
  #     test.value = another_value?
  #   end
  branches_have_method?(node) && condition == if_branch.first_argument &&
    !use_hash_key_access?(if_branch)
end
use_arithmetic_operation?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 244
def use_arithmetic_operation?(node)
  node.respond_to?(:arithmetic_operation?) && node.arithmetic_operation?
end
use_hash_key_access?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 97
def use_hash_key_access?(node)
  node.send_type? && node.method?(:[])
end
use_hash_key_assignment?(else_branch) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 93
def use_hash_key_assignment?(else_branch)
  else_branch&.send_type? && else_branch&.method?(:[]=)
end
use_if_branch?(else_branch) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 89
def use_if_branch?(else_branch)
  else_branch&.if_type?
end
without_argument_parentheses_method?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_condition.rb, line 248
def without_argument_parentheses_method?(node)
  node.send_type? && !node.arguments.empty? &&
    !node.parenthesized? && !node.operator_method? && !node.assignment_method?
end