Class: RuboCop::Cop::Lint::StructNewOverride

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/struct_new_override.rb

Overview

Checks unexpected overrides of the Struct built-in methods via Struct.new.

Examples:

# bad
Bad = Struct.new(:members, :clone, :count)
b = Bad.new([], true, 1)
b.members #=> [] (overriding `Struct#members`)
b.clone #=> true (overriding `Object#clone`)
b.count #=> 1 (overriding `Enumerable#count`)

# good
Good = Struct.new(:id, :name)
g = Good.new(1, "foo")
g.members #=> [:id, :name]
g.clone #=> #<struct Good id=1, name="foo">
g.count #=> 2

Constant Summary collapse

MSG =
'`%<member_name>s` member overrides `Struct#%<method_name>s` ' \
'and it may be unexpected.'
RESTRICT_ON_SEND =
i[new].freeze
STRUCT_METHOD_NAMES =

This is based on Struct.instance_methods.sort in Ruby 4.0.0.

i[
  ! != !~ <=> == === [] []= __id__ __send__ all? any? chain chunk chunk_while class clone
  collect collect_concat compact count cycle deconstruct deconstruct_keys
  define_singleton_method detect dig display drop drop_while dup each each_cons each_entry
  each_pair each_slice each_with_index each_with_object entries enum_for eql? equal? extend
  filter filter_map find find_all find_index first flat_map freeze frozen? grep grep_v
  group_by hash include? inject inspect instance_eval instance_exec instance_of?
  instance_variable_defined? instance_variable_get instance_variable_set instance_variables
  is_a? itself kind_of? lazy length map max max_by member? members method methods
  min min_by minmax minmax_by nil? none? object_id one? partition private_methods
  protected_methods public_method public_methods public_send reduce reject
  remove_instance_variable respond_to? reverse_each select send singleton_class
  singleton_method singleton_methods size slice_after slice_before slice_when sort sort_by
  sum take take_while tally tap then to_a to_enum to_h to_s to_set uniq values values_at
  yield_self zip
].freeze
STRUCT_MEMBER_NAME_TYPES =
i[sym str].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/lint/struct_new_override.rb', line 54

def on_send(node)
  return unless struct_new(node)

  node.arguments.each_with_index do |arg, index|
    # Ignore if the first argument is a class name
    next if index.zero? && arg.str_type?

    # Ignore if the argument is not a member name
    next unless STRUCT_MEMBER_NAME_TYPES.include?(arg.type)

    member_name = arg.value

    next unless STRUCT_METHOD_NAMES.include?(member_name.to_sym)

    message = format(MSG, member_name: member_name.inspect, method_name: member_name.to_s)
    add_offense(arg, message: message)
  end
end

#struct_new(node) ⇒ Object



49
50
51
52
# File 'lib/rubocop/cop/lint/struct_new_override.rb', line 49

def_node_matcher :struct_new, "(send\n  (const {nil? cbase} :Struct) :new ...)\n"