Class: RuboCop::Cop::Style::RedundantStructKeywordInit
- 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.
Constant Summary collapse
- MSG =
'Remove the redundant `keyword_init: %<value>s`.'- RESTRICT_ON_SEND =
i[new].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #keyword_init?(node) ⇒ Object
- #keyword_init_false?(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
- #redundant_keyword_init?(node) ⇒ Object
- #struct_new?(node) ⇒ Object
Methods included from AutoCorrector
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#keyword_init?(node) ⇒ Object
40 41 42 |
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 40 def_node_matcher :keyword_init?, "{#redundant_keyword_init? #keyword_init_false?}\n" |
#keyword_init_false?(node) ⇒ Object
50 51 52 |
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 50 def_node_matcher :keyword_init_false?, "(pair (sym :keyword_init) (false))\n" |
#on_send(node) ⇒ Object Also known as: on_csend
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 54 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
45 46 47 |
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 45 def_node_matcher :redundant_keyword_init?, "(pair (sym :keyword_init) {(true) (nil)})\n" |
#struct_new?(node) ⇒ Object
35 36 37 |
# File 'lib/rubocop/cop/style/redundant_struct_keyword_init.rb', line 35 def_node_matcher :struct_new?, "(call (const {nil? cbase} :Struct) :new ...)\n" |