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

Style/IfInsideElse error: undefined method `last_line' for nil:NilClass #9333

Closed
rc-tom opened this issue Jan 4, 2021 · 1 comment · Fixed by #9334
Closed

Style/IfInsideElse error: undefined method `last_line' for nil:NilClass #9333

rc-tom opened this issue Jan 4, 2021 · 1 comment · Fixed by #9334
Labels

Comments

@rc-tom
Copy link

rc-tom commented Jan 4, 2021

When linting my code, Rubocop reports that an error occurred in the Style/IfInsideElse cop.


Expected behavior

I expect Rubocop to not produce an error.

Actual behavior

An error occurs.

Debug backtrace
> bundle exec rubocop -d tester.rb

warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.8-compliant syntax, but you are running 2.5.7.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
For /Users/tom/Projects/myproject: configuration from /Users/tom/Projects/myproject/.rubocop.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-rake-0.5.1/config/default.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-rake-0.5.1/config/default.yml
Default configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/config/default.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-rspec-2.1.0/config/default.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-rspec-2.1.0/config/default.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-performance-1.9.2/config/default.yml
configuration from /Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-performance-1.9.2/config/default.yml
Inheriting configuration from /Users/tom/Projects/myproject/.rubocop_todo.yml
Inspecting 1 file
Scanning /Users/tom/Projects/myproject/tester.rb
An error occurred while Style/IfInsideElse cop was inspecting /Users/tom/Projects/myproject/tester.rb:3:0.
undefined method `last_line' for nil:NilClass
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/mixin/range_help.rb:72:in `range_by_whole_lines'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/style/if_inside_else.rb:90:in `autocorrect'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/style/if_inside_else.rb:76:in `block in on_if'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/base.rb:339:in `correct'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/base.rb:126:in `add_offense'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/style/if_inside_else.rb:75:in `on_if'
org/jruby/RubyKernel.java:1960:in `public_send'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:100:in `block in trigger_responding_cops'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:166:in `with_cop_error_handling'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
org/jruby/RubyArray.java:1809:in `each'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:69:in `on_if'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:157:in `on_if'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/team.rb:157:in `investigate_partial'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:315:in `inspect_file'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:259:in `block in do_inspection_loop'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:293:in `block in iterate_until_no_changes'
org/jruby/RubyKernel.java:1442:in `loop'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:286:in `iterate_until_no_changes'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:255:in `do_inspection_loop'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:132:in `block in file_offenses'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:157:in `file_offense_cache'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:131:in `file_offenses'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:122:in `process_file'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
org/jruby/RubyArray.java:1809:in `each'
org/jruby/RubyEnumerable.java:1126:in `inject'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:86:in `inspect_files'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/runner.rb:47:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/command.rb:11:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli/environment.rb:18:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli.rb:65:in `run_command'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/lib/rubocop/cli.rb:41:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/exe/rubocop:13:in `block in <main>'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/stdlib/benchmark.rb:308:in `realtime'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/rubocop-1.7.0/exe/rubocop:12:in `<main>'
org/jruby/RubyKernel.java:1009:in `load'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/bin/rubocop:23:in `<main>'
org/jruby/RubyKernel.java:1009:in `load'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/exe/bundle:46:in `block in <main>'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/lib/ruby/gems/shared/gems/bundler-2.1.4/exe/bundle:34:in `<main>'
org/jruby/RubyKernel.java:1009:in `load'
/Users/tom/.asdf/installs/ruby/jruby-9.2.13.0/bin/bundle:23:in `<main>'
.
1 file inspected, no offenses detected
1 error occurred:
An error occurred while Style/IfInsideElse cop was inspecting /Users/tom/Projects/myproject/tester.rb:3:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues
Mention the following information in the issue report:
1.7.0 (using Parser 2.7.2.0, rubocop-ast 1.4.0, running on jruby 2.5.7 java)
Finished in 2.6855570369953057 seconds

Steps to reproduce the problem

Lint the following file using RuboCop 1.7:

if a
  blah if COND
elsif b
  foo if COND
else
  bar if COND
end

RuboCop version

$ bundle exec rubocop -V
1.7.0 (using Parser 2.7.2.0, rubocop-ast 1.4.0, running on jruby 2.5.7 java)
  - rubocop-performance 1.9.2
  - rubocop-rake 0.5.1
  - rubocop-rspec 2.1.0
@koic koic added the bug label Jan 5, 2021
koic added a commit to koic/rubocop that referenced this issue Jan 5, 2021
Fixes rubocop#9333.

This PR fixes an error for `Style/IfInsideElse`
when using a modifier `if` nested inside an `else` after `elsif`.
bbatsov pushed a commit that referenced this issue Jan 5, 2021
Fixes #9333.

This PR fixes an error for `Style/IfInsideElse`
when using a modifier `if` nested inside an `else` after `elsif`.
ananace added a commit to voxpupuli/puppet-k8s that referenced this issue May 12, 2021
@joshbuker
Copy link

Running into this exact issue on some legacy code. I'm guessing most people are just wrapping the second conditional into the original if/else chain?

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.

3 participants