class RuboCop::Cop::Style::EmptyElse

Checks for empty else-clauses, possibly including comments and/or an explicit `nil` depending on the EnforcedStyle.

SupportedStyles:

@example

# good for all styles
if condition
  statement
else
  statement
end

# good for all styles
if condition
  statement
end

empty - warn only on empty else

@example
# bad
if condition
  statement
else
end

# good
if condition
  statement
else
  nil
end

nil - warn on else with nil in it

@example
# bad
if condition
  statement
else
  nil
end

# good
if condition
  statement
else
end

both - warn on empty else and else with nil in it

@example
# bad
if condition
  statement
else
  nil
end

# bad
if condition
  statement
else
end

Constants

MSG

Public Instance Methods

on_case(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 78
def on_case(node)
  check(node, case_else_clause(node))
end
on_normal_if_unless(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 74
def on_normal_if_unless(node)
  check(node, if_else_clause(node))
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 114
def autocorrect(node)
  return false if autocorrect_forbidden?(node.type.to_s)

  lambda do |corrector|
    end_pos = if node.loc.end
                node.loc.end.begin_pos
              else
                node.source_range.end_pos + 1
              end
    range = Parser::Source::Range.new(node.source_range.source_buffer,
                                      node.loc.else.begin_pos,
                                      end_pos)
    corrector.remove(range)
  end
end
autocorrect_forbidden?(type) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 130
def autocorrect_forbidden?(type)
  [type, 'both'].include? missing_else_style
end
both_check(node, else_clause) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 104
def both_check(node, else_clause)
  return if node.loc.else.nil?

  if else_clause.nil?
    add_offense(node, :else, MSG)
  elsif else_clause.type == :nil
    add_offense(node, :else, MSG)
  end
end
check(node, else_clause) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 84
def check(node, else_clause)
  case style
  when :empty
    empty_check(node, else_clause)
  when :nil
    nil_check(node, else_clause)
  when :both
    both_check(node, else_clause)
  end
end
empty_check(node, else_clause) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 95
def empty_check(node, else_clause)
  add_offense(node, :else, MSG) if node.loc.else && else_clause.nil?
end
missing_else_style() click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 134
def missing_else_style
  missing_config = config.for_cop('Style/MissingElse')
  missing_config['Enabled'] ? missing_config['EnforcedStyle'] : nil
end
nil_check(node, else_clause) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 99
def nil_check(node, else_clause)
  return unless else_clause && else_clause.type == :nil
  add_offense(node, node.location, MSG)
end