Merge pull request #12643 from Homebrew/dependabot/bundler/Library/Homebrew/rubocop-performance-1.13.0

build(deps): bump rubocop-performance from 1.12.0 to 1.13.0 in /Library/Homebrew
This commit is contained in:
Mike McQuaid 2021-12-29 13:33:04 +00:00 committed by GitHub
commit 36e9e45c54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
64 changed files with 248 additions and 100 deletions

View File

@ -135,7 +135,7 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
rubocop-performance (1.12.0)
rubocop-performance (1.13.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.13.0)

View File

@ -81,7 +81,7 @@ module Homebrew
Formulary.enable_factory_cache!
recursive = !args.send("1?")
recursive = !args.send(:"1?")
installed = args.installed? || dependents(args.named.to_formulae_and_casks).all?(&:any_version_installed?)
@use_runtime_dependencies = installed && recursive &&

View File

@ -46,10 +46,7 @@ class RuboCop::Cop::Performance::BigDecimalWithNumericArgument < ::RuboCop::Cop:
def big_decimal_with_numeric_argument?(param0 = T.unsafe(nil)); end
def on_send(node); end
private
def specifies_precision?(node); end
def to_d?(param0 = T.unsafe(nil)); end
end
RuboCop::Cop::Performance::BigDecimalWithNumericArgument::MSG = T.let(T.unsafe(nil), String)
@ -406,8 +403,10 @@ class RuboCop::Cop::Performance::MapCompact < ::RuboCop::Cop::Base
private
def compact_method_with_final_newline_range(compact_method_range); end
def invoke_method_after_map_compact_on_same_line?(compact_node, chained_method); end
def remove_compact_method(corrector, compact_node); end
def map_method_and_compact_method_on_same_line?(compact_node); end
def remove_compact_method(corrector, compact_node, chained_method); end
end
RuboCop::Cop::Performance::MapCompact::MSG = T.let(T.unsafe(nil), String)
@ -586,6 +585,7 @@ class RuboCop::Cop::Performance::RedundantStringChars < ::RuboCop::Cop::Base
def build_bad_method(method, args); end
def build_call_args(call_args_node); end
def build_good_method(method, args); end
def build_good_method_for_brackets_or_first_method(method, args); end
def build_message(method, args); end
def correction_range(receiver, node); end
def offense_range(receiver, node); end
@ -734,6 +734,15 @@ end
RuboCop::Cop::Performance::StartWith::MSG = T.let(T.unsafe(nil), String)
RuboCop::Cop::Performance::StartWith::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array)
class RuboCop::Cop::Performance::StringIdentifierArgument < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::AutoCorrector
def on_send(node); end
end
RuboCop::Cop::Performance::StringIdentifierArgument::MSG = T.let(T.unsafe(nil), String)
RuboCop::Cop::Performance::StringIdentifierArgument::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array)
class RuboCop::Cop::Performance::StringInclude < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::AutoCorrector

View File

@ -130,7 +130,7 @@ describe "ENV" do
describe "#compiler" do
it "allows switching compilers" do
subject.public_send("gcc-6")
subject.public_send(:"gcc-6")
expect(subject.compiler).to eq("gcc-6")
end
end

View File

@ -87,7 +87,7 @@ $:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-ast-1.15.1/li
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ruby-progressbar-1.11.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/unicode-display_width-2.1.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-1.24.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.12.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.13.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-rails-2.13.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-rspec-2.7.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-sorbet-0.6.5/lib"

View File

@ -1,46 +0,0 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# This cop identifies places where numeric argument to BigDecimal should be
# converted to string. Initializing from String is faster
# than from Numeric for BigDecimal.
#
# @example
# # bad
# BigDecimal(1, 2)
# BigDecimal(1.2, 3, exception: true)
#
# # good
# BigDecimal('1', 2)
# BigDecimal('1.2', 3, exception: true)
#
class BigDecimalWithNumericArgument < Base
extend AutoCorrector
MSG = 'Convert numeric argument to string before passing to `BigDecimal`.'
RESTRICT_ON_SEND = %i[BigDecimal].freeze
def_node_matcher :big_decimal_with_numeric_argument?, <<~PATTERN
(send nil? :BigDecimal $numeric_type? ...)
PATTERN
def on_send(node)
return unless (numeric = big_decimal_with_numeric_argument?(node))
return if numeric.float_type? && specifies_precision?(node)
add_offense(numeric.source_range) do |corrector|
corrector.wrap(numeric, "'", "'")
end
end
private
def specifies_precision?(node)
node.arguments.size > 1 && !node.arguments[1].hash_type?
end
end
end
end
end

View File

@ -17,7 +17,7 @@ Performance/ArraySemiInfiniteRangeSlice:
VersionAdded: '1.9'
Performance/BigDecimalWithNumericArgument:
Description: 'Convert numeric argument to string before passing to BigDecimal.'
Description: 'Convert numeric literal to string and pass it to `BigDecimal`.'
Enabled: 'pending'
VersionAdded: '1.7'
@ -43,10 +43,9 @@ Performance/CaseWhenSplat:
Reordering `when` conditions with a splat to the end
of the `when` branches can improve performance.
Enabled: false
AutoCorrect: false
SafeAutoCorrect: false
VersionAdded: '0.34'
VersionChanged: '0.59'
VersionChanged: '1.13'
Performance/Casecmp:
Description: >-
@ -137,7 +136,7 @@ Performance/DoubleStartEndWith:
VersionAdded: '0.36'
VersionChanged: '0.48'
# Used to check for `starts_with?` and `ends_with?`.
# These methods are defined by `ActiveSupport`.
# These methods are defined by Active Support.
IncludeActiveSupportAliases: false
Performance/EndWith:
@ -312,6 +311,11 @@ Performance/StartWith:
VersionAdded: '0.36'
VersionChanged: '1.10'
Performance/StringIdentifierArgument:
Description: 'Use symbol identifier argument instead of string identifier argument.'
Enabled: pending
VersionAdded: '1.13'
Performance/StringInclude:
Description: 'Use `String#include?` instead of a regex match with literal-only pattern.'
Enabled: 'pending'
@ -330,17 +334,20 @@ Performance/StringReplacement:
Performance/Sum:
Description: 'Use `sum` instead of a custom array summation.'
SafeAutoCorrect: false
Reference: 'https://blog.bigbinary.com/2016/11/02/ruby-2-4-introduces-enumerable-sum.html'
Enabled: 'pending'
VersionAdded: '1.8'
VersionChanged: '1.13'
OnlySumOrWithInitialValue: false
Performance/TimesMap:
Description: 'Checks for .times.map calls.'
AutoCorrect: false
Enabled: true
# See https://github.com/rubocop/rubocop/issues/4658
SafeAutoCorrect: false
VersionAdded: '0.36'
VersionChanged: '0.50'
SafeAutoCorrect: false # see https://github.com/rubocop/rubocop/issues/4658
VersionChanged: '1.13'
Performance/UnfreezeString:
Description: 'Use unary plus to get an unfrozen string literal.'

View File

@ -0,0 +1,57 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# This cop identifies places where numeric argument to BigDecimal should be
# converted to string. Initializing from String is faster
# than from Numeric for BigDecimal.
#
# @example
# # bad
# BigDecimal(1, 2)
# 4.to_d(6)
# BigDecimal(1.2, 3, exception: true)
# 4.5.to_d(6, exception: true)
#
# # good
# BigDecimal('1', 2)
# BigDecimal('4', 6)
# BigDecimal('1.2', 3, exception: true)
# BigDecimal('4.5', 6, exception: true)
#
class BigDecimalWithNumericArgument < Base
extend AutoCorrector
MSG = 'Convert numeric literal to string and pass it to `BigDecimal`.'
RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze
def_node_matcher :big_decimal_with_numeric_argument?, <<~PATTERN
(send nil? :BigDecimal $numeric_type? ...)
PATTERN
def_node_matcher :to_d?, <<~PATTERN
(send [!nil? $numeric_type?] :to_d ...)
PATTERN
def on_send(node)
if (numeric = big_decimal_with_numeric_argument?(node))
add_offense(numeric.source_range) do |corrector|
corrector.wrap(numeric, "'", "'")
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(', ')
corrector.replace(node, "BigDecimal(#{big_decimal_args})")
end
end
end
end
end
end
end

View File

@ -27,10 +27,6 @@ module RuboCop
# [].detect { |item| true }
# [].reverse.detect { |item| true }
#
# `ActiveRecord` compatibility:
# `ActiveRecord` does not implement a `detect` method and `find` has its
# own meaning. Correcting ActiveRecord methods with this cop should be
# considered unsafe.
class Detect < Base
extend AutoCorrector

View File

@ -7,6 +7,9 @@ module RuboCop
# separated by `||`. In some cases such calls can be replaced
# with an single `#start_with?`/`#end_with?` call.
#
# `IncludeActiveSupportAliases` configuration option is used to check for
# `starts_with?` and `ends_with?`. These methods are defined by Active Support.
#
# @example
# # bad
# str.start_with?("a") || str.start_with?(Some::CONST)
@ -17,6 +20,24 @@ module RuboCop
# str.start_with?("a", Some::CONST)
# str.start_with?("a", "b", "c")
# str.end_with?(var1, var2)
#
# @example IncludeActiveSupportAliases: false (default)
# # good
# str.starts_with?("a", "b") || str.starts_with?("c")
# str.ends_with?(var1) || str.ends_with?(var2)
#
# str.starts_with?("a", "b", "c")
# str.ends_with?(var1, var2)
#
# @example IncludeActiveSupportAliases: true
# # bad
# str.starts_with?("a", "b") || str.starts_with?("c")
# str.ends_with?(var1) || str.ends_with?(var2)
#
# # good
# str.starts_with?("a", "b", "c")
# str.ends_with?(var1, var2)
#
class DoubleStartEndWith < Base
extend AutoCorrector

View File

@ -58,19 +58,19 @@ module RuboCop
add_offense(range) do |corrector|
corrector.replace(map_node.loc.selector, 'filter_map')
remove_compact_method(corrector, node)
remove_compact_method(corrector, node, node.parent)
end
end
private
def remove_compact_method(corrector, compact_node)
chained_method = compact_node.parent
def remove_compact_method(corrector, compact_node, chained_method)
compact_method_range = compact_node.loc.selector
if compact_node.multiline? && chained_method&.loc.respond_to?(:selector) && chained_method.dot? &&
!map_method_and_compact_method_on_same_line?(compact_node) &&
!invoke_method_after_map_compact_on_same_line?(compact_node, chained_method)
compact_method_range = range_by_whole_lines(compact_method_range, include_final_newline: true)
compact_method_range = compact_method_with_final_newline_range(compact_method_range)
else
corrector.remove(compact_node.loc.dot)
end
@ -78,9 +78,21 @@ module RuboCop
corrector.remove(compact_method_range)
end
def map_method_and_compact_method_on_same_line?(compact_node)
return false unless compact_node.children.first.respond_to?(:send_node)
map_node = compact_node.children.first.send_node
compact_node.loc.selector.line == map_node.loc.selector.line
end
def invoke_method_after_map_compact_on_same_line?(compact_node, chained_method)
compact_node.loc.selector.line == chained_method.loc.selector.line
end
def compact_method_with_final_newline_range(compact_method_range)
range_by_whole_lines(compact_method_range, include_final_newline: true)
end
end
end
end

View File

@ -82,14 +82,10 @@ module RuboCop
def build_good_method(method, args)
case method
when :[], :slice
when :slice
"[#{build_call_args(args)}].chars"
when :first
if args.any?
"[0...#{args.first.source}].chars"
else
'[0]'
end
when :[], :first
build_good_method_for_brackets_or_first_method(method, args)
when :take
"[0...#{args.first.source}].chars"
else
@ -97,6 +93,18 @@ module RuboCop
end
end
def build_good_method_for_brackets_or_first_method(method, args)
first_arg = args.first
if first_arg&.range_type?
"[#{build_call_args(args)}].chars"
elsif method == :first && args.any?
"[0...#{args.first.source}].chars"
else
first_arg ? "[#{first_arg.source}]" : '[0]'
end
end
def build_bad_method(method, args)
case method
when :[]

View File

@ -0,0 +1,59 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# This cop identifies places where string identifier argument can be replaced
# by symbol identifier argument.
# It prevents the redundancy of the internal string-to-symbol conversion.
#
# This cop targets methods that take identifier (e.g. method name) argument
# and the following examples are parts of it.
#
# @example
#
# # bad
# send('do_something')
# attr_accessor 'do_something'
# instance_variable_get('@ivar')
#
# # good
# send(:do_something)
# attr_accessor :do_something
# instance_variable_get(:@ivar)
#
class StringIdentifierArgument < Base
extend AutoCorrector
MSG = 'Use `%<symbol_arg>s` instead of `%<string_arg>s`.'
RESTRICT_ON_SEND = %i[
alias_method attr attr_accessor attr_reader attr_writer autoload autoload?
class_variable_defined? const_defined? const_get const_set const_source_location
define_method instance_method method_defined? private_class_method? private_method_defined?
protected_method_defined? public_class_method public_instance_method public_method_defined?
remove_class_variable remove_method undef_method class_variable_get class_variable_set
deprecate_constant module_function private private_constant protected public public_constant
remove_const ruby2_keywords
define_singleton_method instance_variable_defined instance_variable_get instance_variable_set
method public_method public_send remove_instance_variable respond_to? send singleton_method
__send__
].freeze
def on_send(node)
return unless (first_argument = node.first_argument)
return unless first_argument.str_type?
return if first_argument.value.include?(' ')
replacement = first_argument.value.to_sym.inspect
message = format(MSG, symbol_arg: replacement, string_arg: first_argument.source)
add_offense(first_argument, message: message) do |corrector|
corrector.replace(first_argument, replacement)
end
end
end
end
end
end

View File

@ -6,35 +6,46 @@ module RuboCop
# This cop identifies places where custom code finding the sum of elements
# in some Enumerable object can be replaced by `Enumerable#sum` method.
#
# This cop can change auto-correction scope depending on the value of
# `SafeAutoCorrect`.
# Its auto-correction is marked as safe by default (`SafeAutoCorrect: true`)
# to prevent `TypeError` in auto-correced code when initial value is not
# specified as shown below:
# @safety
# Auto-corrections are unproblematic wherever an initial value is provided explicitly:
#
# [source,ruby]
# ----
# ['a', 'b'].sum # => (String can't be coerced into Integer)
# ----
# [source,ruby]
# ----
# [1, 2, 3].reduce(4, :+) # => 10
# [1, 2, 3].sum(4) # => 10
#
# Therefore if initial value is not specified, unsafe auto-corrected will not occur.
# [].reduce(4, :+) # => 4
# [].sum(4) # => 4
# ----
#
# If you always want to enable auto-correction, you can set `SafeAutoCorrect: false`.
# This also holds true for non-numeric types which implement a `:+` method:
#
# [source,yaml]
# ----
# Performance/Sum:
# SafeAutoCorrect: false
# ----
# [source,ruby]
# ----
# ['l', 'o'].reduce('Hel', :+) # => "Hello"
# ['l', 'o'].sum('Hel') # => "Hello"
# ----
#
# Please note that the auto-correction command line option will be changed from
# `rubocop -a` to `rubocop -A`, which includes unsafe auto-correction.
# When no initial value is provided though, `Enumerable#reduce` will pick the first enumerated value
# as initial value and successively add all following values to it, whereas
# `Enumerable#sum` will set an initial value of `0` (`Integer`) which can lead to a `TypeError`:
#
# @example
# [source,ruby]
# ----
# [].reduce(:+) # => nil
# [1, 2, 3].reduce(:+) # => 6
# ['H', 'e', 'l', 'l', 'o'].reduce(:+) # => "Hello"
#
# [].sum # => 0
# [1, 2, 3].sum # => 6
# ['H', 'e', 'l', 'l', 'o'].sum # => in `+': String can't be coerced into Integer (TypeError)
# ----
#
# @example OnlySumOrWithInitialValue: false (default)
# # bad
# [1, 2, 3].inject(:+) # These bad cases with no initial value are unsafe and
# [1, 2, 3].inject(&:+) # will not be auto-correced by default. If you want to
# [1, 2, 3].reduce { |acc, elem| acc + elem } # auto-corrected, you can set `SafeAutoCorrect: false`.
# [1, 2, 3].inject(:+) # Auto-corrections for cases without initial value are unsafe
# [1, 2, 3].inject(&:+) # and will only be performed when using the `-A` option.
# [1, 2, 3].reduce { |acc, elem| acc + elem } # They can be prohibited completely using `SafeAutoCorrect: true`.
# [1, 2, 3].reduce(10, :+)
# [1, 2, 3].map { |elem| elem ** 2 }.sum
# [1, 2, 3].collect(&:count).sum(10)
@ -45,6 +56,17 @@ module RuboCop
# [1, 2, 3].sum { |elem| elem ** 2 }
# [1, 2, 3].sum(10, &:count)
#
# @example OnlySumOrWithInitialValue: true
# # bad
# [1, 2, 3].reduce(10, :+)
# [1, 2, 3].map { |elem| elem ** 2 }.sum
# [1, 2, 3].collect(&:count).sum(10)
#
# # good
# [1, 2, 3].sum(10)
# [1, 2, 3].sum { |elem| elem ** 2 }
# [1, 2, 3].sum(10, &:count)
#
class Sum < Base
include RangeHelp
extend AutoCorrector
@ -103,6 +125,8 @@ module RuboCop
def handle_sum_candidate(node)
sum_candidate?(node) do |method, init, operation|
next if cop_config['OnlySumOrWithInitialValue'] && init.empty?
range = sum_method_range(node)
message = build_method_message(node, method, init, operation)

View File

@ -44,6 +44,7 @@ require_relative 'performance/size'
require_relative 'performance/sort_reverse'
require_relative 'performance/squeeze'
require_relative 'performance/start_with'
require_relative 'performance/string_identifier_argument'
require_relative 'performance/string_include'
require_relative 'performance/string_replacement'
require_relative 'performance/sum'

View File

@ -4,7 +4,7 @@ module RuboCop
module Performance
# This module holds the RuboCop Performance version information.
module Version
STRING = '1.12.0'
STRING = '1.13.0'
def self.document_version
STRING.match('\d+\.\d+').to_s