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 =
Struct.instance_methods 
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

 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# File 'lib/rubocop/cop/lint/struct_new_override.rb', line 38 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

 33 34 35 36
# File 'lib/rubocop/cop/lint/struct_new_override.rb', line 33 def_node_matcher :struct_new, "(send\n (const {nil? cbase} :Struct) :new ...)\n"