class RuboCop::Cop::Style::RedundantSelf
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.
@example
# 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
Constants
- KERNEL_METHODS
- MSG
Public Class Methods
autocorrect_incompatible_with()
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 48 def self.autocorrect_incompatible_with [ColonMethodCall] end
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 52 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/rubocop/cop/style/redundant_self.rb, line 104 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/rubocop/cop/style/redundant_self.rb, line 78 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 100 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 82 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 73 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 86 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 66 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 60 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 91 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) end
Private Instance Methods
add_scope(node, local_variables = [])
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 113 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 146 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 119 def allowed_send_node?(node) @allowed_send_nodes.include?(node) || @local_variables_scopes[node].include?(node.method_name) || KERNEL_METHODS.include?(node.method_name) end
keyword?(method_name)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 138 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 __FILE__ __LINE__ __ENCODING__].include?(method_name) end
on_argument(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 133 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 125 def regular_method_call?(node) !(node.operator_method? || keyword?(node.method_name) || node.camel_case_method? || node.setter_method? || node.implicit_call?) end