From 72b0dfc4ed18b59bcee96f34971c2fc16f7cc20d Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Sun, 4 Sep 2022 10:02:42 +0900 Subject: [PATCH] Avoid collection literals in method --- lib/rubocop/cop/performance/compare_with_block.rb | 4 +++- lib/rubocop/cop/performance/regexp_match.rb | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/performance/compare_with_block.rb b/lib/rubocop/cop/performance/compare_with_block.rb index cfbbb1de9b..c3a75a4aab 100644 --- a/lib/rubocop/cop/performance/compare_with_block.rb +++ b/lib/rubocop/cop/performance/compare_with_block.rb @@ -3,6 +3,8 @@ module RuboCop module Cop module Performance + KEY_TYPES = %i[sym str int].freeze + # Identifies places where `sort { |a, b| a.foo <=> b.foo }` # can be replaced by `sort_by(&:foo)`. # This cop also checks `max` and `min` methods. @@ -73,7 +75,7 @@ def slow_compare?(method, args_a, args_b) return false unless args_a.size == 1 key = args_a.first - return false unless %i[sym str int].include?(key.type) + return false unless KEY_TYPES.include?(key.type) else return false unless args_a.empty? end diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index 5a5c5ee62c..3c2b36ff7c 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -81,6 +81,7 @@ class RegexpMatch < Base # Constants are included in this list because it is unlikely that # someone will store `nil` as a constant and then use it for comparison TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].freeze + GVAR_SYMS = %i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].freeze MSG = 'Use `match?` instead of `%s` when `MatchData` is not used.' def_node_matcher :match_method?, <<~PATTERN @@ -240,7 +241,7 @@ def scope_root(node) end def match_gvar?(sym) - %i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].include?(sym) + GVAR_SYMS.include?(sym) end def correct_operator(corrector, recv, arg, oper = nil)