Class: RuboCop::Cop::Style::RedundantSelf

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/redundant_self.rb

Overview

This cop checks for redundant uses of `self`.

The usage of `self` is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of `self` with operators because it would be awkward otherwise.

Examples:


# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Constant Summary collapse

MSG =
'Redundant `self` detected.'
KERNEL_METHODS =
Kernel.methods(false)
KEYWORDS =
%i[alias and begin break case class def defined? do
else elsif end ensure false for if in module
next nil not or redo rescue retry return self
super then true undef unless until when while
yield __FILE__ __LINE__ __ENCODING__].freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, 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

add_parentheses, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, indent, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Constructor Details

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

Returns a new instance of RedundantSelf.


59
60
61
62
63
# File 'lib/rubocop/cop/style/redundant_self.rb', line 59

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

Class Method Details

.autocorrect_incompatible_withObject


55
56
57
# File 'lib/rubocop/cop/style/redundant_self.rb', line 55

def self.autocorrect_incompatible_with
  [ColonMethodCall]
end

Instance Method Details

#on_args(node) ⇒ Object


85
86
87
# File 'lib/rubocop/cop/style/redundant_self.rb', line 85

def on_args(node)
  node.children.each { |arg| on_argument(arg) }
end

#on_block(node) ⇒ Object


117
118
119
# File 'lib/rubocop/cop/style/redundant_self.rb', line 117

def on_block(node)
  add_scope(node, @local_variables_scopes[node])
end

#on_blockarg(node) ⇒ Object


89
90
91
# File 'lib/rubocop/cop/style/redundant_self.rb', line 89

def on_blockarg(node)
  on_argument(node)
end

#on_def(node) ⇒ Object Also known as: on_defs

Using self.x to distinguish from local variable x


80
81
82
# File 'lib/rubocop/cop/style/redundant_self.rb', line 80

def on_def(node)
  add_scope(node)
end

#on_lvasgn(node) ⇒ Object


100
101
102
103
# File 'lib/rubocop/cop/style/redundant_self.rb', line 100

def on_lvasgn(node)
  lhs, rhs = *node
  add_lhs_to_local_variables_scopes(rhs, lhs)
end

#on_masgn(node) ⇒ Object


93
94
95
96
97
98
# File 'lib/rubocop/cop/style/redundant_self.rb', line 93

def on_masgn(node)
  lhs, rhs = *node
  lhs.children.each do |child|
    add_lhs_to_local_variables_scopes(rhs, child.to_a.first)
  end
end

#on_op_asgn(node) ⇒ Object


73
74
75
76
# File 'lib/rubocop/cop/style/redundant_self.rb', line 73

def on_op_asgn(node)
  lhs, _op, _rhs = *node
  allow_self(lhs)
end

#on_or_asgn(node) ⇒ Object Also known as: on_and_asgn

Assignment of self.x


67
68
69
70
# File 'lib/rubocop/cop/style/redundant_self.rb', line 67

def on_or_asgn(node)
  lhs, _rhs = *node
  allow_self(lhs)
end

#on_send(node) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubocop/cop/style/redundant_self.rb', line 105

def on_send(node)
  return unless node.self_receiver? && regular_method_call?(node)
  return if node.parent&.mlhs_type?

  return if allowed_send_node?(node)

  add_offense(node) do |corrector|
    corrector.remove(node.receiver)
    corrector.remove(node.loc.dot)
  end
end