class RuboCop::Cop::Lint::UselessDefaultValueArgument
Rails.cache.fetch(name, options) { block }
# good
@example AllowedReceivers: [‘Rails.cache’]
x.fetch(key, keyword: :arg) { block_value }
# good - keyword arguments aren’t registered as offenses
Array.new(size, default_value)
x.fetch(key, default_value)
# also good - in case default value argument is desired instead
Array.new(size) { block_value }
x.fetch(key) { block_value }
# good
Array.new(size, default_value) { block_value }
x.fetch(key, default_value) { block_value }
# bad
@example
so removing it would change behavior.
—-
Array.new(5, x(1)) { 2 }
def x(a) = puts “side effect”
—-
[source,ruby]
It is also unsafe because default value argument could have side effects:
of ‘fetch`, or be a class other than the one listed above.
This cop is unsafe because the receiver could have nonstandard implementation
@safety
an offense.
A `fetch` call without a receiver is considered a custom method and does not register
`Thread#fetch`.
applies to `Array.new`, `Array#fetch`, `Hash#fetch`, `ENV.fetch` and
This cop emulates Ruby warning “block supersedes default value argument” which
and block. In such cases, block will always be used as default value.
Checks for usage of method `fetch` or `Array.new` with default value argument
def hash_without_braces?(node)
def hash_without_braces?(node) node.hash_type? && !node.braces? end
def on_send(node)
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