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

undefined method `source' for nil:NilClass in ZeroLengthPredicate #3131

Closed
chronodm opened this issue May 12, 2016 · 1 comment · Fixed by #3147
Closed

undefined method `source' for nil:NilClass in ZeroLengthPredicate #3131

chronodm opened this issue May 12, 2016 · 1 comment · Fixed by #3147

Comments

@chronodm
Copy link

chronodm commented May 12, 2016

Expected behavior

rubocop -a completes without errors

Actual behavior

rubocop -a bails after parsing five of 20 files, with the attached stack trace (originating at zero_length_predicate.rb:65).

Steps to reproduce the problem

Run rubocop -a against the following source:

class Foo
  attr_accessor :size

  def frob
    return nil if size == 0
    1
  end
end

Notes

  • Using @size instead of size fixes the problem, as does using size <= 0 instead of size == 0.
  • Using self.size instead of size does not fix the problem -- though self.size gets autocorrected to size before it fails.
  • Introducing an empty? method does not fix the problem (although it's likely that in that case the failure is in the empty? method's own size == 0 check).
  • adding # rubocop:disable Style/ZeroLengthPredicate after def frob, surprisingly, does not fix the problem.

RuboCop version

Include the output of rubocop -V:

$ bundle exec rubocop -V
0.40.0 (using Parser 2.3.1.0, running on ruby 2.2.3 x86_64-darwin14)
@tejasbubane
Copy link
Contributor

Actually the bug is with the cop showing offense for size == 0:

Offenses:

rubocop_bug_test.rb:5:19: C: Style/ZeroLengthPredicate: Use empty? instead of size == 0.
    return nil if size == 0
                  ^^^^^^^^^

1 file inspected, 1 offense detected
Finished in 0.23116467497311532 seconds

This should not happen since the cop works on Array#size and not variables. Same happens for length. I added some failing specs for these and will send a PR soon.

tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 19, 2016
Closes rubocop#3131

`Style/ZeroLengthPredicate` cop recognized offense for variables with
names `length` and `size`.

eg. `size == 0`

This PR fixes the issue.
bbatsov pushed a commit that referenced this issue May 19, 2016
…es (#3147)

Closes #3131

`Style/ZeroLengthPredicate` cop recognized offense for variables with
names `length` and `size`.

eg. `size == 0`

This PR fixes the issue.
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
…es (rubocop#3147)

Closes rubocop#3131

`Style/ZeroLengthPredicate` cop recognized offense for variables with
names `length` and `size`.

eg. `size == 0`

This PR fixes the issue.
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

Successfully merging a pull request may close this issue.

2 participants