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

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

Overview

This cop 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

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].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #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, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #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.


59
60
61
62
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 59

def initialize(config = nil, options = nil)
  super
  @definitions = {}
end

Instance Method Details

#on_alias(node) ⇒ Object


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

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

  found_instance_method(node, name)
end

#on_def(node) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 64

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

  found_instance_method(node, node.method_name)
end

#on_defs(node) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubocop/cop/lint/duplicate_methods.rb', line 73

def on_defs(node)
  return if node.ancestors.any?(&:if_type?)
  return if possible_dsl?(node)

  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


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

def on_send(node)
  if (name = alias_method?(node))
    return if node.ancestors.any?(&:if_type?)
    return if possible_dsl?(node)

    found_instance_method(node, name)
  elsif (attr = node.attribute_accessor?)
    on_attr(node, *attr)
  end
end