class RuboCop::Cop::Style::EmptyElse
end
statement
if condition
# good
end
statement
else
statement
if condition
# good
end
else
statement
if condition
# bad
end
nil
else
statement
if condition
# bad
# warn on empty else and else with nil in it
@example EnforcedStyle: both (default)
end
statement
if condition
# good
end
statement
else
statement
if condition
# good
end
else
statement
if condition
# good
end
nil
else
statement
if condition
# bad
# warn on else with nil in it
@example EnforcedStyle: nil
end
statement
if condition
# good
end
statement
else
statement
if condition
# good
end
nil
else
statement
if condition
# good
end
else
statement
if condition
# bad
# warn only on empty else
@example EnforcedStyle: empty
explicit ‘nil` depending on the EnforcedStyle.
Checks for empty else-clauses, possibly including comments and/or an
def autocorrect(node)
def autocorrect(node) return false if autocorrect_forbidden?(node.type.to_s) return false if comment_in_else?(node) lambda do |corrector| end_pos = base_node(node).loc.end.begin_pos corrector.remove(range_between(node.loc.else.begin_pos, end_pos)) end end
def autocorrect_forbidden?(type)
def autocorrect_forbidden?(type) [type, 'both'].include?(missing_else_style) end
def base_node(node)
def base_node(node) return node if node.case_type? return node unless node.elsif? node.each_ancestor(:if, :case, :when).find(-> { node }) do |parent| parent.loc.end end end
def check(node)
def check(node) empty_check(node) if empty_style? nil_check(node) if nil_style? end
def comment_in_else?(node)
def comment_in_else?(node) range = else_line_range(node.loc) processed_source.find_comment { |c| range.include?(c.loc.line) } end
def else_line_range(loc)
def else_line_range(loc) return 0..0 if loc.else.nil? || loc.end.nil? loc.else.first_line..loc.end.first_line end
def empty_check(node)
def empty_check(node) return unless node.else? && !node.else_branch add_offense(node, location: :else) end
def empty_style?
def empty_style? style == :empty || style == :both end
def missing_else_style
def missing_else_style missing_cfg = config.for_cop('Style/MissingElse') missing_cfg.fetch('Enabled') ? missing_cfg['EnforcedStyle'] : nil end
def nil_check(node)
def nil_check(node) return unless node.else_branch&.nil_type? add_offense(node, location: :else) end
def nil_style?
def nil_style? style == :nil || style == :both end
def on_case(node)
def on_case(node) check(node) end
def on_normal_if_unless(node)
def on_normal_if_unless(node) check(node) end