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

Bug in Performance/Count #2080

Closed
bquorning opened this issue Jul 28, 2015 · 4 comments · Fixed by #2087
Closed

Bug in Performance/Count #2080

bquorning opened this issue Jul 28, 2015 · 4 comments · Fixed by #2087

Comments

@bquorning
Copy link
Contributor

With this file test.rb:

# encoding: utf-8

# nodoc #
class A < Array
  def count(&block)
    select(&block).count
  end
end

a = A.new
a += [1, 2, 3, 4, 5, 6]
p a.count { |e| e > 1 }

RuboCop tells me that An error occurred while Performance/Count cop was inspecting /Users/bquorning/Code/rubocop/test.rb.

Extra information:

0.32.1 (using Parser 2.2.2.5, running on ruby 2.2.2 x86_64-darwin13)

Actually, I was using the latest master, cdfb667.

@bquorning
Copy link
Contributor Author

cc @rrosenblum

@rrosenblum
Copy link
Contributor

I will look into this.

@rrosenblum
Copy link
Contributor

I have a fix for this. I will put in a pull request shortly.

In the given example, the auto correct would be incorrect because of the method name.

def count(&block)
  select(&block).count
end

would correct to

def count(&block)
  count(&block)
end

which would be a recursive call and not function properly. To fix the code, the method would need to be renamed, the method should be removed, or the method should call super(&block).

For the fix, I am not taking into consideration that the defined method name is going to be the same as the correction.

@bquorning
Copy link
Contributor Author

For the fix, I am not taking into consideration that the defined method name is going to be the same as the correction.

Fine by me 👍

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