Class: RuboCop::Cop::Lint::DuplicateMethods

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/duplicate_methods.rb

Overview

Checks for duplicated instance (or singleton) method definitions.

Examples:


# bad
def foo
  1
end

def foo
  2
end

# bad
def foo
  1
end

alias foo bar

# good
def foo
  1
end

def bar
  2
end

# good
def foo
  1
end

alias bar foo

AllCops:ActiveSupportExtensionsEnabled: false (default)


# good
def foo
  1
end

delegate :foo, to: :bar

AllCops:ActiveSupportExtensionsEnabled: true


# bad
def foo
  1
end

delegate :foo, to: :bar

# good
def foo
  1
end

delegate :baz, to: :bar

# good - delegate with splat arguments is ignored
def foo
  1
end

delegate :foo, **options

# good - delegate inside a condition is ignored
def foo
  1
end

if cond
  delegate :foo, to: :bar
end

Constant Summary collapse

MSG =
'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].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

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

#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

#initialize(config = nil, options = nil) ⇒ DuplicateMethods

Returns a new instance of DuplicateMethods.



89
90
91
92
93
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 89

def initialize(config = nil, options = nil)
  super
  @definitions = {}
  @scopes = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#alias_method?(node) ⇒ Object



127
128
129
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 127

def_node_matcher :alias_method?, <<~PATTERN
  (send nil? :alias_method (sym $_name) _)
PATTERN

#delegate_method?(node) ⇒ Object



132
133
134
135
136
137
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 132

def_node_matcher :delegate_method?, <<~PATTERN
  (send nil? :delegate
    ({sym str} $_)+
    (hash <(pair (sym :to) {sym str}) ...>)
  )
PATTERN

#method_alias?(node) ⇒ Object



115
116
117
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 115

def_node_matcher :method_alias?, <<~PATTERN
  (alias (sym $_name) sym)
PATTERN

#on_alias(node) ⇒ Object



119
120
121
122
123
124
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 119

def on_alias(node)
  return unless (name = method_alias?(node))
  return if node.ancestors.any?(&:if_type?)

  found_instance_method(node, name)
end

#on_def(node) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 95

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 103

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/CyclomaticComplexity, Metrics/PerceivedComplexity



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 142

def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  if (name = alias_method?(node))
    return if node.ancestors.any?(&:if_type?)

    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 node.ancestors.any?(&:if_type?)

    on_delegate(node, names)
  end
end

#sym_name(node) ⇒ Object



140
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 140

def_node_matcher :sym_name, '(sym $_name)'