Sha256: 99cd30fa2242f7536b16dc0c720b4fece37b4c6bd67f15fa650f0ff991116616

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Checks for empty else-clauses, possibly including comments and/or an
      # explicit `nil`.
      class EmptyElse < Cop
        include OnNormalIfUnless

        MSG = 'Redundant empty `else`-clause.'

        def on_normal_if_unless(node)
          check(node, if_else_clause(node))
        end

        def on_case(node)
          check(node, case_else_clause(node))
        end

        private

        def check(node, else_clause)
          return unless node.loc.else
          return if else_clause && else_clause.type != :nil

          add_offense(node, :else, MSG)
        end

        def if_else_clause(node)
          keyword = node.loc.keyword
          if keyword.is?('if')
            node.children[2]
          elsif keyword.is?('elsif')
            node.children[2]
          elsif keyword.is?('unless')
            node.children[1]
          end
        end

        def case_else_clause(node)
          node.children.last
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.29.1 lib/rubocop/cop/style/empty_else.rb
rubocop-0.29.0 lib/rubocop/cop/style/empty_else.rb
rubocop-0.28.0 lib/rubocop/cop/style/empty_else.rb