From 066b226f44776d8b901beafd952bffb3d6f7425e Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Mon, 27 Sep 2021 12:13:26 -0400 Subject: [PATCH] Revert changes from #10061. 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. --- lib/rubocop/cop/style/redundant_sort.rb | 18 ------------------ spec/rubocop/cop/style/redundant_sort_spec.rb | 10 ---------- 2 files changed, 28 deletions(-) diff --git a/lib/rubocop/cop/style/redundant_sort.rb b/lib/rubocop/cop/style/redundant_sort.rb index e4af9691c17..bf31a01c9fb 100644 --- a/lib/rubocop/cop/style/redundant_sort.rb +++ b/lib/rubocop/cop/style/redundant_sort.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/style/redundant_sort_spec.rb b/spec/rubocop/cop/style/redundant_sort_spec.rb index da7eb3bdce6..f69bd6009c5 100644 --- a/spec/rubocop/cop/style/redundant_sort_spec.rb +++ b/spec/rubocop/cop/style/redundant_sort_spec.rb @@ -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