class RuboCop::Cop::Style::RedundantSelf
This cop checks for redundant uses of `self`.
`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.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Example:
def bar
:baz
end
def foo(bar)
self.bar # resolves name clash with argument
end
def foo2
bar = 1 self.bar # resolves name clash with local variable
end
%w[x y z].select do |bar|
self.bar == bar # resolves name clash with argument of a block
end
-
Calling an attribute writer to prevent an local variable assignment
attr_writer :bar
def foo
self.bar= 1 # Make sure above attr writer is called
end
Special cases:
We allow uses of `self` with operators because it would be awkward otherwise.
Constants
- MSG
Public Class Methods
new(config = nil, options = nil)
click to toggle source
Calls superclass method
RuboCop::Cop::Cop.new
# File lib/rubocop/cop/style/redundant_self.rb, line 51 def initialize(config = nil, options = nil) super @allowed_send_nodes = [] @local_variables_scopes = Hash.new { |hash, key| hash[key] = [] } end
Public Instance Methods
on_args(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 77 def on_args(node) node.children.each { |arg| on_argument(arg) } end
on_block(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 99 def on_block(node) add_scope(node, @local_variables_scopes[node]) end
on_blockarg(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 81 def on_blockarg(node) on_argument(node) end
on_def(node)
click to toggle source
Using self.x to distinguish from local variable x
# File lib/rubocop/cop/style/redundant_self.rb, line 72 def on_def(node) add_scope(node) end
Also aliased as: on_defs
on_lvasgn(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 85 def on_lvasgn(node) lhs, rhs = *node @local_variables_scopes[rhs] << lhs end
on_op_asgn(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 65 def on_op_asgn(node) lhs, _op, _rhs = *node allow_self(lhs) end
on_or_asgn(node)
click to toggle source
Assignment of self.x
# File lib/rubocop/cop/style/redundant_self.rb, line 59 def on_or_asgn(node) lhs, _rhs = *node allow_self(lhs) end
Also aliased as: on_and_asgn
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 90 def on_send(node) return unless node.self_receiver? && regular_method_call?(node) return if node.parent && node.parent.mlhs_type? return if allowed_send_node?(node) add_offense(node) end
Private Instance Methods
add_scope(node, local_variables = [])
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 112 def add_scope(node, local_variables = []) node.descendants.each do |child_node| @local_variables_scopes[child_node] = local_variables end end
allow_self(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 144 def allow_self(node) return unless node.send_type? && node.self_receiver? @allowed_send_nodes << node end
allowed_send_node?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 118 def allowed_send_node?(node) @allowed_send_nodes.include?(node) || @local_variables_scopes[node].include?(node.method_name) end
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 105 def autocorrect(node) lambda do |corrector| corrector.remove(node.receiver.source_range) corrector.remove(node.loc.dot) end end
keyword?(method_name)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 136 def keyword?(method_name) %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].include?(method_name) end
on_argument(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 131 def on_argument(node) name, = *node @local_variables_scopes[node] << name end
regular_method_call?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 123 def regular_method_call?(node) !(operator?(node.method_name) || keyword?(node.method_name) || node.camel_case_method? || node.setter_method? || node.implicit_call?) end