class RuboCop::Cop::Style::MissingElse
end
# the content of ‘else` branch will be determined by Style/EmptyElse
else
statement
if condition
# good
end
statement
if condition
# good
end
# the content of `else` branch will be determined by Style/EmptyElse
else
statement
when condition
case var
# good
end
statement
when condition
case var
# bad
# warn when a `case` expression is missing an `else` branch.
@example EnforcedStyle: case
end
# the content of `else` branch will be determined by Style/EmptyElse
else
statement
when condition
case var
# good
end
statement
when condition
case var
# good
end
# the content of `else` branch will be determined by Style/EmptyElse
else
statement
if condition
# good
end
statement
if condition
# bad
# warn when an `if` expression is missing an `else` branch.
@example EnforcedStyle: if
end
# the content of `else` branch will be determined by Style/EmptyElse
else
statement
when condition
case var
# good
end
# the content of `else` branch will be determined by Style/EmptyElse
else
statement
if condition
# good
end
statement
when condition
case var
# bad
end
statement
if condition
# bad
# warn when an `if` or `case` expression is missing an `else` branch.
@example EnforcedStyle: both (default)
Supported styles are: if, case, both.
it raises `NoMatchingPatternError` if the pattern doesn’t match and without having ‘else`.
NOTE: Pattern matching is allowed to have no `else` branch because unlike `if` and `case`,
Checks for `if` expressions that do not have an `else` branch.
def autocorrect(corrector, node)
def autocorrect(corrector, node) case empty_else_style when :empty corrector.insert_before(node.loc.end, 'else; nil; ') when :nil corrector.insert_before(node.loc.end, 'else; ') end end
def case_style?
def case_style? style == :case end
def check(node)
def check(node) return if node.else? add_offense(node, message: format(message_template, type: node.type)) do |corrector| autocorrect(corrector, node) end end
def empty_else_config
def empty_else_config config.for_cop('Style/EmptyElse') end
def empty_else_cop_enabled?
def empty_else_cop_enabled? empty_else_config.fetch('Enabled') end
def empty_else_style
def empty_else_style return unless empty_else_config.key?('EnforcedStyle') empty_else_config['EnforcedStyle'].to_sym end
def if_style?
def if_style? style == :if end
def message_template
def message_template case empty_else_style when :empty MSG_NIL when :nil MSG_EMPTY else MSG end end
def on_case(node)
def on_case(node) return if if_style? check(node) end
def on_case_match(node)
def on_case_match(node) # do nothing. end
def on_normal_if_unless(node)
def on_normal_if_unless(node) return if case_style? return if unless_else_cop_enabled? && node.unless? check(node) end
def unless_else_config
def unless_else_config config.for_cop('Style/UnlessElse') end
def unless_else_cop_enabled?
def unless_else_cop_enabled? unless_else_config.fetch('Enabled') end