Sha256: 27c24a3895893280ea153b1ae4670c5bbd7bc18bb380fec3554fd16d873bb7be

Contents?: true

Size: 863 Bytes

Versions: 3

Compression:

Stored size: 863 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_offence(:warning, e2.loc.expression, MSG)
            end
          end

          super
        end

        private

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/lint/unreachable_code.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.9.0 lib/rubocop/cop/lint/unreachable_code.rb