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

Void context in block not detected if block only contains one line #5551

Closed
untitaker opened this issue Feb 7, 2018 · 1 comment
Closed
Labels

Comments

@untitaker
Copy link
Contributor

untitaker commented Feb 7, 2018

Consider this code:

array.each do |_item|
  42
end

Expected behavior

Rubocop should emit a Lint/Void for the second line.

Actual behavior

Rubocop considers this code valid.

RuboCop version

master, or 0.52

Notes

The following code is linted as expected, until the second int literal is removed:

array.each do |_item|
  42
  42
end
@pocke
Copy link
Collaborator

pocke commented Feb 13, 2018

array.each do |_item|
  42
end

array.each do |_item|
  42
  42
end
$ ruby-parse test.rb
(begin
  (block
    (send
      (send nil :array) :each)
    (args
      (procarg0 :_item))
    (int 42))
  (block
    (send
      (send nil :array) :each)
    (args
      (procarg0 :_item))
    (begin
      (int 42)
      (int 42))))

When a block body has one sentence, it does not have a begin node. The cop's checks begin and kwbegin node only, so the cop can't catch it now.
https://github.com/bbatsov/rubocop/blob/031b29b495ad435dc69172fb7603042894faae53/lib/rubocop/cop/lint/void.rb#L82-L85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants