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

Runtime error in Lint/DefEndAlignment auto-correct #2015

Closed
gotrevor opened this issue Jul 5, 2015 · 2 comments
Closed

Runtime error in Lint/DefEndAlignment auto-correct #2015

gotrevor opened this issue Jul 5, 2015 · 2 comments
Assignees

Comments

@gotrevor
Copy link
Contributor

gotrevor commented Jul 5, 2015

Using this file (crash.rb) as input:

# encoding: utf-8
def func
  foo
rescue
  baz
  end

With this option enabled:

Lint/DefEndAlignment:
  AutoCorrect: true

I get the following crash:

[~/repos/rubocop] $ bin/rubocop crash.rb -a -d
...
An error occurred while Lint/DefEndAlignment cop was inspecting /Users/gotrevor/repos/rubocop/crash.rb.
undefined method `expression' for nil:NilClass
/Users/gotrevor/repos/rubocop/lib/rubocop/cop/mixin/end_keyword_alignment.rb:49:in `align'
/Users/gotrevor/repos/rubocop/lib/rubocop/cop/lint/def_end_alignment.rb:52:in `autocorrect'
...

I've tracked the problem down to the method investigate here:
https://github.com/bbatsov/rubocop/blob/v0.32.1/lib/rubocop/cop/variable_force.rb#L71-L79
processed_source enters with

processed_source.ast.ancestors.first == nil

it exists with

processed_source.ast.ancestors.first != nil 
&& processed_source.ast.ancestors.first.loc == nil

This causes the crash here:
https://github.com/bbatsov/rubocop/blob/v0.32.1/lib/rubocop/cop/mixin/end_keyword_alignment.rb#L49
Which assumes that a non-nil node will have a loc. Is that a valid assumption?

I suspect that VariableForce#investigate shouldn't be mutating processed_source.ast.
Am I right about that?

@jonas054 jonas054 self-assigned this Jul 6, 2015
@jonas054
Copy link
Collaborator

jonas054 commented Jul 6, 2015

Thanks, @gotrevor for the analysis. I'm working on this problem already, but hadn't gotten down to VariableForce#investigate yet. I'll take your views in to consideration, and will make a PR soon.

@jonas054
Copy link
Collaborator

jonas054 commented Jul 7, 2015

To answer your question, I think it is reasonable for EndKeywordAlignment#align to expect that nodes given to it as the base for alignment have a loc attribute. When the node turns out to be a "fake" begin node, then it's the caller that's in error.

bbatsov added a commit that referenced this issue Jul 7, 2015
[Fix #2015] Fix --auto-correct for DefEndAlignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants