Class: RuboCop::Cop::Lint::RedundantRequireStatement

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/lint/redundant_require_statement.rb

Overview

Checks for unnecessary require statement.

The following features are unnecessary require statement because they are already loaded. e.g. Ruby 2.2:

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"]

Below are the features that each TargetRubyVersion targets.

  • 2.0+ …​ enumerator

  • 2.1+ …​ thread

  • 2.2+ …​ Add rational and complex above

  • 2.5+ …​ Add pp above

  • 2.7+ …​ Add ruby2_keywords above

  • 3.1+ …​ Add fiber above

  • 3.2+ …​ set

This cop target those features.

Examples:

# bad
require 'unloaded_feature'
require 'thread'

# good
require 'unloaded_feature'

Cop Safety Information:

  • This cop’s autocorrection is unsafe because if require 'pp' is removed from one file, NameError can be encountered when another file uses PP.pp.

Constant Summary collapse

MSG =
'Remove unnecessary `require` statement.'
RESTRICT_ON_SEND =
%i[require].freeze
RUBY_22_LOADED_FEATURES =
%w[rational complex].freeze
PRETTY_PRINT_METHODS =
%i[
  pretty_inspect pretty_print pretty_print_cycle
  pretty_print_inspect pretty_print_instance_variables
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_send(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/lint/redundant_require_statement.rb', line 61

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

  add_offense(node) do |corrector|
    if node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
      corrector.insert_after(node.parent, "\nend")

      range = range_with_surrounding_space(node.source_range, side: :right)
    else
      range = range_by_whole_lines(node.source_range, include_final_newline: true)
    end

    corrector.remove(range)
  end
end

#pp_const?(node) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/lint/redundant_require_statement.rb', line 57

def_node_matcher :pp_const?, <<~PATTERN
  (const {nil? cbase} :PP)
PATTERN

#redundant_require_statement?(node) ⇒ Object



51
52
53
54
# File 'lib/rubocop/cop/lint/redundant_require_statement.rb', line 51

def_node_matcher :redundant_require_statement?, <<~PATTERN
  (send nil? :require
    (str #redundant_feature?))
PATTERN