class RuboCop::Cop::Lint::UselessDefaultValueArgument

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.

@safety

This cop is unsafe because the receiver could have nonstandard implementation
of `fetch`, or be a class other than the one listed above.

It is also unsafe because default value argument could have side effects:

[source,ruby]
----
def x(a) = puts "side effect"
Array.new(5, x(1)) { 2 }
----

so removing it would change behavior.

@example

# bad
x.fetch(key, default_value) { block_value }
Array.new(size, default_value) { block_value }

# good
x.fetch(key) { block_value }
Array.new(size) { block_value }

# also good - in case default value argument is desired instead
x.fetch(key, default_value)
Array.new(size, default_value)

# good - keyword arguments aren't registered as offenses
x.fetch(key, keyword: :arg) { block_value }

@example AllowedReceivers: [‘Rails.cache’]

# good
Rails.cache.fetch(name, options) { block }