class RuboCop::Cop::Style::MissingElse
Checks for ‘if` expressions that do not have an `else` branch.
NOTE: Pattern matching is allowed to have no ‘else` branch because unlike `if` and `case`, it raises `NoMatchingPatternError` if the pattern doesn’t match and without having ‘else`.
Supported styles are: if, case, both.
@example EnforcedStyle: both (default)
# warn when an `if` or `case` expression is missing an `else` branch. # bad if condition statement end # bad case var when condition statement end # good if condition statement else # the content of `else` branch will be determined by Style/EmptyElse end # good case var when condition statement else # the content of `else` branch will be determined by Style/EmptyElse end
@example EnforcedStyle: if
# warn when an `if` expression is missing an `else` branch. # bad if condition statement end # good if condition statement else # the content of `else` branch will be determined by Style/EmptyElse end # good case var when condition statement end # good case var when condition statement else # the content of `else` branch will be determined by Style/EmptyElse end
@example EnforcedStyle: case
# warn when a `case` expression is missing an `else` branch. # bad case var when condition statement end # good case var when condition statement else # the content of `else` branch will be determined by Style/EmptyElse end # good if condition statement end # good if condition statement else # the content of `else` branch will be determined by Style/EmptyElse end
Constants
- MSG
- MSG_EMPTY
- MSG_NIL
Public Instance Methods
Source
# File lib/rubocop/cop/style/missing_else.rb, line 115 def on_case(node) return if if_style? check(node) end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 121 def on_case_match(node) # do nothing. end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 108 def on_normal_if_unless(node) return if case_style? return if unless_else_cop_enabled? && node.unless? check(node) end
Private Instance Methods
Source
# File lib/rubocop/cop/style/missing_else.rb, line 146 def autocorrect(corrector, node) node = node.ancestors.find { |ancestor| ancestor.loc.end } unless node.loc.end 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
Source
# File lib/rubocop/cop/style/missing_else.rb, line 161 def case_style? style == :case end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 127 def check(node) return if node.else? add_offense(node, message: format(message_template, type: node.type)) do |corrector| autocorrect(corrector, node) end end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 179 def empty_else_config config.for_cop('Style/EmptyElse') end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 173 def empty_else_style return unless empty_else_config.key?('EnforcedStyle') empty_else_config['EnforcedStyle'].to_sym end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 157 def if_style? style == :if end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 135 def message_template case empty_else_style when :empty MSG_NIL when :nil MSG_EMPTY else MSG end end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 169 def unless_else_config config.for_cop('Style/UnlessElse') end
Source
# File lib/rubocop/cop/style/missing_else.rb, line 165 def unless_else_cop_enabled? unless_else_config.fetch('Enabled') end