class RuboCop::Cop::Style::EmptyElse
Checks for empty else-clauses, possibly including comments and/or an explicit `nil` depending on the EnforcedStyle.
SupportedStyles:
@example
# good for all styles if condition statement else statement end # good for all styles if condition statement end
@example
# empty - warn only on empty else # bad if condition statement else end # good if condition statement else nil end
@example
# nil - warn on else with nil in it # bad if condition statement else nil end # good if condition statement else end
@example
# both - warn on empty else and else with nil in it # bad if condition statement else nil end # bad if condition statement else end
Constants
- MSG
Public Instance Methods
on_case(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 82 def on_case(node) check(node) end
on_normal_if_unless(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 78 def on_normal_if_unless(node) check(node) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 113 def autocorrect(node) return false if autocorrect_forbidden?(node.type.to_s) lambda do |corrector| end_pos = base_if_node(node).loc.end.begin_pos corrector.remove(range_between(node.loc.else.begin_pos, end_pos)) end end
autocorrect_forbidden?(type)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 128 def autocorrect_forbidden?(type) [type, 'both'].include?(missing_else_style) end
base_if_node(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 123 def base_if_node(node) return node unless node.case_type? || node.elsif? node.each_ancestor(:if).find { |parent| parent.loc.end } || node end
check(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 88 def check(node) empty_check(node) if empty_style? nil_check(node) if nil_style? end
empty_check(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 101 def empty_check(node) return unless node.else? && !node.else_branch add_offense(node, location: :else) end
empty_style?()
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 97 def empty_style? style == :empty || style == :both end
missing_else_style()
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 132 def missing_else_style missing_cfg = config.for_cop('Style/MissingElse') missing_cfg.fetch('Enabled') ? missing_cfg['EnforcedStyle'] : nil end
nil_check(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 107 def nil_check(node) return unless node.else_branch && node.else_branch.nil_type? add_offense(node, location: :else) end
nil_style?()
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 93 def nil_style? style == :nil || style == :both end