class RuboCop::Cop::Lint::IneffectiveAccessModifier

This cop checks for `private` or `protected` access modifiers which are applied to a singleton method. These access modifiers do not make singleton methods private/protected. `private_class_method` can be used for that.

@example

@bad
class C
  private

  def self.method
    puts 'hi'
  end
end

@good
class C
  def self.method
    puts 'hi'
  end

  private_class_method :method
end

class C
  class << self
    private

    def method
      puts 'hi'
    end
  end
end

Constants

ALTERNATIVE_PRIVATE
ALTERNATIVE_PROTECTED
MSG

Public Instance Methods

on_class(node) click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 55
def on_class(node)
  check_node(node.children[2]) # class body
end
on_module(node) click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 59
def on_module(node)
  check_node(node.children[1]) # module body
end

Private Instance Methods

check_node(node) click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 70
def check_node(node)
  if node && node.begin_type?
    clear
    check_scope(node)

    @useless.each do |_name, (defs_node, visibility, modifier)|
      add_offense(defs_node, :keyword,
                  format_message(visibility, modifier))
    end
  end
end
check_scope(node, cur_vis = :public) click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 92
def check_scope(node, cur_vis = :public)
  node.children.each do |child|
    if (new_vis = access_modifier(child))
      @last_access_modifier = child
      cur_vis = new_vis
    elsif child.defs_type?
      if cur_vis != :public
        _, method_name, = *child
        @useless[method_name] = [child, cur_vis, @last_access_modifier]
      end
    elsif (methods = private_class_method(child))
      # don't warn about defs nodes which are followed by a call to
      # `private_class_method :name`
      # obviously the programmer knows what they are doing
      methods.select(&:sym_type?).each do |sym|
        @useless.delete(sym.children[0])
      end
    elsif child.kwbegin_type?
      cur_vis = check_scope(child, cur_vis)
    end
  end

  cur_vis
end
clear() click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 65
def clear
  @useless = {}
  @last_access_modifier = nil
end
format_message(visibility, modifier) click to toggle source
# File lib/rubocop/cop/lint/ineffective_access_modifier.rb, line 82
def format_message(visibility, modifier)
  alternative = if visibility == :private
                  ALTERNATIVE_PRIVATE
                else
                  ALTERNATIVE_PROTECTED
                end
  format(MSG, visibility, modifier.location.expression.line, visibility,
         alternative)
end