Sha256: ce5fa35afc34d5f5678e6b203043a3ea4e23ef140754bb56ba6e03847669e58f

Contents?: true

Size: 829 Bytes

Versions: 5

Compression:

Stored size: 829 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for unreachable code.
      # The check are based on the presence of flow of control
      # statement in non-final position in *begin*(implicit) blocks.
      class UnreachableCode < Cop
        MSG = 'Unreachable code detected.'

        NODE_TYPES = [:return, :next, :break, :retry, :redo]
        FLOW_COMMANDS = [:throw, :raise, :fail]

        def on_begin(node)
          expressions = *node

          expressions.each_cons(2) do |e1, e2|
            if NODE_TYPES.include?(e1.type) || flow_command?(e1)
              add_offense(e2, :expression)
            end
          end
        end

        private

        def flow_command?(node)
          FLOW_COMMANDS.any? { |c| command?(c, node) }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.20.1 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.20.0 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.19.1 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.19.0 lib/rubocop/cop/lint/unreachable_code.rb