class RuboCop::Cop::Style::EmptyElse
Checks for empty else-clauses, possibly including comments and/or an explicit `nil` depending on the EnforcedStyle.
@example EnforcedStyle: empty
# warn only on empty else # bad if condition statement else end # good if condition statement else nil end # good if condition statement else statement end # good if condition statement end
@example EnforcedStyle: nil
# warn on else with nil in it # bad if condition statement else nil end # good if condition statement else end # good if condition statement else statement end # good if condition statement end
@example EnforcedStyle: both (default)
# warn on empty else and else with nil in it # bad if condition statement else nil end # bad if condition statement else end # good if condition statement else statement end # good if condition statement end
Constants
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 107 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
on_case(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 103 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 99 def on_normal_if_unless(node) check(node) end
Private Instance Methods
autocorrect_forbidden?(type)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 164 def autocorrect_forbidden?(type) [type, 'both'].include?(missing_else_style) end
base_node(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 155 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
check(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 119 def check(node) empty_check(node) if empty_style? nil_check(node) if nil_style? end
comment_in_else?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 144 def comment_in_else?(node) range = else_line_range(node.loc) processed_source.find_comment { |c| range.include?(c.loc.line) } end
else_line_range(loc)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 149 def else_line_range(loc) return 0..0 if loc.else.nil? || loc.end.nil? loc.else.first_line..loc.end.first_line end
empty_check(node)
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 132 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 128 def empty_style? style == :empty || style == :both end
missing_else_style()
click to toggle source
# File lib/rubocop/cop/style/empty_else.rb, line 168 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 138 def nil_check(node) return unless 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 124 def nil_style? style == :nil || style == :both end