Class: RuboCop::Cop::Style::ArgumentsForwarding
- Extended by:
- AutoCorrector, TargetRubyVersion
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/style/arguments_forwarding.rb
Overview
In Ruby 2.7, arguments forwarding has been added.
This cop identifies places where ‘do_something(*args, &block)` can be replaced by `do_something(…)`.
In Ruby 3.1, anonymous block forwarding has been added.
This cop identifies places where ‘do_something(&block)` can be replaced by `do_something(&)`; if desired, this functionality can be disabled by setting `UseAnonymousForwarding: false`.
In Ruby 3.2, anonymous args/kwargs forwarding has been added.
This cop also identifies places where ‘use_args(*args)`/`use_kwargs(**kwargs)` can be replaced by `use_args(*)`/`use_kwargs(**)`; if desired, this functionality can be disabled by setting `UseAnonymousForwarding: false`.
And this cop has ‘RedundantRestArgumentNames`, `RedundantKeywordRestArgumentNames`, and `RedundantBlockArgumentNames` options. This configuration is a list of redundant names that are sufficient for anonymizing meaningless naming.
Meaningless names that are commonly used can be anonymized by default: e.g., ‘*args`, `**options`, `&block`, and so on.
Names not on this list are likely to be meaningful and are allowed by default.
This cop handles not only method forwarding but also forwarding to ‘super`.
Defined Under Namespace
Classes: SendNodeClassifier
Constant Summary collapse
- FORWARDING_LVAR_TYPES =
%i[splat kwsplat block_pass].freeze
- ADDITIONAL_ARG_TYPES =
%i[lvar arg].freeze
- FORWARDING_MSG =
'Use shorthand syntax `...` for arguments forwarding.'
- ARGS_MSG =
'Use anonymous positional arguments forwarding (`*`).'
- KWARGS_MSG =
'Use anonymous keyword arguments forwarding (`**`).'
- BLOCK_MSG =
'Use anonymous block arguments forwarding (`&`).'
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Methods included from AutoCorrector
Methods included from TargetRubyVersion
minimum_target_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?, 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_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
Class Method Details
.autocorrect_incompatible_with ⇒ Object
142 143 144 |
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 142 def self.autocorrect_incompatible_with [Naming::BlockForwarding] end |
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 146 def on_def(node) return unless node.body restarg, kwrestarg, blockarg = extract_forwardable_args(node.arguments) forwardable_args = redundant_forwardable_named_args(restarg, kwrestarg, blockarg) send_nodes = node.each_descendant(:send, :csend, :super, :yield).to_a send_classifications = classify_send_nodes( node, send_nodes, non_splat_or_block_pass_lvar_references(node.body), forwardable_args ) return if send_classifications.empty? if only_forwards_all?(send_classifications) add_forward_all_offenses(node, send_classifications, forwardable_args) elsif target_ruby_version >= 3.2 add_post_ruby_32_offenses(node, send_classifications, forwardable_args) end end |