From 8003ca093a3bd22c44df9363b105954e52a87ca5 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 10 Oct 2020 00:01:42 +0900 Subject: [PATCH] Require RuboCop 0.90 or higher due to use `RESTRICT_ON_SEND` Follow https://github.com/rubocop-hq/rubocop/pull/8365 This PR uses `RESTRICT_ON_SEND` to restrict callbacks `on_send` to specific method names only. `RESTRICT_ON_SEND` has been introduced since RuboCop 0.90. --- CHANGELOG.md | 1 + lib/rubocop/cop/performance/ancestors_include.rb | 1 + .../performance/big_decimal_with_numeric_argument.rb | 1 + lib/rubocop/cop/performance/bind_call.rb | 1 + lib/rubocop/cop/performance/caller.rb | 1 + lib/rubocop/cop/performance/casecmp.rb | 1 + lib/rubocop/cop/performance/count.rb | 1 + lib/rubocop/cop/performance/delete_prefix.rb | 1 + lib/rubocop/cop/performance/delete_suffix.rb | 1 + lib/rubocop/cop/performance/detect.rb | 1 + lib/rubocop/cop/performance/end_with.rb | 1 + lib/rubocop/cop/performance/fixed_size.rb | 1 + lib/rubocop/cop/performance/flat_map.rb | 1 + lib/rubocop/cop/performance/inefficient_hash_search.rb | 2 ++ lib/rubocop/cop/performance/io_readlines.rb | 10 +++------- lib/rubocop/cop/performance/open_struct.rb | 1 + lib/rubocop/cop/performance/range_include.rb | 1 + lib/rubocop/cop/performance/redundant_match.rb | 1 + lib/rubocop/cop/performance/redundant_merge.rb | 1 + lib/rubocop/cop/performance/redundant_string_chars.rb | 8 ++------ lib/rubocop/cop/performance/reverse_each.rb | 1 + lib/rubocop/cop/performance/reverse_first.rb | 1 + lib/rubocop/cop/performance/size.rb | 1 + lib/rubocop/cop/performance/squeeze.rb | 1 + lib/rubocop/cop/performance/start_with.rb | 1 + lib/rubocop/cop/performance/string_include.rb | 1 + lib/rubocop/cop/performance/string_replacement.rb | 1 + lib/rubocop/cop/performance/sum.rb | 1 + lib/rubocop/cop/performance/times_map.rb | 1 + lib/rubocop/cop/performance/unfreeze_string.rb | 1 + lib/rubocop/cop/performance/uri_default_parser.rb | 1 + rubocop-performance.gemspec | 2 +- 32 files changed, 36 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ae3a05e28..2ab6ee74c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#170](https://github.com/rubocop-hq/rubocop-performance/pull/170): Extend `Performance/Sum` to register an offense for `map { ... }.sum`. ([@eugeneius][]) * [#179](https://github.com/rubocop-hq/rubocop-performance/pull/179): Change `Performance/Sum` to warn about empty arrays, and not register an offense on empty array literals. ([@ghiculescu][]) +* [#180](https://github.com/rubocop-hq/rubocop-performance/pull/180): Require RuboCop 0.90 or higher. ([@koic][]) ## 1.8.1 (2020-09-19) diff --git a/lib/rubocop/cop/performance/ancestors_include.rb b/lib/rubocop/cop/performance/ancestors_include.rb index 62ae0cd19c..b1e9109983 100644 --- a/lib/rubocop/cop/performance/ancestors_include.rb +++ b/lib/rubocop/cop/performance/ancestors_include.rb @@ -18,6 +18,7 @@ class AncestorsInclude < Base extend AutoCorrector MSG = 'Use `<=` instead of `ancestors.include?`.' + RESTRICT_ON_SEND = %i[include?].freeze def_node_matcher :ancestors_include_candidate?, <<~PATTERN (send (send $_subclass :ancestors) :include? $_superclass) diff --git a/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb b/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb index 87bea01028..1248544cf2 100644 --- a/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +++ b/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb @@ -20,6 +20,7 @@ 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? ...) diff --git a/lib/rubocop/cop/performance/bind_call.rb b/lib/rubocop/cop/performance/bind_call.rb index e143987762..001209171f 100644 --- a/lib/rubocop/cop/performance/bind_call.rb +++ b/lib/rubocop/cop/performance/bind_call.rb @@ -28,6 +28,7 @@ class BindCall < Base MSG = 'Use `bind_call(%s%s%s)` ' \ 'instead of `bind(%s).call(%s)`.' + RESTRICT_ON_SEND = %i[call].freeze def_node_matcher :bind_with_call_method?, <<~PATTERN (send diff --git a/lib/rubocop/cop/performance/caller.rb b/lib/rubocop/cop/performance/caller.rb index fc8077cd7f..ee5e5f4754 100644 --- a/lib/rubocop/cop/performance/caller.rb +++ b/lib/rubocop/cop/performance/caller.rb @@ -23,6 +23,7 @@ class Caller < Base ' instead of `%s[%d]`.' MSG_FIRST = 'Use `%s(%d..%d).first`' \ ' instead of `%s.first`.' + RESTRICT_ON_SEND = %i[first []].freeze def_node_matcher :slow_caller?, <<~PATTERN { diff --git a/lib/rubocop/cop/performance/casecmp.rb b/lib/rubocop/cop/performance/casecmp.rb index 0bccda3d84..c981757c79 100644 --- a/lib/rubocop/cop/performance/casecmp.rb +++ b/lib/rubocop/cop/performance/casecmp.rb @@ -23,6 +23,7 @@ class Casecmp < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[== eql? !=].freeze CASE_METHODS = %i[downcase upcase].freeze def_node_matcher :downcase_eq, <<~PATTERN diff --git a/lib/rubocop/cop/performance/count.rb b/lib/rubocop/cop/performance/count.rb index 0b20aa2141..d6c6ad0501 100644 --- a/lib/rubocop/cop/performance/count.rb +++ b/lib/rubocop/cop/performance/count.rb @@ -42,6 +42,7 @@ class Count < Base extend AutoCorrector MSG = 'Use `count` instead of `%s...%s`.' + RESTRICT_ON_SEND = %i[count length size].freeze def_node_matcher :count_candidate?, <<~PATTERN { diff --git a/lib/rubocop/cop/performance/delete_prefix.rb b/lib/rubocop/cop/performance/delete_prefix.rb index 9ec14b62d1..f023ffbc20 100644 --- a/lib/rubocop/cop/performance/delete_prefix.rb +++ b/lib/rubocop/cop/performance/delete_prefix.rb @@ -51,6 +51,7 @@ class DeletePrefix < Base minimum_target_ruby_version 2.5 MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[gsub gsub! sub sub!].freeze PREFERRED_METHODS = { gsub: :delete_prefix, diff --git a/lib/rubocop/cop/performance/delete_suffix.rb b/lib/rubocop/cop/performance/delete_suffix.rb index 45f8c06004..d4251210bd 100644 --- a/lib/rubocop/cop/performance/delete_suffix.rb +++ b/lib/rubocop/cop/performance/delete_suffix.rb @@ -51,6 +51,7 @@ class DeleteSuffix < Base minimum_target_ruby_version 2.5 MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[gsub gsub! sub sub!].freeze PREFERRED_METHODS = { gsub: :delete_suffix, diff --git a/lib/rubocop/cop/performance/detect.rb b/lib/rubocop/cop/performance/detect.rb index 96fe8dbd81..55b07329ea 100644 --- a/lib/rubocop/cop/performance/detect.rb +++ b/lib/rubocop/cop/performance/detect.rb @@ -39,6 +39,7 @@ class Detect < Base '`%s[%i]`.' INDEX_REVERSE_MSG = 'Use `reverse.%s` instead of ' \ '`%s[%i]`.' + RESTRICT_ON_SEND = %i[first last []].freeze def_node_matcher :detect_candidate?, <<~PATTERN { diff --git a/lib/rubocop/cop/performance/end_with.rb b/lib/rubocop/cop/performance/end_with.rb index 26545ecc1b..155c48f8a2 100644 --- a/lib/rubocop/cop/performance/end_with.rb +++ b/lib/rubocop/cop/performance/end_with.rb @@ -47,6 +47,7 @@ class EndWith < Base 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 {(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt))) diff --git a/lib/rubocop/cop/performance/fixed_size.rb b/lib/rubocop/cop/performance/fixed_size.rb index 9469a2bb89..01959f2130 100644 --- a/lib/rubocop/cop/performance/fixed_size.rb +++ b/lib/rubocop/cop/performance/fixed_size.rb @@ -47,6 +47,7 @@ module Performance # class FixedSize < Base MSG = 'Do not compute the size of statically sized objects.' + RESTRICT_ON_SEND = %i[count length size].freeze def_node_matcher :counter, <<~MATCHER (send ${array hash str sym} {:count :length :size} $...) diff --git a/lib/rubocop/cop/performance/flat_map.rb b/lib/rubocop/cop/performance/flat_map.rb index 6179a8838b..150e9c841a 100644 --- a/lib/rubocop/cop/performance/flat_map.rb +++ b/lib/rubocop/cop/performance/flat_map.rb @@ -19,6 +19,7 @@ class FlatMap < Base extend AutoCorrector MSG = 'Use `flat_map` instead of `%s...%s`.' + RESTRICT_ON_SEND = %i[flatten flatten!].freeze FLATTEN_MULTIPLE_LEVELS = ' Beware, `flat_map` only flattens 1 level ' \ 'and `flatten` can be used to flatten ' \ 'multiple levels.' diff --git a/lib/rubocop/cop/performance/inefficient_hash_search.rb b/lib/rubocop/cop/performance/inefficient_hash_search.rb index 60ff196cbe..a5c47def6c 100644 --- a/lib/rubocop/cop/performance/inefficient_hash_search.rb +++ b/lib/rubocop/cop/performance/inefficient_hash_search.rb @@ -39,6 +39,8 @@ module Performance class InefficientHashSearch < Base extend AutoCorrector + RESTRICT_ON_SEND = %i[include?].freeze + def_node_matcher :inefficient_include?, <<~PATTERN (send (send $_ {:keys :values}) :include? _) PATTERN diff --git a/lib/rubocop/cop/performance/io_readlines.rb b/lib/rubocop/cop/performance/io_readlines.rb index 1290a9b84c..fb9d04b034 100644 --- a/lib/rubocop/cop/performance/io_readlines.rb +++ b/lib/rubocop/cop/performance/io_readlines.rb @@ -29,14 +29,14 @@ class IoReadlines < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' - ENUMERABLE_METHODS = (Enumerable.instance_methods + [:each]).freeze + RESTRICT_ON_SEND = (Enumerable.instance_methods + [:each]).freeze def_node_matcher :readlines_on_class?, <<~PATTERN - $(send $(send (const nil? {:IO :File}) :readlines ...) #enumerable_method?) + $(send $(send (const nil? {:IO :File}) :readlines ...) _) PATTERN def_node_matcher :readlines_on_instance?, <<~PATTERN - $(send $(send ${nil? !const_type?} :readlines ...) #enumerable_method? ...) + $(send $(send ${nil? !const_type?} :readlines ...) _ ...) PATTERN def on_send(node) @@ -55,10 +55,6 @@ def on_send(node) private - def enumerable_method?(node) - ENUMERABLE_METHODS.include?(node.to_sym) - end - def autocorrect(corrector, enumerable_call, readlines_call, receiver) # We cannot safely correct `.readlines` method called on IO/File classes # due to its signature and we are not sure with implicit receiver diff --git a/lib/rubocop/cop/performance/open_struct.rb b/lib/rubocop/cop/performance/open_struct.rb index 25ad5f8f63..6709b68e14 100644 --- a/lib/rubocop/cop/performance/open_struct.rb +++ b/lib/rubocop/cop/performance/open_struct.rb @@ -30,6 +30,7 @@ module Performance class OpenStruct < Base MSG = 'Consider using `Struct` over `OpenStruct` ' \ 'to optimize the performance.' + RESTRICT_ON_SEND = %i[new].freeze def_node_matcher :open_struct, <<~PATTERN (send (const {nil? cbase} :OpenStruct) :new ...) diff --git a/lib/rubocop/cop/performance/range_include.rb b/lib/rubocop/cop/performance/range_include.rb index e32d9e42cd..e39dcc2400 100644 --- a/lib/rubocop/cop/performance/range_include.rb +++ b/lib/rubocop/cop/performance/range_include.rb @@ -28,6 +28,7 @@ class RangeInclude < Base extend AutoCorrector MSG = 'Use `Range#cover?` instead of `Range#%s`.' + RESTRICT_ON_SEND = %i[include? member?].freeze # TODO: If we traced out assignments of variables to their uses, we # might pick up on a few more instances of this issue diff --git a/lib/rubocop/cop/performance/redundant_match.rb b/lib/rubocop/cop/performance/redundant_match.rb index 791bcebf71..b75a8f856c 100644 --- a/lib/rubocop/cop/performance/redundant_match.rb +++ b/lib/rubocop/cop/performance/redundant_match.rb @@ -22,6 +22,7 @@ class RedundantMatch < Base 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 # a string or regexp literal on one side or the other diff --git a/lib/rubocop/cop/performance/redundant_merge.rb b/lib/rubocop/cop/performance/redundant_merge.rb index 244e768199..d986cc3a09 100644 --- a/lib/rubocop/cop/performance/redundant_merge.rb +++ b/lib/rubocop/cop/performance/redundant_merge.rb @@ -29,6 +29,7 @@ class RedundantMerge < Base AREF_ASGN = '%s[%s] = %s' MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[merge!].freeze WITH_MODIFIER_CORRECTION = <<~RUBY %s %s diff --git a/lib/rubocop/cop/performance/redundant_string_chars.rb b/lib/rubocop/cop/performance/redundant_string_chars.rb index be84d464c4..9d5eb625c6 100644 --- a/lib/rubocop/cop/performance/redundant_string_chars.rb +++ b/lib/rubocop/cop/performance/redundant_string_chars.rb @@ -44,10 +44,10 @@ class RedundantStringChars < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' - REPLACEABLE_METHODS = %i[[] slice first last take drop length size empty?].freeze + RESTRICT_ON_SEND = %i[[] slice first last take drop length size empty?].freeze def_node_matcher :redundant_chars_call?, <<~PATTERN - (send $(send _ :chars) $#replaceable_method? $...) + (send $(send _ :chars) $_ $...) PATTERN def on_send(node) @@ -66,10 +66,6 @@ def on_send(node) private - def replaceable_method?(method_name) - REPLACEABLE_METHODS.include?(method_name) - end - def offense_range(receiver, node) range_between(receiver.loc.selector.begin_pos, node.loc.expression.end_pos) end diff --git a/lib/rubocop/cop/performance/reverse_each.rb b/lib/rubocop/cop/performance/reverse_each.rb index 86041cd6be..51fc4d05e0 100644 --- a/lib/rubocop/cop/performance/reverse_each.rb +++ b/lib/rubocop/cop/performance/reverse_each.rb @@ -17,6 +17,7 @@ class ReverseEach < Base extend AutoCorrector MSG = 'Use `reverse_each` instead of `reverse.each`.' + RESTRICT_ON_SEND = %i[each].freeze UNDERSCORE = '_' def_node_matcher :reverse_each?, <<~MATCHER diff --git a/lib/rubocop/cop/performance/reverse_first.rb b/lib/rubocop/cop/performance/reverse_first.rb index 13d627fe4c..7db7f53e09 100644 --- a/lib/rubocop/cop/performance/reverse_first.rb +++ b/lib/rubocop/cop/performance/reverse_first.rb @@ -21,6 +21,7 @@ class ReverseFirst < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[first].freeze def_node_matcher :reverse_first_candidate?, <<~PATTERN (send $(send _ :reverse) :first (int _)?) diff --git a/lib/rubocop/cop/performance/size.rb b/lib/rubocop/cop/performance/size.rb index b67c544dc8..4356c5d6e5 100644 --- a/lib/rubocop/cop/performance/size.rb +++ b/lib/rubocop/cop/performance/size.rb @@ -39,6 +39,7 @@ class Size < Base extend AutoCorrector MSG = 'Use `size` instead of `count`.' + RESTRICT_ON_SEND = %i[count].freeze def_node_matcher :array?, <<~PATTERN { diff --git a/lib/rubocop/cop/performance/squeeze.rb b/lib/rubocop/cop/performance/squeeze.rb index 39fa4fb323..2c07f184de 100644 --- a/lib/rubocop/cop/performance/squeeze.rb +++ b/lib/rubocop/cop/performance/squeeze.rb @@ -22,6 +22,7 @@ class Squeeze < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[gsub gsub!].freeze PREFERRED_METHODS = { gsub: :squeeze, diff --git a/lib/rubocop/cop/performance/start_with.rb b/lib/rubocop/cop/performance/start_with.rb index b343247134..c55ac7af5c 100644 --- a/lib/rubocop/cop/performance/start_with.rb +++ b/lib/rubocop/cop/performance/start_with.rb @@ -47,6 +47,7 @@ class StartWith < Base MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \ 'the beginning of the string.' + RESTRICT_ON_SEND = %i[match =~ match?].freeze def_node_matcher :redundant_regex?, <<~PATTERN {(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt))) diff --git a/lib/rubocop/cop/performance/string_include.rb b/lib/rubocop/cop/performance/string_include.rb index 655023535e..c7fc6cd1b2 100644 --- a/lib/rubocop/cop/performance/string_include.rb +++ b/lib/rubocop/cop/performance/string_include.rb @@ -23,6 +23,7 @@ class StringInclude < Base extend AutoCorrector MSG = 'Use `String#include?` instead of a regex match with literal-only pattern.' + RESTRICT_ON_SEND = %i[match =~ match?].freeze def_node_matcher :redundant_regex?, <<~PATTERN {(send $!nil? {:match :=~ :match?} (regexp (str $#literal?) (regopt))) diff --git a/lib/rubocop/cop/performance/string_replacement.rb b/lib/rubocop/cop/performance/string_replacement.rb index 6364f98ab8..68f85eab4f 100644 --- a/lib/rubocop/cop/performance/string_replacement.rb +++ b/lib/rubocop/cop/performance/string_replacement.rb @@ -23,6 +23,7 @@ class StringReplacement < Base extend AutoCorrector MSG = 'Use `%s` instead of `%s`.' + RESTRICT_ON_SEND = %i[gsub gsub!].freeze DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/.freeze DELETE = 'delete' TR = 'tr' diff --git a/lib/rubocop/cop/performance/sum.rb b/lib/rubocop/cop/performance/sum.rb index 76f82ef1f0..a2dd20c2e0 100644 --- a/lib/rubocop/cop/performance/sum.rb +++ b/lib/rubocop/cop/performance/sum.rb @@ -28,6 +28,7 @@ class Sum < Base MSG = 'Use `%s` instead of `%s`.' MSG_IF_NO_INIT_VALUE = 'Use `%s` instead of `%s`, unless calling `%s` on an empty array.' + RESTRICT_ON_SEND = %i[inject reduce sum].freeze def_node_matcher :sum_candidate?, <<~PATTERN (send _ ${:inject :reduce} $_init ? ${(sym :+) (block_pass (sym :+))}) diff --git a/lib/rubocop/cop/performance/times_map.rb b/lib/rubocop/cop/performance/times_map.rb index 8718de9f96..907983a04c 100644 --- a/lib/rubocop/cop/performance/times_map.rb +++ b/lib/rubocop/cop/performance/times_map.rb @@ -23,6 +23,7 @@ class TimesMap < Base MESSAGE = 'Use `Array.new(%s)` with a block ' \ 'instead of `.times.%s`' MESSAGE_ONLY_IF = 'only if `%s` is always 0 or more' + RESTRICT_ON_SEND = %i[map collect].freeze def on_send(node) check(node) diff --git a/lib/rubocop/cop/performance/unfreeze_string.rb b/lib/rubocop/cop/performance/unfreeze_string.rb index e366cec098..e88695b710 100644 --- a/lib/rubocop/cop/performance/unfreeze_string.rb +++ b/lib/rubocop/cop/performance/unfreeze_string.rb @@ -25,6 +25,7 @@ module Performance # +'' class UnfreezeString < Base MSG = 'Use unary plus to get an unfrozen string literal.' + RESTRICT_ON_SEND = %i[dup new].freeze def_node_matcher :dup_string?, <<~PATTERN (send {str dstr} :dup) diff --git a/lib/rubocop/cop/performance/uri_default_parser.rb b/lib/rubocop/cop/performance/uri_default_parser.rb index add87fe820..0dc0d43b5c 100644 --- a/lib/rubocop/cop/performance/uri_default_parser.rb +++ b/lib/rubocop/cop/performance/uri_default_parser.rb @@ -18,6 +18,7 @@ class UriDefaultParser < Base MSG = 'Use `%sURI::DEFAULT_PARSER` instead of ' \ '`%sURI::Parser.new`.' + RESTRICT_ON_SEND = %i[new].freeze def_node_matcher :uri_parser_new?, <<~PATTERN (send diff --git a/rubocop-performance.gemspec b/rubocop-performance.gemspec index a0f72bd822..e886c195d2 100644 --- a/rubocop-performance.gemspec +++ b/rubocop-performance.gemspec @@ -31,7 +31,7 @@ Gem::Specification.new do |s| 'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop-performance/issues' } - s.add_runtime_dependency('rubocop', '>= 0.87.0') + s.add_runtime_dependency('rubocop', '>= 0.90.0') s.add_runtime_dependency('rubocop-ast', '>= 0.4.0') s.add_development_dependency('simplecov') end