class RuboCop::Cop::Lint::ElseLayout

This cop checks for odd else block layout - like having an expression on the same line as the else keyword, which is usually a mistake.

@example

# bad

if something
  ...
else do_this
  do_that
end

@example

# good

if something
  ...
else
  do_this
  do_that
end

Constants

MSG

Public Instance Methods

on_if(node) click to toggle source
# File lib/rubocop/cop/lint/else_layout.rb, line 33
def on_if(node)
  return if node.ternary? || node.elsif?

  check(node)
end

Private Instance Methods

check(node) click to toggle source
# File lib/rubocop/cop/lint/else_layout.rb, line 41
def check(node)
  return unless node.else_branch

  if node.else? && node.loc.else.is?('else')
    check_else(node)
  elsif node.if?
    check(node.else_branch)
  end
end
check_else(node) click to toggle source
# File lib/rubocop/cop/lint/else_layout.rb, line 51
def check_else(node)
  else_branch = node.else_branch

  return unless else_branch.begin_type?

  first_else = else_branch.children.first

  return unless first_else.source_range.line == node.loc.else.line

  add_offense(first_else)
end