Class: RuboCop::Cop::Lint::UselessDefaultValueArgument
- Extended by:
- AutoCorrector
- Includes:
- AllowedReceivers
- Defined in:
- lib/rubocop/cop/lint/useless_default_value_argument.rb
Overview
Checks for usage of method ‘fetch` or `Array.new` with default value argument and block. In such cases, block will always be used as default value.
This cop emulates Ruby warning “block supersedes default value argument” which applies to ‘Array.new`, `Array#fetch`, `Hash#fetch`, `ENV.fetch` and `Thread#fetch`.
A ‘fetch` call without a receiver is considered a custom method and does not register an offense.
Constant Summary collapse
- MSG =
'Block supersedes default value argument.'
- RESTRICT_ON_SEND =
%i[fetch new].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #default_value_argument_and_block(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
Methods included from AutoCorrector
Methods included from AllowedReceivers
#allowed_receiver?, #allowed_receivers, #receiver_name
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
#default_value_argument_and_block(node) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/lint/useless_default_value_argument.rb', line 59 def_node_matcher :default_value_argument_and_block, <<~PATTERN (any_block { (call !nil? :fetch $_key $_default_value) (send (const _ :Array) :new $_size $_default_value) } _args _block_body) PATTERN |
#on_send(node) ⇒ Object Also known as: on_csend
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubocop/cop/lint/useless_default_value_argument.rb', line 69 def on_send(node) unless (prev_arg_node, default_value_node = default_value_argument_and_block(node.parent)) return end return if allowed_receiver?(node.receiver) return if hash_without_braces?(default_value_node) add_offense(default_value_node) do |corrector| corrector.remove(prev_arg_node.source_range.end.join(default_value_node.source_range)) end end |