class RuboCop::Cop::Style::RedundantSelf

This cop checks for redundant uses of `self`.

`self` is only needed when:

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 48
def initialize(config = nil, options = nil)
  super
  @allowed_send_nodes = []
  @local_variables = []
end

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 106
def autocorrect(node)
  receiver, _method_name, *_args = *node
  lambda do |corrector|
    corrector.remove(receiver.source_range)
    corrector.remove(node.loc.dot)
  end
end
on_and_asgn(node)
Alias for: on_or_asgn
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_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 70
def on_def(_node)
  @local_variables = []
end
on_defs(_node) click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 74
def on_defs(_node)
  @local_variables = []
end
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 << lhs
end
on_op_asgn(node) click to toggle source
# File lib/rubocop/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/rubocop/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

Detect offenses

# File lib/rubocop/cop/style/redundant_self.rb, line 93
def on_send(node)
  receiver, method_name, *_args = *node
  return unless receiver && receiver.type == :self
  return if operator?(method_name) ||
            keyword?(method_name) ||
            constant_name?(method_name) ||
            node.asgn_method_call? ||
            @allowed_send_nodes.include?(node) ||
            @local_variables.include?(method_name)

  add_offense(node, :expression)
end

Private Instance Methods

allow_self(node) click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 133
def allow_self(node)
  return unless node.type == :send

  receiver, _method_name, *_args = *node
  @allowed_send_nodes << node if receiver && receiver.type == :self
end
constant_name?(method_name) click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 129
def constant_name?(method_name)
  method_name.match(/^[A-Z]/)
end
keyword?(method_name) click to toggle source
# File lib/rubocop/cop/style/redundant_self.rb, line 121
def keyword?(method_name)
  [: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 116
def on_argument(node)
  name, = *node
  @local_variables << name
end