From 4134cb2da4d1aee7e34e352cfa22d9473c7ddf14 Mon Sep 17 00:00:00 2001 From: mollerhoj Date: Mon, 8 Aug 2022 12:05:33 +0200 Subject: [PATCH] add min and max to list of false positives for Style/SymbolProc --- ...f_false_positives_for_style_symbol_proc.md | 1 + lib/rubocop/cop/style/symbol_proc.rb | 5 +++ spec/rubocop/cop/style/symbol_proc_spec.rb | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 changelog/fix_add_min_and_max_to_list_of_false_positives_for_style_symbol_proc.md diff --git a/changelog/fix_add_min_and_max_to_list_of_false_positives_for_style_symbol_proc.md b/changelog/fix_add_min_and_max_to_list_of_false_positives_for_style_symbol_proc.md new file mode 100644 index 00000000000..5992ec3ac3b --- /dev/null +++ b/changelog/fix_add_min_and_max_to_list_of_false_positives_for_style_symbol_proc.md @@ -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][]) diff --git a/lib/rubocop/cop/style/symbol_proc.rb b/lib/rubocop/cop/style/symbol_proc.rb index 5808194a59d..2a11bef344d 100644 --- a/lib/rubocop/cop/style/symbol_proc.rb +++ b/lib/rubocop/cop/style/symbol_proc.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/style/symbol_proc_spec.rb b/spec/rubocop/cop/style/symbol_proc_spec.rb index 59d5162beee..a51120be63b 100644 --- a/spec/rubocop/cop/style/symbol_proc_spec.rb +++ b/spec/rubocop/cop/style/symbol_proc_spec.rb @@ -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 } } @@ -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 }