Rubocop: Thinks block is unused

Created on 16 Aug 2014  路  6Comments  路  Source: rubocop-hq/rubocop

lib/tasks/generate_client.rake:20:19: W: [Corrected] Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as run(*) if you want the method to accept any arguments but don't care about them.
    def self.run(&block)
                  ^^^^^

The code is:

    def self.run(&block)
      client = new
      yield(client) if block_given?
      client.run
    end

and rubocop corrects it to:

    def self.run(&_block)
      client = new
      yield(client) if block_given?
      client.run
    end

It's used in yield and block_given?

Why am I adding (&block) anyway?
This allows me to see immediately, that this method takes a block.

Most helpful comment

I, for one, would like to see Rubocop accept &block in this case. (I recognize that the _variable_ is unused but the argument certainly is.)

The style guide uses it liberally. It's a convention, and what are many of Rubocop's cops if not enforced conventions?

All 6 comments

RuboCop's behavior here seems perfectly fine to me. You are in fact not using the block argument. You're calling the block, but the variable could have been named anything. It's not accessed.

If you really dislike the added underscore, perhaps you'd like to change the condition to

yield(client) if block

I think your method will work the same if you don't specify the block parameter in the signature.

All ruby methods always take an optional block. You can yield and ask if block_given? without naming the block.

I also think that there's no problem with the current behavior. Unless you're going to call the block, there's little point in having it as an argument in the first place.

I, for one, would like to see Rubocop accept &block in this case. (I recognize that the _variable_ is unused but the argument certainly is.)

The style guide uses it liberally. It's a convention, and what are many of Rubocop's cops if not enforced conventions?

I don't see an unused &block arg in https://github.com/bbatsov/ruby-style-guide. Do you mean a different style guide?

You're right I suppose, that all instances of &block in the style guide (except one "bad" example) are eventually used. I meant that the style guide uses the conventional &block name.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikegee picture mikegee  路  3Comments

herwinw picture herwinw  路  3Comments

bbatsov picture bbatsov  路  3Comments

kirrmann picture kirrmann  路  3Comments

bbugh picture bbugh  路  3Comments