Class: RuboCop::Cop::Style::RedundantStructKeywordInit

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Defined in:
lib/rubocop/cop/style/redundant_struct_keyword_init.rb

Overview

Checks for redundant ‘keyword_init` option for `Struct.new`.

Since Ruby 3.2, ‘keyword_init` in `Struct.new` defaults to `nil` behavior. Therefore, this cop detects and autocorrects redundant `keyword_init: nil` and `keyword_init: true` in `Struct.new`.

This cop is disabled by default because ‘keyword_init: true` is not purely redundant. It changes behavior in the following ways:

  • ‘Struct#keyword_init?` returns `true` instead of `nil`.

  • A ‘Struct` with `keyword_init: true` accepts a `Hash` argument and expands it as keyword arguments, whereas without it the `Hash` is treated as a positional argument.

  • ‘keyword_init: true` raises an `ArgumentError` for positional arguments, enforcing keyword-only initialization.

Examples:


# bad
Struct.new(:foo, keyword_init: nil)
Struct.new(:foo, keyword_init: true)

# good
Struct.new(:foo)

Constant Summary collapse

MSG =
'Remove the redundant `keyword_init: %<value>s`.'
RESTRICT_ON_SEND =
%i[new].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

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

cop_dir_for, #exclude_limit, read_limits

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

#keyword_init?(node) ⇒ Object



50
51
52
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 50

def_node_matcher :keyword_init?, <<~PATTERN
  {#redundant_keyword_init? #keyword_init_false?}
PATTERN

#keyword_init_false?(node) ⇒ Object



60
61
62
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 60

def_node_matcher :keyword_init_false?, <<~PATTERN
  (pair (sym :keyword_init) (false))
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 64

def on_send(node)
  return if !struct_new?(node) || node.arguments.none? || !node.last_argument.hash_type?

  keyword_init_nodes = select_keyword_init_nodes(node)
  return if keyword_init_nodes.any? { |node| keyword_init_false?(node) }

  redundant_keyword_init_nodes = select_redundant_keyword_init_nodes(keyword_init_nodes)

  redundant_keyword_init_nodes.each do |redundant_keyword_init|
    register_offense(redundant_keyword_init)
  end
end

#redundant_keyword_init?(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 55

def_node_matcher :redundant_keyword_init?, <<~PATTERN
  (pair (sym :keyword_init) {(true) (nil)})
PATTERN

#struct_new?(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 45

def_node_matcher :struct_new?, <<~PATTERN
  (call (const {nil? cbase} :Struct) :new ...)
PATTERN