Skip to content

Commit

Permalink
Avoid collection literals in method
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Sep 5, 2022
1 parent 64aeb36 commit fab23f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/rubocop/cop/performance/compare_with_block.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/performance/regexp_match.rb
Expand Up @@ -80,7 +80,8 @@ 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
TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].to_set.freeze
GVAR_SYMS = %i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].to_set.freeze
MSG = 'Use `match?` instead of `%<current>s` when `MatchData` is not used.'

def_node_matcher :match_method?, <<~PATTERN
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fab23f8

Please sign in to comment.