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 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 inherited from Base
Instance Attribute Summary
Attributes inherited from Base
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_rails_version, #target_ruby_version
Methods included from ExcludeLimit
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
106 107 108 |
# File 'lib/rubocop/cop/style/redundant_initialize.rb', line 106 def_node_matcher :initialize_forwards?, <<~PATTERN (def _ (args $arg*) $({super zsuper} ...)) PATTERN |
#on_def(node) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rubocop/cop/style/redundant_initialize.rb', line 110 def on_def(node) return if acceptable?(node) if node.body.nil? register_offense(node, MSG_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 |