class RuboCop::Cop::Style::MissingElse

Checks for `if` expressions that do not have an `else` branch. SupportedStyles

if @example

# bad
if condition
  statement
end

case @example

# bad
case var
when condition
  statement
end

@example

# good
if condition
  statement
else
# the content of the else branch will be determined by Style/EmptyElse
end

Constants

MSG
MSG_EMPTY
MSG_NIL

Public Instance Methods

on_case(node) click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 47
def on_case(node)
  return if if_style?

  check(node)
end
on_normal_if_unless(node) click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 40
def on_normal_if_unless(node)
  return if case_style?
  return if unless_else_cop_enabled? && node.unless?

  check(node)
end

Private Instance Methods

case_style?() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 86
def case_style?
  style == :case
end
check(node) click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 55
def check(node)
  return if node.else?

  if empty_else_cop_enabled?
    if empty_else_style == :empty
      add_offense(node)
    elsif empty_else_style == :nil
      add_offense(node)
    end
  end

  add_offense(node)
end
empty_else_config() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 107
def empty_else_config
  config.for_cop('Style/EmptyElse')
end
empty_else_cop_enabled?() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 98
def empty_else_cop_enabled?
  empty_else_config.fetch('Enabled')
end
empty_else_style() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 102
def empty_else_style
  return unless empty_else_config.key?('EnforcedStyle')
  empty_else_config['EnforcedStyle'].to_sym
end
if_style?() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 82
def if_style?
  style == :if
end
message(node) click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 69
def message(node)
  template = case empty_else_style
             when :empty
               MSG_NIL
             when :nil
               MSG_EMPTY
             else
               MSG
             end

  format(template, node.type)
end
unless_else_config() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 94
def unless_else_config
  config.for_cop('Style/UnlessElse')
end
unless_else_cop_enabled?() click to toggle source
# File lib/rubocop/cop/style/missing_else.rb, line 90
def unless_else_cop_enabled?
  unless_else_config.fetch('Enabled')
end