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

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

Overview

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 a 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. Also allows the use of self.it without arguments in blocks, as in 0.times { self.it }, following Lint/ItWithoutArgumentsInBlock cop.

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

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

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

silence_warnings

Constructor Details

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

Returns a new instance of RedundantSelf.



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

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

Class Method Details

.autocorrect_incompatible_withObject



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

def self.autocorrect_incompatible_with
  [ColonMethodCall, Layout::DotPosition]
end

Instance Method Details

#on_args(node) ⇒ Object



87
88
89
# File 'lib/rubocop/cop/style/redundant_self.rb', line 87

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

#on_block(node) ⇒ Object Also known as: on_numblock, on_itblock



128
129
130
# File 'lib/rubocop/cop/style/redundant_self.rb', line 128

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

#on_blockarg(node) ⇒ Object



91
92
93
# File 'lib/rubocop/cop/style/redundant_self.rb', line 91

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



82
83
84
# File 'lib/rubocop/cop/style/redundant_self.rb', line 82

def on_def(node)
  add_scope(node)
end

#on_if(node) ⇒ Object Also known as: on_while, on_until



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubocop/cop/style/redundant_self.rb', line 135

def on_if(node)
  # Allow conditional nodes to use `self` in the condition if that variable
  # name is used in an `lvasgn` or `masgn` within the `if`.
  node.each_descendant(:lvasgn, :masgn) do |descendant_node|
    if descendant_node.lvasgn_type?
      add_lhs_to_local_variables_scopes(node.condition, descendant_node.lhs)
    else
      add_masgn_lhs_variables(node.condition, descendant_node.lhs)
    end
  end
end

#on_in_pattern(node) ⇒ Object



112
113
114
# File 'lib/rubocop/cop/style/redundant_self.rb', line 112

def on_in_pattern(node)
  add_match_var_scopes(node)
end

#on_lvasgn(node) ⇒ Object



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

def on_lvasgn(node)
  add_lhs_to_local_variables_scopes(node.rhs, node.lhs)
end

#on_masgn(node) ⇒ Object



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

def on_masgn(node)
  add_masgn_lhs_variables(node.rhs, node.lhs)
end

#on_op_asgn(node) ⇒ Object



76
77
78
# File 'lib/rubocop/cop/style/redundant_self.rb', line 76

def on_op_asgn(node)
  allow_self(node.lhs)
end

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

Assignment of self.x



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

def on_or_asgn(node)
  allow_self(node.lhs)

  lhs_name = node.lhs.lvasgn_type? ? node.lhs.name : node.lhs
  add_lhs_to_local_variables_scopes(node.rhs, lhs_name)
end

#on_resbody(node) ⇒ Object

Register the exception variable of rescue => e so that self.e in the body is not treated as redundant (it disambiguates the local variable).



105
106
107
108
109
110
# File 'lib/rubocop/cop/style/redundant_self.rb', line 105

def on_resbody(node)
  exception_variable = node.exception_variable
  return unless exception_variable&.lvasgn_type?

  @local_variables_scopes[node] << exception_variable.name
end

#on_send(node) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubocop/cop/style/redundant_self.rb', line 116

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)
  return if it_method_in_block?(node)

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