class RubbyCop::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
-
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
RubbyCop::Cop::Cop::new
# File lib/rubbycop/cop/style/redundant_self.rb, line 48 def initialize(config = nil, options = nil) super @allowed_send_nodes = [] @local_variables_scopes = Hash.new { |hash, key| hash[key] = [] } end
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 99 def autocorrect(node) lambda do |corrector| corrector.remove(node.receiver.source_range) corrector.remove(node.loc.dot) end end
on_args(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 76 def on_args(node) node.children.each { |arg| on_argument(arg) } end
on_blockarg(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 80 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/rubbycop/cop/style/redundant_self.rb, line 70 def on_def(node) add_scope(node) end
Also aliased as: on_defs
on_lvasgn(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 84 def on_lvasgn(node) lhs, rhs = *node @local_variables_scopes[rhs] << lhs end
on_op_asgn(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 63 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/rubbycop/cop/style/redundant_self.rb, line 56 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/rubbycop/cop/style/redundant_self.rb, line 89 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_nodes.include?(node) || @local_variables_scopes[node].include?(node.method_name) add_offense(node, :expression) end
Private Instance Methods
add_scope(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 108 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/rubbycop/cop/style/redundant_self.rb, line 136 def allow_self(node) return unless node.send_type? && node.self_receiver? @allowed_send_nodes << node end
keyword?(method_name)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 128 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/rubbycop/cop/style/redundant_self.rb, line 123 def on_argument(node) name, = *node @local_variables_scopes[node] << name end
regular_method_call?(node)
click to toggle source
# File lib/rubbycop/cop/style/redundant_self.rb, line 115 def regular_method_call?(node) !(operator?(node.method_name) || keyword?(node.method_name) || node.camel_case_method? || node.setter_method? || node.implicit_call?) end