Sha256: 2860fb0b0ed074947f85e42cdef5c2225e608b9a9f75c08aabab12171ba8d89e

Contents?: true

Size: 1.5 KB

Versions: 19

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for unnecessary `require` statement.
      #
      # The following features are unnecessary `require` statement because
      # they are already loaded.
      #
      #   ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
      #   ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
      #   ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
      #
      # This cop targets Ruby 2.2 or higher containing these 4 features.
      #
      # @example
      #   # bad
      #   require 'unloaded_feature'
      #   require 'thread'
      #
      #   # good
      #   require 'unloaded_feature'
      class RedundantRequireStatement < Base
        include RangeHelp
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 2.2

        MSG = 'Remove unnecessary `require` statement.'
        RESTRICT_ON_SEND = %i[require].freeze

        # @!method unnecessary_require_statement?(node)
        def_node_matcher :unnecessary_require_statement?, <<~PATTERN
          (send nil? :require
            (str {"enumerator" "rational" "complex" "thread"}))
        PATTERN

        def on_send(node)
          return unless unnecessary_require_statement?(node)

          add_offense(node) do |corrector|
            range = range_with_surrounding_space(node.loc.expression, side: :right)

            corrector.remove(range)
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 15 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/redundant_require_statement.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/redundant_require_statement.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/redundant_require_statement.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.36.0 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.35.1 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.35.0 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.34.1 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.34.0 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.33.0 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.32.0 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.31.2 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.31.1 lib/rubocop/cop/lint/redundant_require_statement.rb
rubocop-1.31.0 lib/rubocop/cop/lint/redundant_require_statement.rb