Class: RuboCop::Cop::Style::RedundantInitialize
- Extended by:
- AutoCorrector
- Includes:
- CommentsHelp, RangeHelp
- Defined in:
- lib/rubocop/cop/style/redundant_initialize.rb
Overview
Checks for initialize methods that are redundant.
An initializer is redundant if it does not do anything, or if it only
calls super with the same arguments given to it. If the initializer takes
an argument that accepts multiple values (restarg, kwrestarg, etc.) it
will not register an offense, because it allows the initializer to take a different
number of arguments as its superclass potentially does.
NOTE: If an initializer takes any arguments and has an empty body, RuboCop
assumes it to not be redundant. This is to prevent potential ArgumentError.
NOTE: If an initializer argument has a default value, RuboCop assumes it to not be redundant.
NOTE: Empty initializers are registered as offenses, but it is possible
to purposely create an empty initialize method to override a superclass's
initializer.
Constant Summary collapse
- MSG =
'Remove unnecessary `initialize` method.'- MSG_EMPTY =
'Remove unnecessary empty `initialize` method.'
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
Methods included from AutoCorrector
Methods included from CommentsHelp
#comments_contain_disables?, #comments_in_range, #contains_comments?, #source_range_with_comment
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
cop_dir_for, #exclude_limit, read_limits
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#initialize_forwards?(node) ⇒ Object
115 116 117 |
# File 'lib/rubocop/cop/style/redundant_initialize.rb', line 115 def_node_matcher :initialize_forwards?, <<~PATTERN (def _ (args $arg*) $({super zsuper} ...)) PATTERN |
#on_def(node) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rubocop/cop/style/redundant_initialize.rb', line 119 def on_def(node) return if acceptable?(node) if node.body.nil? register_offense(node, MSG_EMPTY) if node.arguments.empty? else return if node.body.begin_type? if (args, super_node = initialize_forwards?(node)) return unless same_args?(super_node, args) register_offense(node, MSG) end end end |