Skip to content

Commit

Permalink
Merge pull request #13849 from Homebrew/dependabot/bundler/Library/Ho…
Browse files Browse the repository at this point in the history
…mebrew/rubocop-performance-1.15.0

build(deps): bump rubocop-performance from 1.14.3 to 1.15.0 in /Library/Homebrew
  • Loading branch information
MikeMcQuaid committed Sep 13, 2022
2 parents 0d4fb24 + 20d2bda commit db01704
Show file tree
Hide file tree
Showing 61 changed files with 92 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-performance (1.14.3)
rubocop-performance (1.15.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.16.0)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Library/Homebrew/vendor/bundle/bundler/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ruby-progressbar-1.11.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/unicode-display_width-2.2.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-1.35.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.14.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.15.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-rails-2.16.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-rspec-2.12.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-sorbet-0.6.11/lib"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# This is the default configuration file.

Performance:
Enabled: true
DocumentationBaseURL: https://docs.rubocop.org/rubocop-performance

Performance/AncestorsInclude:
Description: 'Use `A <= B` instead of `A.ancestors.include?(B)`.'
Reference: 'https://github.com/JuanitoFatas/fast-ruby#ancestorsinclude-vs--code'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ module SortBlock
$send)
PATTERN

def_node_matcher :sort_with_numblock?, <<~PATTERN
(numblock
$(send _ :sort)
$_arg_count
$send)
PATTERN

def_node_matcher :replaceable_body?, <<~PATTERN
(send (lvar %1) :<=> (lvar %2))
PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ def on_send(node)
end
elsif (numeric_to_d = to_d?(node))
add_offense(numeric_to_d.source_range) do |corrector|
big_decimal_args = node
.arguments
.map(&:source)
.unshift("'#{numeric_to_d.source}'")
.join(', ')
big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')

corrector.replace(node, "BigDecimal(#{big_decimal_args})")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class BindCall < Base

minimum_target_ruby_version 2.7

MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` ' \
'instead of `bind(%<bind_arg>s).call(%<call_args>s)`.'
MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` instead of `bind(%<bind_arg>s).call(%<call_args>s)`.'
RESTRICT_ON_SEND = %i[call].freeze

def_node_matcher :bind_with_call_method?, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def replacement(conditions)

def inline_fix_branch(corrector, when_node)
conditions = when_node.conditions
range = range_between(conditions[0].loc.expression.begin_pos,
conditions[-1].loc.expression.end_pos)
range = range_between(conditions[0].loc.expression.begin_pos, conditions[-1].loc.expression.end_pos)

corrector.replace(range, replacement(conditions))
end
Expand All @@ -111,8 +110,7 @@ def reorder_condition(corrector, when_node)
return if when_branches.one?

corrector.remove(when_branch_range(when_node))
corrector.insert_after(when_branches.last.source_range,
reordering_correction(when_node))
corrector.insert_after(when_branches.last.source_range, reordering_correction(when_node))
end

def reordering_correction(when_node)
Expand All @@ -126,11 +124,9 @@ def reordering_correction(when_node)
end

def when_branch_range(when_node)
next_branch =
when_node.parent.when_branches[when_node.branch_index + 1]
next_branch = when_node.parent.when_branches[when_node.branch_index + 1]

range_between(when_node.source_range.begin_pos,
next_branch.source_range.begin_pos)
range_between(when_node.source_range.begin_pos, next_branch.source_range.begin_pos)
end

def new_condition_with_then(node, new_condition)
Expand Down Expand Up @@ -162,13 +158,11 @@ def splat_offenses(when_conditions)
def non_splat?(condition)
variable, = *condition

(condition.splat_type? && variable.array_type?) ||
!condition.splat_type?
(condition.splat_type? && variable.array_type?) || !condition.splat_type?
end

def needs_reorder?(when_node)
following_branches =
when_node.parent.when_branches[(when_node.branch_index + 1)..]
following_branches = when_node.parent.when_branches[(when_node.branch_index + 1)..]

following_branches.any? do |when_branch|
when_branch.conditions.any? do |condition|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ def parent_is_loop?(node)
end

def loop?(ancestor, node)
keyword_loop?(ancestor.type) ||
kernel_loop?(ancestor) ||
node_within_enumerable_loop?(node, ancestor)
keyword_loop?(ancestor.type) || kernel_loop?(ancestor) || node_within_enumerable_loop?(node, ancestor)
end

def keyword_loop?(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class ConstantRegexp < Base
MSG = 'Extract this regexp into a constant, memoize it, or append an `/o` option to its options.'

def on_regexp(node)
return if within_allowed_assignment?(node) ||
!include_interpolated_const?(node) ||
node.single_interpolation?
return if within_allowed_assignment?(node) || !include_interpolated_const?(node) || node.single_interpolation?

add_offense(node) do |corrector|
corrector.insert_after(node, 'o')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ module Performance
class DoubleStartEndWith < Base
extend AutoCorrector

MSG = 'Use `%<receiver>s.%<method>s(%<combined_args>s)` ' \
'instead of `%<original_code>s`.'
MSG = 'Use `%<receiver>s.%<method>s(%<combined_args>s)` instead of `%<original_code>s`.'

def on_or(node)
receiver, method, first_call_args, second_call_args = process_source(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class EndWith < Base
include RegexpMetacharacter
extend AutoCorrector

MSG = 'Use `String#end_with?` instead of a regex match anchored to ' \
'the end of the string.'
MSG = 'Use `String#end_with?` instead of a regex match anchored to the end of the string.'
RESTRICT_ON_SEND = %i[match =~ match?].freeze

def_node_matcher :redundant_regex?, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def current_method(node)

def use_long_method
preferred_config = config.for_all_cops['Style/PreferredHashMethods']
preferred_config &&
preferred_config['EnforcedStyle'] == 'long' &&
preferred_config['Enabled']
preferred_config && preferred_config['EnforcedStyle'] == 'long' && preferred_config['Enabled']
end

def autocorrect_argument(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ module Performance
# end
#
class OpenStruct < Base
MSG = 'Consider using `Struct` over `OpenStruct` ' \
'to optimize the performance.'
MSG = 'Consider using `Struct` over `OpenStruct` to optimize the performance.'
RESTRICT_ON_SEND = %i[new].freeze

def_node_matcher :open_struct, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ module Performance
class RedundantMatch < Base
extend AutoCorrector

MSG = 'Use `=~` in places where the `MatchData` returned by ' \
'`#match` will not be used.'
MSG = 'Use `=~` in places where the `MatchData` returned by `#match` will not be used.'
RESTRICT_ON_SEND = %i[match].freeze

# 'match' is a fairly generic name, so we don't flag it unless we see
Expand All @@ -41,21 +40,23 @@ def on_send(node)
!(node.parent && node.parent.block_type?)

add_offense(node) do |corrector|
autocorrect(corrector, node)
autocorrect(corrector, node) if autocorrectable?(node)
end
end

private

def autocorrect(corrector, node)
# Regexp#match can take a second argument, but this cop doesn't
# register an offense in that case
return unless node.first_argument.regexp_type?

new_source = "#{node.receiver.source} =~ #{node.first_argument.source}"

corrector.replace(node.source_range, new_source)
end

def autocorrectable?(node)
# Regexp#match can take a second argument, but this cop doesn't
# register an offense in that case
node.receiver.regexp_type? || node.first_argument.regexp_type?
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def kwsplat_used?(pairs)
end

def non_redundant_value_used?(receiver, node)
node.value_used? &&
!EachWithObjectInspector.new(node, receiver).value_used?
node.value_used? && !EachWithObjectInspector.new(node, receiver).value_used?
end

def correct_multiple_elements(corrector, node, parent, new_source)
Expand All @@ -125,9 +124,7 @@ def to_assignments(receiver, pairs)

key = key.sym_type? && pair.colon? ? ":#{key.source}" : key.source

format(AREF_ASGN, receiver: receiver.source,
key: key,
value: value.source)
format(AREF_ASGN, receiver: receiver.source, key: key, value: value.source)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ class RedundantSortBlock < Base
include SortBlock
extend AutoCorrector

MSG = 'Use `sort` instead of `%<bad_method>s`.'
MSG = 'Use `sort` without block.'

def on_block(node)
return unless (send, var_a, var_b, body = sort_with_block?(node))

replaceable_body?(body, var_a, var_b) do
range = sort_range(send, node)
register_offense(send, node)
end
end

def on_numblock(node)
return unless (send, arg_count, body = sort_with_numblock?(node))
return unless arg_count == 2

add_offense(range, message: message(var_a, var_b)) do |corrector|
corrector.replace(range, 'sort')
end
replaceable_body?(body, :_1, :_2) do
register_offense(send, node)
end
end

private

def message(var_a, var_b)
bad_method = "sort { |#{var_a}, #{var_b}| #{var_a} <=> #{var_b} }"
format(MSG, bad_method: bad_method)
def register_offense(send, node)
range = sort_range(send, node)

add_offense(range) do |corrector|
corrector.replace(range, 'sort')
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ def modifier_form?(match_node)
def find_last_match(body, range, scope_root)
last_matches(body).find do |ref|
ref_pos = ref.loc.expression.begin_pos
range.cover?(ref_pos) &&
scope_root(ref) == scope_root
range.cover?(ref_pos) && scope_root(ref) == scope_root
end
end

Expand All @@ -241,14 +240,7 @@ def scope_root(node)
end

def match_gvar?(sym)
%i[
$~
$MATCH
$PREMATCH
$POSTMATCH
$LAST_PAREN_MATCH
$LAST_MATCH_INFO
].include?(sym)
%i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].include?(sym)
end

def correct_operator(corrector, recv, arg, oper = nil)
Expand Down

0 comments on commit db01704

Please sign in to comment.