Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new `Style/ExplicitBlockArgument` cop #8415

Merged
merged 1 commit into from Aug 1, 2020

Conversation

@fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Jul 30, 2020

https://rubystyle.guide/#block-argument

Consider using explicit block argument to avoid writing block literal that just passes its arguments to another block.

require 'tempfile'

# bad
def with_tmp_dir
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir) { |dir| yield dir }  # block just passes arguments
  end
end

# good
def with_tmp_dir(&block)
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir, &block)
  end
end

with_tmp_dir do |dir|
  puts "dir is accessible as a parameter and pwd is set: #{dir}"
end
include RangeHelp
extend AutoCorrector

MSG = 'Consider using explicit block argument.'

This comment has been minimized.

@bbatsov

bbatsov Jul 31, 2020
Collaborator

I think this message is a bit confusing, as it's not very clear where is this argument supposed to be. Perhaps something like "Consider using explicit block argument in the surrounding method's signature over yield." or something along those lines.

This comment has been minimized.

@fatkodima

fatkodima Jul 31, 2020
Author Contributor

Corrected.

@fatkodima fatkodima force-pushed the fatkodima:explicit_block_argument-cop branch from e52c8df to 5f02f80 Jul 31, 2020
@marcandre
Copy link
Contributor

@marcandre marcandre commented Jul 31, 2020

Please mark as unsafe (as it may change the yielding arity)

@fatkodima fatkodima force-pushed the fatkodima:explicit_block_argument-cop branch from 5f02f80 to a591ac2 Jul 31, 2020
@bbatsov
Copy link
Collaborator

@bbatsov bbatsov commented Aug 1, 2020

Btw, I've been thinking it'd be great if we started to add to the cop descriptions some explanations as to what exactly makes them unsafe so it's more obvious for the users.

@fatkodima
Copy link
Contributor Author

@fatkodima fatkodima commented Aug 1, 2020

Yes, I have the same thought and already added a line about this ))

@bbatsov bbatsov merged commit e1c057d into rubocop-hq:master Aug 1, 2020
26 checks passed
26 checks passed
windows 2.4
Details
windows 2.5
Details
windows 2.6
Details
windows 2.7
Details
windows mingw
Details
ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
@bbatsov
Copy link
Collaborator

@bbatsov bbatsov commented Aug 1, 2020

Thanks!

@marcandre
Copy link
Contributor

@marcandre marcandre commented Aug 1, 2020

Btw, I've been thinking it'd be great if we started to add to the cop descriptions some explanations as to what exactly makes them unsafe so it's more obvious for the users.

Yes! With an example too...

and already added a line about this

I don't see it Oh, I see it. We need it to show in the doc and an example would help.

@bbatsov
Copy link
Collaborator

@bbatsov bbatsov commented Aug 1, 2020

Going forward I think we should also add such explanation and examples to the cop description from which the documentation is generated.

@marcandre
Copy link
Contributor

@marcandre marcandre commented Aug 1, 2020

I created #8431

koic added a commit to rubocop-hq/rubocop-rails that referenced this pull request Aug 2, 2020
Follow rubocop-hq/rubocop#8415

This commit suppresses the following `Style/ExplicitBlockArgument`'s
offenses.

```console
% bundle exec rake
(snip)

Offenses:

lib/rubocop/rails/schema_loader/schema.rb:105:13: C:
Style/ExplicitBlockArgument: Consider using explicit block argument in
the surrounding method's signature over yield.
            node.body.children.each do |child| ...
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

181 files inspected, 1 offense detected
RuboCop failed!
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.