Skip to content

Commit

Permalink
Revert changes from #10061.
Browse files Browse the repository at this point in the history
These changes added an exception for `size` but the same issue can occur regardless of what method is used, as long as it results in multiple values having the same resulting value. Instead, this cop is being made unsafe.
  • Loading branch information
dvandersluis authored and bbatsov committed Sep 28, 2021
1 parent 3a13e1d commit 066b226
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 28 deletions.
18 changes: 0 additions & 18 deletions lib/rubocop/cop/style/redundant_sort.rb
Expand Up @@ -76,12 +76,8 @@ class RedundantSort < Base

def on_send(node)
if (sort_node, sorter, accessor = redundant_sort?(node.parent))
return if use_size_method_in_block?(sort_node)

ancestor = node.parent
elsif (sort_node, sorter, accessor = redundant_sort?(node.parent&.parent))
return if use_size_method_in_block?(sort_node)

ancestor = node.parent.parent
else
return
Expand All @@ -92,20 +88,6 @@ def on_send(node)

private

def use_size_method_in_block?(sort_node)
return true if block_calls_send?(sort_node)
return false unless sort_node.block_argument?

sort_node.last_argument.children.first.value == :size
end

def block_calls_send?(node)
return false unless node.send_type? && node.block_node
return false unless node.block_node.body.send_type?

node.block_node.body.method?(:size)
end

def register_offense(ancestor, sort_node, sorter, accessor)
message = message(ancestor, sorter, accessor)

Expand Down
10 changes: 0 additions & 10 deletions spec/rubocop/cop/style/redundant_sort_spec.rb
Expand Up @@ -231,16 +231,6 @@
expect_no_offenses('[[1, 2], [3, 4]].first.sort')
end

# `[2, 1, 3].sort_by(&:size).last` is not equivalent to `[2, 1, 3].max_by(&:size)`.
it 'does not register an offense when using `sort_by(&:size).last`' do
expect_no_offenses('[2, 1, 3].sort_by(&:size).last')
end

# `[2, 1, 3].sort_by { |i| i.size }.last` is not equivalent to `[2, 1, 3].max_by { |i| i.size }`.
it 'does not register an offense when using `sort_by { |i| i.size }.last`' do
expect_no_offenses('[2, 1, 3].sort_by { |i| i.size }.last')
end

# `[2, 1, 3].sort_by(&:size).first` is not equivalent to `[2, 1, 3].first`, but this
# cop would "correct" it to `[2, 1, 3].min_by`.
it 'does not register an offense when sort_by is not given a block' do
Expand Down

0 comments on commit 066b226

Please sign in to comment.