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

Lint/EmptyConditionalBody cop throws an error when inspecting a conditional with multiple empty branches #10514

Closed
davidenglishmusic opened this issue Apr 8, 2022 · 0 comments · Fixed by #10516
Labels

Comments

@davidenglishmusic
Copy link

The cop throws an error when inspecting a conditional with more than one empty branch.


Expected behavior

It should not throw an error.

Actual behavior

It throws an error.

An error occurred while Lint/EmptyConditionalBody cop was inspecting /Users/david/code/rubocop-test/app.rb:7:0.
undefined method `line' for nil:NilClass
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/mixin/comments_help.rb:55:in `find_end_line'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/mixin/comments_help.rb:16:in `contains_comments?'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/lint/empty_conditional_body.rb:62:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:98:in `each'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:69:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:156:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:156:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:309:in `inspect_file'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:280:in `loop'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:249:in `do_inspection_loop'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:129:in `file_offenses'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:67:in `block in warm_cache'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:587:in `call_with_index'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:557:in `process_incoming_jobs'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:537:in `block in worker'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:528:in `fork'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:528:in `worker'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:519:in `block in create_workers'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:518:in `each'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:518:in `each_with_index'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:518:in `create_workers'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:457:in `work_in_processes'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:294:in `map'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/parallel-1.22.1/lib/parallel.rb:238:in `each'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:67:in `warm_cache'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/runner.rb:46:in `run'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/command.rb:11:in `run'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli/environment.rb:18:in `run'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli.rb:71:in `run_command'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli.rb:78:in `execute_runners'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/lib/rubocop/cli.rb:47:in `run'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/exe/rubocop:12:in `block in <top (required)>'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/Users/david/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.27.0/exe/rubocop:12:in `<top (required)>'
/Users/david/.rbenv/versions/2.6.6/bin/rubocop:23:in `load'
/Users/david/.rbenv/versions/2.6.6/bin/rubocop:23:in `<main>'

Steps to reproduce the problem

Create app.rb with the following code:

# frozen_string_literal: true

if condition_a
  puts 'hello'
elsif condition_b
  # Do nothing
elsif condition_c
  # Do nothing
end

Run rubocop app.rb

RuboCop version

% rubocop -V     
1.27.0 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 2.6.6 -darwin20)
@koic koic added the bug label Apr 9, 2022
koic added a commit to koic/rubocop that referenced this issue Apr 9, 2022
Fixes rubocop#10514.

This PR fixes an error for `Lint/EmptyConditionalBody`
when missing second `elsif` body.
bbatsov pushed a commit that referenced this issue Apr 10, 2022
Fixes #10514.

This PR fixes an error for `Lint/EmptyConditionalBody`
when missing second `elsif` body.
@koic koic pinned this issue Apr 14, 2022
@koic koic unpinned this issue Apr 22, 2022
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