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

EmptyConditionalBody AllowedComments doesn't work for if/unless correctly #12279

Closed
matthewhively opened this issue Oct 13, 2023 · 0 comments · Fixed by #12281
Closed

EmptyConditionalBody AllowedComments doesn't work for if/unless correctly #12279

matthewhively opened this issue Oct 13, 2023 · 0 comments · Fixed by #12281
Labels

Comments

@matthewhively
Copy link

Expected behavior

As documented here: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/EmptyConditionalBody
Checks for the presence of ‘if`, `elsif` and `unless` branches without a body.
Expect AllowComments to work for both if/unless and elsif blocks

Actual behavior

AllowComments works for if/unless blocks only when there is no elsif/else and code after the block ends at the same nesting level.
AllowComments always works of elsif` blocks

Steps to reproduce the problem

Based on documentation all of these should be allowed when AllowComments: true
test code:

module Something

  def method1(foobar, barfoo)
    puts 'lets do something'
    if foobar # 1
      puts 'testing the conditional'
    end
    unless barfoo # 1
      # no error
    end
    puts 'foobar'
  end

  def method2(foobar, barfoo)
    puts 'lets do something'
    if foobar # 2
      puts 'testing the conditional'
    end
    unless barfoo # 2
      # issue found!
    end
    #puts 'foobar'
  end

  def method3(foobar, barfoo)
    puts 'lets do something'
    if foobar # 3
      # no error
    end
    unless barfoo # 3
      # issue found!
    end
  end

  def method4(foobar)
    puts 'lets do something'
    if foobar # 4
      # no error
    end
    puts 'foobar'
  end

  def method5(foobar)
    puts 'lets do something'
    if foobar # 5
      # issue found!
    end
  end

  def method6(foobar, barfoo)
    puts 'lets do something'
    if foobar # 6
      # no error
    elsif barfoo # 6
      # no error
    end
  end

  def method7(foobar)
    puts 'lets do something'
    if foobar # 7
      # no error
    else # 7
      puts 'hello'
    end
  end

  def method8(foobar, barfoo)
    puts 'lets do something'
    if foobar # 8
      puts 'foobar'
      if barfoo # 8
        # issue found!
      end
    end
    puts 'hello'
  end

  def method9(foobar, barfoo)
    puts 'lets do something'
    if foobar # 9
      puts 'foobar'
      if barfoo # 9
        # no error
      end
      puts 'hello'
    end
  end

end

test config:

require:
  - rubocop-performance
  - rubocop-rails

AllCops:
  TargetRubyVersion: 2.7

Lint/EmptyConditionalBody:
  AllowComments: true
  Enabled: true

test command and output:

rubocop --only Lint/EmptyConditionalBody test.rb
Inspecting 1 file
W

Offenses:

test.rb:19:5: W: [Correctable] Lint/EmptyConditionalBody: Avoid unless branches without a body.
    unless barfoo # 2 ...
    ^^^^^^^^^^^^^^^^^
test.rb:30:5: W: [Correctable] Lint/EmptyConditionalBody: Avoid unless branches without a body.
    unless barfoo # 3 ...
    ^^^^^^^^^^^^^^^^^
test.rb:45:5: W: [Correctable] Lint/EmptyConditionalBody: Avoid if branches without a body.
    if foobar # 5 ...
    ^^^^^^^^^^^^^
test.rb:72:7: W: [Correctable] Lint/EmptyConditionalBody: Avoid if branches without a body.
      if barfoo # 8 ...
      ^^^^^^^^^^^^^

There may be more weird edge cases, but this is as far down this rabbit-hole as I choose to go for now.

RuboCop version

rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 2.7.8) [x86_64-darwin21]
  - rubocop-performance 1.19.1
  - rubocop-rails 2.21.2
@koic koic added the bug label Oct 13, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 13, 2023
Fixes rubocop#12279.

This PR fixes false positives for `Lint/EmptyConditionalBody`
when missing 2nd `if` body with a comment.
bbatsov pushed a commit that referenced this issue Oct 14, 2023
Fixes #12279.

This PR fixes false positives for `Lint/EmptyConditionalBody`
when missing 2nd `if` body with a comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants