Class: RuboCop::Cop::Lint::DuplicateMethods
- Defined in:
- lib/rubocop/cop/lint/duplicate_methods.rb
Overview
Checks for duplicated instance (or singleton) method definitions.
NOTE: Aliasing a method to itself is allowed, as it indicates that the developer intends to suppress Ruby’s method redefinition warnings. See bugs.ruby-lang.org/issues/13574.
Constant Summary collapse
- MSG =
rubocop:disable Metrics/ClassLength
'Method `%<method>s` is defined at both %<defined>s and %<current>s.'- RESTRICT_ON_SEND =
%i[alias_method attr_reader attr_writer attr_accessor attr delegate def_delegator def_instance_delegator def_delegators def_instance_delegators].freeze
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
- #alias_method?(node) ⇒ Object
- #class_or_module_new_block?(node) ⇒ Object
- #delegate_method?(node) ⇒ Object
- #delegator?(node) ⇒ Object
- #delegators?(node) ⇒ Object
-
#initialize(config = nil, options = nil) ⇒ DuplicateMethods
constructor
A new instance of DuplicateMethods.
- #method_alias?(node) ⇒ Object
- #on_alias(node) ⇒ Object
- #on_def(node) ⇒ Object
- #on_defs(node) ⇒ Object
-
#on_send(node) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
- #sym_name(node) ⇒ Object
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, #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
#initialize(config = nil, options = nil) ⇒ DuplicateMethods
Returns a new instance of DuplicateMethods.
127 128 129 130 131 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 127 def initialize(config = nil, = nil) super @definitions = {} @scopes = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#alias_method?(node) ⇒ Object
167 168 169 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 167 def_node_matcher :alias_method?, <<~PATTERN (send nil? :alias_method (sym $_name) (sym $_original_name)) PATTERN |
#class_or_module_new_block?(node) ⇒ Object
201 202 203 204 205 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 201 def_node_matcher :class_or_module_new_block?, <<~PATTERN (block (send (const _ {:Class :Module}) :new ...) ...) PATTERN |
#delegate_method?(node) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 172 def_node_matcher :delegate_method?, <<~PATTERN (send nil? :delegate ({sym str} $_)+ (hash <(pair (sym :to) {sym str}) ...>) ) PATTERN |
#delegator?(node) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 180 def_node_matcher :delegator?, <<~PATTERN (send nil? {:def_delegator :def_instance_delegator} { {sym str} ({sym str} $_) | {sym str} {sym str} ({sym str} $_) } ) PATTERN |
#delegators?(node) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 190 def_node_matcher :delegators?, <<~PATTERN (send nil? {:def_delegators :def_instance_delegators} {sym str} ({sym str} $_)+ ) PATTERN |
#method_alias?(node) ⇒ Object
153 154 155 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 153 def_node_matcher :method_alias?, <<~PATTERN (alias (sym $_name) (sym $_original_name)) PATTERN |
#on_alias(node) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 157 def on_alias(node) name, original_name = method_alias?(node) return unless name && original_name return if name == original_name return if node.ancestors.any?(&:if_type?) found_instance_method(node, name) end |
#on_def(node) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 133 def on_def(node) # if a method definition is inside an if, it is very likely # that a different definition is used depending on platform, etc. return if node.each_ancestor.any?(&:if_type?) found_instance_method(node, node.method_name) end |
#on_defs(node) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 141 def on_defs(node) return if node.each_ancestor.any?(&:if_type?) if node.receiver.const_type? _, const_name = *node.receiver check_const_receiver(node, node.method_name, const_name) elsif node.receiver.self_type? check_self_receiver(node, node.method_name) end end |
#on_send(node) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 207 def on_send(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity name, original_name = alias_method?(node) if name && original_name return if name == original_name return if inside_condition?(node) found_instance_method(node, name) elsif (attr = node.attribute_accessor?) on_attr(node, *attr) elsif active_support_extensions_enabled? && (names = delegate_method?(node)) return if inside_condition?(node) on_delegate(node, names) elsif (name = delegator?(node)) return if inside_condition?(node) found_instance_method(node, name) elsif (names = delegators?(node)) return if inside_condition?(node) names.each { |name| found_instance_method(node, name) } end end |
#sym_name(node) ⇒ Object
198 |
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 198 def_node_matcher :sym_name, '(sym $_name)' |