Skip to content

Commit

Permalink
Require RuboCop 0.90 or higher due to use RESTRICT_ON_SEND
Browse files Browse the repository at this point in the history
Follow rubocop/rubocop#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.
  • Loading branch information
koic committed Oct 11, 2020
1 parent 79d11d7 commit 5718984
Show file tree
Hide file tree
Showing 32 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes

* [#170](https://github.com/rubocop-hq/rubocop-performance/pull/170): Extend `Performance/Sum` to register an offense for `map { ... }.sum`. ([@eugeneius][])
* [#180](https://github.com/rubocop-hq/rubocop-performance/pull/180): Require RuboCop 0.90 or higher. ([@koic][])

## 1.8.1 (2020-09-19)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/ancestors_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? ...)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/bind_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BindCall < Base

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
(send
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Caller < Base
' instead of `%<method>s[%<m>d]`.'
MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
' instead of `%<method>s.first`.'
RESTRICT_ON_SEND = %i[first []].freeze

def_node_matcher :slow_caller?, <<~PATTERN
{
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/casecmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Casecmp < Base
extend AutoCorrector

MSG = 'Use `%<good>s` instead of `%<bad>s`.'
RESTRICT_ON_SEND = %i[== eql? !=].freeze
CASE_METHODS = %i[downcase upcase].freeze

def_node_matcher :downcase_eq, <<~PATTERN
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Count < Base
extend AutoCorrector

MSG = 'Use `count` instead of `%<selector>s...%<counter>s`.'
RESTRICT_ON_SEND = %i[count length size].freeze

def_node_matcher :count_candidate?, <<~PATTERN
{
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class DeletePrefix < Base
minimum_target_ruby_version 2.5

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[gsub gsub! sub sub!].freeze

PREFERRED_METHODS = {
gsub: :delete_prefix,
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class DeleteSuffix < Base
minimum_target_ruby_version 2.5

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[gsub gsub! sub sub!].freeze

PREFERRED_METHODS = {
gsub: :delete_suffix,
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/detect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Detect < Base
'`%<first_method>s[%<index>i]`.'
INDEX_REVERSE_MSG = 'Use `reverse.%<prefer>s` instead of ' \
'`%<first_method>s[%<index>i]`.'
RESTRICT_ON_SEND = %i[first last []].freeze

def_node_matcher :detect_candidate?, <<~PATTERN
{
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/end_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/fixed_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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} $...)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/flat_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FlatMap < Base
extend AutoCorrector

MSG = 'Use `flat_map` instead of `%<method>s...%<flatten>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.'
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/performance/inefficient_hash_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/performance/io_readlines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class IoReadlines < Base
extend AutoCorrector

MSG = 'Use `%<good>s` instead of `%<bad>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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/range_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RangeInclude < Base
extend AutoCorrector

MSG = 'Use `Range#cover?` instead of `Range#%<bad_method>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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/redundant_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/redundant_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RedundantMerge < Base

AREF_ASGN = '%<receiver>s[%<key>s] = %<value>s'
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[merge!].freeze

WITH_MODIFIER_CORRECTION = <<~RUBY
%<keyword>s %<condition>s
Expand Down
8 changes: 2 additions & 6 deletions lib/rubocop/cop/performance/redundant_string_chars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class RedundantStringChars < Base
extend AutoCorrector

MSG = 'Use `%<good_method>s` instead of `%<bad_method>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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/reverse_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/reverse_first.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ReverseFirst < Base
extend AutoCorrector

MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
RESTRICT_ON_SEND = %i[first].freeze

def_node_matcher :reverse_first_candidate?, <<~PATTERN
(send $(send _ :reverse) :first (int _)?)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/squeeze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Squeeze < Base
extend AutoCorrector

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[gsub gsub!].freeze

PREFERRED_METHODS = {
gsub: :squeeze,
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/start_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/string_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/string_replacement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class StringReplacement < Base
extend AutoCorrector

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[gsub gsub!].freeze
DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/.freeze
DELETE = 'delete'
TR = 'tr'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Sum < Base
extend AutoCorrector

MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
RESTRICT_ON_SEND = %i[inject reduce sum].freeze

def_node_matcher :sum_candidate?, <<~PATTERN
(send _ ${:inject :reduce} $_init ? ${(sym :+) (block_pass (sym :+))})
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/times_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TimesMap < Base
MESSAGE = 'Use `Array.new(%<count>s)` with a block ' \
'instead of `.times.%<map_or_collect>s`'
MESSAGE_ONLY_IF = 'only if `%<count>s` is always 0 or more'
RESTRICT_ON_SEND = %i[map collect].freeze

def on_send(node)
check(node)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/unfreeze_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/uri_default_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UriDefaultParser < Base

MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
'`%<double_colon>sURI::Parser.new`.'
RESTRICT_ON_SEND = %i[new].freeze

def_node_matcher :uri_parser_new?, <<~PATTERN
(send
Expand Down
2 changes: 1 addition & 1 deletion rubocop-performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5718984

Please sign in to comment.