Sha256: 2a3777e3dfc70fa77be60ddd16c18b3836d12c2b4a63a25f24a612e9e8e069e5

Contents?: true

Size: 852 Bytes

Versions: 11

Compression:

Stored size: 852 Bytes

Contents

# frozen_string_literal: true

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.'.freeze

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

        def on_begin(node)
          expressions = *node

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

        private

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

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.46.0 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.45.0 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.44.1 lib/rubocop/cop/lint/unreachable_code.rb
rubocop-0.44.0 lib/rubocop/cop/lint/unreachable_code.rb