class RuboCop::Cop::Style::EmptyElse

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

@example EnforcedStyle: both (default)

# warn on empty else and else with nil in it

# bad
if condition
  statement
else
  nil
end

# bad
if condition
  statement
else
end

# good
if condition
  statement
else
  statement
end

# good
if condition
  statement
end

@example EnforcedStyle: empty

# warn only on empty else

# bad
if condition
  statement
else
end

# good
if condition
  statement
else
  nil
end

# good
if condition
  statement
else
  statement
end

# good
if condition
  statement
end

@example EnforcedStyle: nil

# warn on else with nil in it

# bad
if condition
  statement
else
  nil
end

# good
if condition
  statement
else
end

# good
if condition
  statement
else
  statement
end

# good
if condition
  statement
end

@example AllowComments: false (default)

# bad
if condition
  statement
else
  # something comment
  nil
end

# bad
if condition
  statement
else
  # something comment
end

@example AllowComments: true

# good
if condition
  statement
else
  # something comment
  nil
end

# good
if condition
  statement
else
  # something comment
end

Constants

MSG

Public Instance Methods

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

Private Instance Methods

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

  end_pos = base_node(node).loc.end.begin_pos
  corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
end
autocorrect_forbidden?(type) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 193
def autocorrect_forbidden?(type)
  [type, 'both'].include?(missing_else_style)
end
base_node(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 186
def base_node(node)
  return node if node.case_type?
  return node unless node.elsif?

  node.each_ancestor(:if, :case, :when).find(-> { node }) { |parent| parent.loc.end }
end
check(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 145
def check(node)
  return if cop_config['AllowComments'] && comment_in_else?(node.loc)

  empty_check(node) if empty_style?
  nil_check(node) if nil_style?
end
comment_in_else?(loc) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 180
def comment_in_else?(loc)
  return false if loc.else.nil? || loc.end.nil?

  processed_source.contains_comment?(loc.else.join(loc.end))
end
empty_check(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 160
def empty_check(node)
  return unless node.else? && !node.else_branch

  add_offense(node.loc.else) { |corrector| autocorrect(corrector, node) }
end
empty_style?() click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 156
def empty_style?
  style == :empty || style == :both
end
missing_else_style() click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 197
def missing_else_style
  missing_cfg = config.for_cop('Style/MissingElse')
  missing_cfg.fetch('Enabled') ? missing_cfg['EnforcedStyle'] : nil
end
nil_check(node) click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 166
def nil_check(node)
  return unless node.else_branch&.nil_type?

  add_offense(node.loc.else) { |corrector| autocorrect(corrector, node) }
end
nil_style?() click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 152
def nil_style?
  style == :nil || style == :both
end