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

Layout/ElseAlignment failed with "undefined method `line' for nil:NilClass" #5680

Closed
YukiJikumaru opened this issue Mar 14, 2018 · 1 comment
Labels

Comments

@YukiJikumaru
Copy link
Contributor

YukiJikumaru commented Mar 14, 2018

Expected behavior

rubocp should success without complaint.

Actual behavior

rubocp failed and "undefined method `line' for nil:NilClass" occurs.

Steps to reproduce the problem

[jiku@centos7 rubocop_else_alignment_error]$ cat sample.rb 
# frozen_string_literal: true

SAMPLE_NUMBERS = [1, 2, 3].freeze

def execute
  SAMPLE_NUMBERS.each do |num|
    do_something(num)
  rescue StandardError
    logger.error('ERROR')
  else
    logger.info('OK')
  end
end
[jiku@centos7 rubocop_else_alignment_error]$ ruby -c sample.rb 
Syntax OK
[jiku@centos7 rubocop_else_alignment_error]$ bundle exec rubocop -d sample.rb                                                                                                                                      
For /home/jiku/rubocop_else_alignment_error: configuration from /home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/config/default.yml
Inheriting configuration from /home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/config/enabled.yml
Inheriting configuration from /home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/config/disabled.yml
Inspecting 1 file
Scanning /home/jiku/rubocop_else_alignment_error/sample.rb
An error occurred while Layout/ElseAlignment cop was inspecting /home/jiku/rubocop_else_alignment_error/sample.rb:7:4.
undefined method `line' for nil:NilClass
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/mixin/end_keyword_alignment.rb:75:in `effective_column'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/layout/else_alignment.rb:117:in `check_alignment'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/layout/else_alignment.rb:60:in `on_rescue'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:44:in `block (2 levels) in on_rescue'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:109:in `with_cop_error_handling'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:43:in `block in on_rescue'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:42:in `each'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:42:in `on_rescue'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:154:in `on_block'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:48:in `on_block'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:95:in `on_def'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:48:in `on_def'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:49:in `block in on_begin'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:49:in `each'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:49:in `on_begin'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:48:in `on_begin'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/ast/traversal.rb:12:in `walk'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/commissioner.rb:60:in `investigate'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/team.rb:114:in `investigate'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/team.rb:102:in `offenses'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cop/team.rb:44:in `inspect_file'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:259:in `inspect_file'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:206:in `block in do_inspection_loop'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:238:in `block in iterate_until_no_changes'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:231:in `loop'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:231:in `iterate_until_no_changes'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:202:in `do_inspection_loop'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:111:in `block in file_offenses'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:121:in `file_offense_cache'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:109:in `file_offenses'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:100:in `process_file'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:75:in `each'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:75:in `reduce'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:75:in `each_inspected_file'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:67:in `inspect_files'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/runner.rb:39:in `run'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cli.rb:156:in `execute_runner'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cli.rb:84:in `execute_runners'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/lib/rubocop/cli.rb:41:in `run'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/bin/rubocop:13:in `block in <top (required)>'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/gems/rubocop-0.53.0/bin/rubocop:12:in `<top (required)>'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/bin/rubocop:23:in `load'
/home/jiku/rubocop_else_alignment_error/vendor/bundle/ruby/2.5.0/bin/rubocop:23:in `<top (required)>'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `load'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `kernel_load'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:28:in `run'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:424:in `exec'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:27:in `dispatch'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:18:in `start'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/exe/bundle:30:in `block in <top (required)>'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
/home/jiku/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/exe/bundle:22:in `<top (required)>'
/home/jiku/.rbenv/versions/2.5.0/bin/bundle:23:in `load'
/home/jiku/.rbenv/versions/2.5.0/bin/bundle:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Layout/ElseAlignment cop was inspecting /home/jiku/rubocop_else_alignment_error/sample.rb:7:4.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
Mention the following information in the issue report:
0.53.0 (using Parser 2.5.0.4, running on ruby 2.5.0 x86_64-linux)
Finished in 0.4670725599862635 seconds

RuboCop version

[jiku@centos7 rubocop_else_alignment_error]$ bundle exec rubocop -V
0.53.0 (using Parser 2.5.0.4, running on ruby 2.5.0 x86_64-linux)
[jiku@centos7 rubocop_else_alignment_error]$ uname -a
Linux centos7 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[jiku@centos7 rubocop_else_alignment_error]$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

memo https://bugs.ruby-lang.org/issues/12906

@YukiJikumaru YukiJikumaru changed the title Layout/ElseAlignment occurs "undefined method `line' for nil:NilClass" Layout/ElseAlignment failed with "undefined method `line' for nil:NilClass" Mar 14, 2018
@YukiJikumaru
Copy link
Contributor Author

--- a/lib/rubocop/cop/layout/else_alignment.rb
+++ b/lib/rubocop/cop/layout/else_alignment.rb
@@ -55,6 +55,7 @@ module RuboCop
           base = case parent.type
                  when :def, :defs then base_for_method_definition(parent)
                  when :kwbegin then parent.loc.begin
+                 when :block then parent.send_node.source_range
                  else node.loc.keyword
                  end
           check_alignment(base, node.loc.else)

I tested this patch, fix this error and passed all test.

@pocke pocke added the bug label Mar 15, 2018
YukiJikumaru added a commit to YukiJikumaru/rubocop that referenced this issue Mar 15, 2018
@pocke pocke closed this as completed in 059b1c5 Mar 15, 2018
This was referenced Mar 21, 2018
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

2 participants