-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add new Lint/RedundantWithIndex
cop
#4708
Add new Lint/RedundantWithIndex
cop
#4708
Conversation
I think there could make this cop more generic and apply the same concept to This isn't entirely a safe assumption to make about the code based on the number of arguments that are passed to the block. You can still access the index from the single argument. [3] pry(main)> [1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }
1: 0
2: 1
3: 2 The opposite of this is used to give meaning to the key and value when iterating over a hash [6] pry(main)> {a: 1, b: 2, c: 3}.each { |hash| puts "#{hash[0]}: #{hash[1]}" }
a: 1
b: 2
c: 3
[7] pry(main)> {a: 1, b: 2, c: 3}.each { |key, value| puts "#{key}: #{value}" }
a: 1
b: 2
c: 3 |
Thanks for your comment. I also thought about integrating each_with_object. However, since we have not encountered a concrete use case that wrong block argument with
This behavior is JRuby's regression reported in jruby/jruby#4610. The original behavior of % ruby -e '[1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }'
1: 0
0: 1
1: 1 And this regression was fixed in JRuby 9.1.13.0 released several hours ago (!) . % rbenv shell jruby-9.1.12.0 && ruby -e '[1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }'
1: 0
2: 1
3: 2
% rbenv shell jruby-9.1.13.0 && ruby -e '[1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }'
1: 0
0: 1
1: 1 If it depends on the regression behavior, it will break when its regression is fixed. So every code should not depends on the regression behavior. And I thought that the original |
The example that I provided was using Ruby 2.4.1. This isn't a difference between Ruby and jRuby. I do see that jRuby was not handling % rbenv shell jruby-9.1.12.0 && ruby -e '[1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }'
1: 0
2: 1
3: 2
% rbenv shell jruby-9.1.13.0 && ruby -e '[1, 2, 3].each_with_index { |foo| puts "#{foo[0]}: #{foo[1]}" }'
1: 0
0: 1
1: 1
Based on this example, it looks like there is still a bug in jRuby with how a single block argument is handled. I assume that jRuby is working to fix this so jRuby and Ruby function the same way. Assuming they do function in the same way, a single block argument to What would you think about changing this slightly to recommend passing multiple arguments to block methods that yield more than one item.
|
I see!
I think about this use case, please wait for a while. Thank you for pointing out. |
# | ||
class UnneededWithIndex < Cop | ||
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'.freeze | ||
MSG_WITH_INDEX = 'Remove unneeded `with_index`.'.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally you'd use redundant
instead of unneeded
in English.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it all together. This changes the name of this cop from Lint/UnneededWithIndex
to Lint/RedundantWithIndex
. 712bce3
end | ||
|
||
def each_with_index_method?(node) | ||
node.children.first.children.last == :each_with_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this done with pattern matching?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was solved with method_name
method. 44fa554
What's the status here? |
I'm thinking about redesigning this cop considering the pointed out. I will update it soon. Please just a wait. |
5c09594
to
71959d3
Compare
Lint/UnneededWithIndex
copLint/RedundantWithIndex
cop
I examined the case of using only one block argument with In some cases I confirmed. It is exemplified by an Receive index argumentThis is a case that does not offense in this cop. > %i[foo bar].each_with_object([]).with_index { |(v, ret), index| puts "v = #{v}, index = #{index}" }
v = foo, index = 0
v = bar, index = 1
=> [] This also applies to the following cases pointed out. And in this case is no offense.
The following case is also no offense. > %i[foo bar].each_with_object([]).with_index { |v, index| puts "v = #{v}, index = #{index}" }
v = [:foo, []], index = 0
v = [:bar, []], index = 1
=> []
|
📝 I changed this cop name from
|
71959d3
to
ff35189
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. I think that a Lint/RedundantWithObject
would be valuable as well.
Feature
This cop checks for unneeded
with_index
.Target Problem
The main aim is to remove unneeded
with_index
and clear the code. This will reduce unneeded code, thus improving performance.The following is a benchmark script.
It is the result of running the above benchmark script.
The benchmark is for reference, the main aim is to make the code correct. That is why I chose this cop as Lint department.
Other Information
This topic is about JRuby. JRuby has the following Issue related to
with_index
. And this Issue seems to regress in the latest JRuby 9.1.12.0.jruby/jruby#4610
RuboCop had one place using unneeded
with_index
. Perhaps it is the cause of a build error of JRuby 9.1.12.0 at #4703.https://travis-ci.org/bbatsov/rubocop/jobs/272483544
This PR removes unneeded
with_index
, so I think that it will solve the above build error.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).