Skip to content

Commit

Permalink
Merge pull request #10884 from mollerhoj/add_min_and_max_to_list_of_f…
Browse files Browse the repository at this point in the history
…alse_positives_for_style_symbol_proc

add min and max to list of false positives for Style/SymbolProc
  • Loading branch information
koic committed Aug 8, 2022
2 parents b806ff2 + 4134cb2 commit de683c5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10864](https://github.com/rubocop/rubocop/pull/10864): `min` and `max` results in false positives for `Style/SymbolProc` similarly to `select` and `reject`. ([@mollerhoj][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/symbol_proc.rb
Expand Up @@ -121,6 +121,7 @@ def on_block(node)
# we should allow lambdas & procs
return if proc_node?(dispatch_node)
return if unsafe_hash_usage?(dispatch_node)
return if unsafe_array_usage?(dispatch_node)
return if %i[lambda proc].include?(dispatch_node.method_name)
return if allowed_method_name?(dispatch_node.method_name)
return if allow_if_method_has_argument?(node.send_node)
Expand All @@ -144,6 +145,10 @@ def unsafe_hash_usage?(node)
node.receiver&.hash_type? && %i[reject select].include?(node.method_name)
end

def unsafe_array_usage?(node)
node.receiver&.array_type? && %i[min max].include?(node.method_name)
end

def allowed_method_name?(name)
allowed_method?(name) || matches_allowed_pattern?(name)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/style/symbol_proc_spec.rb
Expand Up @@ -180,6 +180,25 @@
end
end

%w[min max].each do |method|
it "registers an offense when receiver is a hash literal and using `#{method}` with a block" do
expect_offense(<<~RUBY, method: method)
{foo: 42}.%{method} {|item| item.foo }
_{method} ^^^^^^^^^^^^^^^^^^ Pass `&:foo` as an argument to `#{method}` instead of a block.
RUBY

expect_correction(<<~RUBY)
{foo: 42}.#{method}(&:foo)
RUBY
end

it "does not register an offense when receiver is a array literal and using `#{method}` with a block" do
expect_no_offenses(<<~RUBY, method: method)
[1, 2, 3].#{method} {|item| item.foo }
RUBY
end
end

context 'when `AllowMethodsWithArguments: true`' do
let(:cop_config) { { 'AllowMethodsWithArguments' => true } }

Expand Down Expand Up @@ -350,6 +369,25 @@
end
end

%w[min max].each do |method|
it "registers an offense when receiver is an hash literal and using `#{method}` with a numblock" do
expect_offense(<<~RUBY, method: method)
{foo: 42}.%{method} { _1.foo }
_{method} ^^^^^^^^^^ Pass `&:foo` as an argument to `#{method}` instead of a block.
RUBY

expect_correction(<<~RUBY)
{foo: 42}.#{method}(&:foo)
RUBY
end

it "does not register an offense when receiver is a array literal and using `#{method}` with a numblock" do
expect_no_offenses(<<~RUBY, method: method)
[1, 2, 3].#{method} { _1.foo }
RUBY
end
end

it 'registers an offense for a block with a numbered parameter' do
expect_offense(<<~RUBY)
something { _1.foo }
Expand Down

0 comments on commit de683c5

Please sign in to comment.