Skip to content

Commit

Permalink
Use redundant instead of unneeded
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Sep 14, 2017
1 parent 44fa554 commit 712bce3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* [#4694](https://github.com/bbatsov/rubocop/pull/4694): Add new `Lint/UriRegexp` cop. ([@koic][])
* Add new `Style/MinMax` cop. ([@drenmi][])
* [#4720](https://github.com/bbatsov/rubocop/pull/4720): Add new `Bundler/InsecureProtocolSource` cop. ([@koic][])
* [#4708](https://github.com/bbatsov/rubocop/pull/4708): Add new `Lint/UnneededWithIndex` cop`. ([@koic][])
* [#4708](https://github.com/bbatsov/rubocop/pull/4708): Add new `Lint/RedundantWithIndex` cop`. ([@koic][])

### Bug fixes

Expand Down
8 changes: 4 additions & 4 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,10 @@ Lint/RandOne:
and most likely a mistake.
Enabled: true

Lint/RedundantWithIndex:
Description: 'Checks for redundant `with_index`.'
Enabled: true

Lint/RequireParentheses:
Description: >-
Use parentheses in the method call to avoid confusion
Expand Down Expand Up @@ -1391,10 +1395,6 @@ Lint/UnneededDisable:
It must be explicitly disabled.
Enabled: true

Lint/UnneededWithIndex:
Description: 'Checks for unneeded `with_index`.'
Enabled: true

Lint/UnneededSplatExpansion:
Description: 'Checks for splat unnecessarily being called on literals'
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
require 'rubocop/cop/lint/percent_string_array'
require 'rubocop/cop/lint/percent_symbol_array'
require 'rubocop/cop/lint/rand_one'
require 'rubocop/cop/lint/redundant_with_index'
require 'rubocop/cop/lint/require_parentheses'
require 'rubocop/cop/lint/rescue_exception'
require 'rubocop/cop/lint/rescue_type'
Expand All @@ -266,7 +267,6 @@
require 'rubocop/cop/lint/underscore_prefixed_variable_name'
require 'rubocop/cop/lint/unified_integer'
require 'rubocop/cop/lint/unneeded_disable'
require 'rubocop/cop/lint/unneeded_with_index'
require 'rubocop/cop/lint/unneeded_splat_expansion'
require 'rubocop/cop/lint/unreachable_code'
require 'rubocop/cop/lint/unused_block_argument'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RuboCop
module Cop
module Lint
# This cop checks for unneeded `with_index`.
# This cop checks for redundant `with_index`.
#
# @example
# # bad
Expand All @@ -26,11 +26,11 @@ module Lint
# v
# end
#
class UnneededWithIndex < Cop
class RedundantWithIndex < Cop
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'.freeze
MSG_WITH_INDEX = 'Remove unneeded `with_index`.'.freeze
MSG_WITH_INDEX = 'Remove redundant `with_index`.'.freeze

def_node_matcher :unneeded_with_index?, <<-PATTERN
def_node_matcher :redundant_with_index?, <<-PATTERN
(block
$(send
_ {:each_with_index :with_index})
Expand All @@ -40,14 +40,14 @@ class UnneededWithIndex < Cop
PATTERN

def on_block(node)
unneeded_with_index?(node) do |send|
redundant_with_index?(node) do |send|
add_offense(node, with_index_range(send))
end
end

def autocorrect(node)
lambda do |corrector|
unneeded_with_index?(node) do |send|
redundant_with_index?(node) do |send|
if send.method_name == :each_with_index
corrector.replace(send.loc.selector, 'each')
else
Expand Down
2 changes: 1 addition & 1 deletion manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ In the following section you find all available cops:
* [Lint/PercentStringArray](cops_lint.md#lintpercentstringarray)
* [Lint/PercentSymbolArray](cops_lint.md#lintpercentsymbolarray)
* [Lint/RandOne](cops_lint.md#lintrandone)
* [Lint/RedundantWithIndex](cops_lint.md#lintredundantwithindex)
* [Lint/RequireParentheses](cops_lint.md#lintrequireparentheses)
* [Lint/RescueException](cops_lint.md#lintrescueexception)
* [Lint/RescueType](cops_lint.md#lintrescuetype)
Expand All @@ -224,7 +225,6 @@ In the following section you find all available cops:
* [Lint/UnifiedInteger](cops_lint.md#lintunifiedinteger)
* [Lint/UnneededDisable](cops_lint.md#lintunneededdisable)
* [Lint/UnneededSplatExpansion](cops_lint.md#lintunneededsplatexpansion)
* [Lint/UnneededWithIndex](cops_lint.md#lintunneededwithindex)
* [Lint/UnreachableCode](cops_lint.md#lintunreachablecode)
* [Lint/UnusedBlockArgument](cops_lint.md#lintunusedblockargument)
* [Lint/UnusedMethodArgument](cops_lint.md#lintunusedmethodargument)
Expand Down
64 changes: 32 additions & 32 deletions manual/cops_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,38 @@ rand(-1.0)
0 # just use 0 instead
```

## Lint/RedundantWithIndex

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop checks for redundant `with_index`.

### Example

```ruby
# bad
ary.each_with_index do |v|
v
end

# good
ary.each do |v|
v
end

# bad
ary.each.with_index do |v|
v
end

# good
ary.each do |v|
v
end
```

## Lint/RequireParentheses

Enabled by default | Supports autocorrection
Expand Down Expand Up @@ -1875,38 +1907,6 @@ else
end
```

## Lint/UnneededWithIndex

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop checks for unneeded `with_index`.

### Example

```ruby
# bad
ary.each_with_index do |v|
v
end

# good
ary.each do |v|
v
end

# bad
ary.each.with_index do |v|
v
end

# good
ary.each do |v|
v
end
```

## Lint/UnreachableCode

Enabled by default | Supports autocorrection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe RuboCop::Cop::Lint::UnneededWithIndex do
describe RuboCop::Cop::Lint::RedundantWithIndex do
let(:config) { RuboCop::Config.new }
subject(:cop) { described_class.new(config) }

Expand All @@ -14,7 +14,7 @@
it 'registers an offense when using `ary.each.with_index { |v| v }`' do
expect_offense(<<-RUBY.strip_indent)
ary.each.with_index { |v| v }
^^^^^^^^^^ Remove unneeded `with_index`.
^^^^^^^^^^ Remove redundant `with_index`.
RUBY
end

Expand Down

0 comments on commit 712bce3

Please sign in to comment.