rubocop-performance 1.20.1 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1687f31268dd621e7a4e82fb11b5ae6927f208936df7b7136b5a80a1d4cfe547
4
- data.tar.gz: f8bf3aaa6084017f9aa368440e57f3e752f203d801132b8cad848634dd5ecb3d
3
+ metadata.gz: c00ee718f283199f3c4f53b54071998b356f7239edcd3c84015bac94ad1b05dc
4
+ data.tar.gz: a5e7d011cd090fd5d30eb0ef9e12acabb93a993cc3acb46129da46c37294e1ba
5
5
  SHA512:
6
- metadata.gz: 7d5e072526c9a1ef7f5636b24845ec09b83548a1e06cf6b12b6b5cabdf4524d536dbc06e3e007f344a2584a6014d29ae19f29be80574a730b88b961d7f33d358
7
- data.tar.gz: f10756696005feffe81f9bd471f52f11d69ac193036f65bffe5be7e4d1bb0a95990ccfa9a6278b2128377efc4d2097a74e5b8f896f56ac7c12856f78b9447a83
6
+ metadata.gz: 711efd2eae8c54dc1b567e198360038fb16eee00c2e6a61db7dbcfb063f219618e36809ac5679cc531e8fe84245ea1f66e6e8eb9fbed5cbb9dcc22f57c10dab6
7
+ data.tar.gz: 5618f678392ffb6f93b34388e3ef060c017f7e70b53d992ebe48d2bb49b7ec5570adc7b21f5f22f0511cddc4fb1dead044904d7bcf40949191ee6ed8b884565f
data/config/default.yml CHANGED
@@ -55,9 +55,10 @@ Performance/Casecmp:
55
55
  Description: >-
56
56
  Use `casecmp` rather than `downcase ==`, `upcase ==`, `== downcase`, or `== upcase`..
57
57
  Reference: 'https://github.com/fastruby/fast-ruby#stringcasecmp-vs--stringcasecmp-vs-stringdowncase---code'
58
- Enabled: true
58
+ Enabled: false
59
59
  Safe: false
60
60
  VersionAdded: '0.36'
61
+ VersionChanged: '1.21'
61
62
 
62
63
  Performance/ChainArrayAllocation:
63
64
  Description: >-
@@ -6,6 +6,12 @@ module RuboCop
6
6
  # Identifies places where a case-insensitive string comparison
7
7
  # can better be implemented using `casecmp`.
8
8
  #
9
+ # This cop is disabled by default because `String#casecmp` only works with
10
+ # ASCII characters. See https://github.com/rubocop/rubocop/issues/9753.
11
+ #
12
+ # If you are working only with ASCII characters, then this cop can be
13
+ # safely enabled.
14
+ #
9
15
  # @safety
10
16
  # This cop is unsafe because `String#casecmp` and `String#casecmp?` behave
11
17
  # differently when using Non-ASCII characters.
@@ -19,8 +19,6 @@ module RuboCop
19
19
  # array.map! { |x| x.downcase }
20
20
  # array
21
21
  class ChainArrayAllocation < Base
22
- include RangeHelp
23
-
24
22
  # These methods return a new array but only sometimes. They must be
25
23
  # called with an argument. For example:
26
24
  #
@@ -54,7 +52,7 @@ module RuboCop
54
52
  def_node_matcher :chain_array_allocation?, <<~PATTERN
55
53
  (send {
56
54
  (send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar send})
57
- (block (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
55
+ ({block numblock} (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
58
56
  (send _ $%RETURNS_NEW_ARRAY ...)
59
57
  } $%HAS_MUTATION_ALTERNATIVE ...)
60
58
  PATTERN
@@ -62,12 +60,21 @@ module RuboCop
62
60
  def on_send(node)
63
61
  chain_array_allocation?(node) do |fm, sm|
64
62
  return if node.each_descendant(:send).any? { |descendant| descendant.method?(:lazy) }
63
+ return if node.method?(:select) && !enumerable_select_method?(node.receiver)
65
64
 
66
- range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
65
+ range = node.loc.selector.begin.join(node.source_range.end)
67
66
 
68
67
  add_offense(range, message: format(MSG, method: fm, second_method: sm))
69
68
  end
70
69
  end
70
+
71
+ private
72
+
73
+ def enumerable_select_method?(node)
74
+ # NOTE: `QueryMethods#select` in Rails accepts positional arguments, whereas `Enumerable#select` does not.
75
+ # This difference can be utilized to reduce the knowledge requirements related to `select`.
76
+ (node.block_type? || node.numblock_type?) && node.send_node.arguments.empty?
77
+ end
71
78
  end
72
79
  end
73
80
  end
@@ -56,7 +56,8 @@ module RuboCop
56
56
  def_node_matcher :redundant_regex?, <<~PATTERN
57
57
  {(call $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt)))
58
58
  (send (regexp (str $#literal_at_end?) (regopt)) {:match :match?} $_)
59
- (match-with-lvasgn (regexp (str $#literal_at_end?) (regopt)) $_)}
59
+ ({send match-with-lvasgn} (regexp (str $#literal_at_end?) (regopt)) $_)
60
+ (send (regexp (str $#literal_at_end?) (regopt)) :=~ $_)}
60
61
  PATTERN
61
62
 
62
63
  def on_send(node)
@@ -49,6 +49,8 @@ module RuboCop
49
49
  next unless body
50
50
 
51
51
  calls_to_report(argname, body).each do |blockcall|
52
+ next if blockcall.block_literal?
53
+
52
54
  add_offense(blockcall, message: format(MSG, argname: argname)) do |corrector|
53
55
  autocorrect(corrector, blockcall)
54
56
  end
@@ -248,7 +248,7 @@ module RuboCop
248
248
  end
249
249
 
250
250
  def correct_operator(corrector, recv, arg, oper = nil)
251
- op_range = correction_range(recv, arg)
251
+ op_range = recv.source_range.end.join(arg.source_range.begin)
252
252
 
253
253
  replace_with_match_predicate_method(corrector, recv, arg, op_range)
254
254
 
@@ -271,13 +271,6 @@ module RuboCop
271
271
  corrector.replace(recv, arg.source)
272
272
  corrector.replace(arg, recv.source)
273
273
  end
274
-
275
- def correction_range(recv, arg)
276
- buffer = processed_source.buffer
277
- op_begin_pos = recv.source_range.end_pos
278
- op_end_pos = arg.source_range.begin_pos
279
- Parser::Source::Range.new(buffer, op_begin_pos, op_end_pos)
280
- end
281
274
  end
282
275
  end
283
276
  end
@@ -56,7 +56,8 @@ module RuboCop
56
56
  def_node_matcher :redundant_regex?, <<~PATTERN
57
57
  {(call $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
58
58
  (send (regexp (str $#literal_at_start?) (regopt)) {:match :match?} $_)
59
- (match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)}
59
+ (match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)
60
+ (send (regexp (str $#literal_at_start?) (regopt)) :=~ $_)}
60
61
  PATTERN
61
62
 
62
63
  def on_send(node)
@@ -16,13 +16,19 @@ module RuboCop
16
16
  # send('do_something')
17
17
  # attr_accessor 'do_something'
18
18
  # instance_variable_get('@ivar')
19
- # const_get("string_#{interpolation}")
19
+ # respond_to?("string_#{interpolation}")
20
20
  #
21
21
  # # good
22
22
  # send(:do_something)
23
23
  # attr_accessor :do_something
24
24
  # instance_variable_get(:@ivar)
25
- # const_get(:"string_#{interpolation}")
25
+ # respond_to?(:"string_#{interpolation}")
26
+ #
27
+ # # good - these methods don't support namespaced symbols
28
+ # const_get("#{module_path}::Base")
29
+ # const_source_location("#{module_path}::Base")
30
+ # const_defined?("#{module_path}::Base")
31
+ #
26
32
  #
27
33
  class StringIdentifierArgument < Base
28
34
  extend AutoCorrector
@@ -34,6 +40,8 @@ module RuboCop
34
40
  protected public public_constant module_function
35
41
  ].freeze
36
42
 
43
+ INTERPOLATION_IGNORE_METHODS = %i[const_get const_source_location const_defined?].freeze
44
+
37
45
  TWO_ARGUMENTS_METHOD = :alias_method
38
46
  MULTIPLE_ARGUMENTS_METHODS = %i[
39
47
  attr_accessor attr_reader attr_writer private private_constant
@@ -44,14 +52,14 @@ module RuboCop
44
52
  # And `attr` may not be used because `Style/Attr` registers an offense.
45
53
  # https://github.com/rubocop/rubocop-performance/issues/278
46
54
  RESTRICT_ON_SEND = (%i[
47
- class_variable_defined? const_defined? const_get const_set const_source_location
55
+ class_variable_defined? const_set
48
56
  define_method instance_method method_defined? private_class_method? private_method_defined?
49
57
  protected_method_defined? public_class_method public_instance_method public_method_defined?
50
58
  remove_class_variable remove_method undef_method class_variable_get class_variable_set
51
59
  deprecate_constant remove_const ruby2_keywords define_singleton_method instance_variable_defined?
52
60
  instance_variable_get instance_variable_set method public_method public_send remove_instance_variable
53
61
  respond_to? send singleton_method __send__
54
- ] + COMMAND_METHODS).freeze
62
+ ] + COMMAND_METHODS + INTERPOLATION_IGNORE_METHODS).freeze
55
63
 
56
64
  def on_send(node)
57
65
  return if COMMAND_METHODS.include?(node.method_name) && node.receiver
@@ -75,9 +83,13 @@ module RuboCop
75
83
  [node.first_argument]
76
84
  end
77
85
 
78
- arguments.compact.filter do |argument|
79
- argument.str_type? || argument.dstr_type?
80
- end
86
+ arguments.compact.filter { |argument| string_argument_compatible?(argument, node) }
87
+ end
88
+
89
+ def string_argument_compatible?(argument, node)
90
+ return true if argument.str_type?
91
+
92
+ argument.dstr_type? && INTERPOLATION_IGNORE_METHODS.none? { |method| node.method?(method) }
81
93
  end
82
94
 
83
95
  def register_offense(argument, argument_value)
@@ -29,7 +29,8 @@ module RuboCop
29
29
  def_node_matcher :redundant_regex?, <<~PATTERN
30
30
  {(call $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
31
31
  (send (regexp (str $#literal?) (regopt)) {:match :match? :===} $_)
32
- (match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
32
+ (match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)
33
+ (send (regexp (str $#literal?) (regopt)) :=~ $_)}
33
34
  PATTERN
34
35
 
35
36
  # rubocop:disable Metrics/AbcSize
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Performance
5
5
  # This module holds the RuboCop Performance version information.
6
6
  module Version
7
- STRING = '1.20.1'
7
+ STRING = '1.21.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-performance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.20.1
4
+ version: 1.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-12-25 00:00:00.000000000 Z
13
+ date: 2024-03-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -38,7 +38,7 @@ dependencies:
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: 1.30.0
41
+ version: 1.31.1
42
42
  - - "<"
43
43
  - !ruby/object:Gem::Version
44
44
  version: '2.0'
@@ -48,7 +48,7 @@ dependencies:
48
48
  requirements:
49
49
  - - ">="
50
50
  - !ruby/object:Gem::Version
51
- version: 1.30.0
51
+ version: 1.31.1
52
52
  - - "<"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.0'
@@ -130,7 +130,7 @@ metadata:
130
130
  homepage_uri: https://docs.rubocop.org/rubocop-performance/
131
131
  changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
132
132
  source_code_uri: https://github.com/rubocop/rubocop-performance/
133
- documentation_uri: https://docs.rubocop.org/rubocop-performance/1.20/
133
+ documentation_uri: https://docs.rubocop.org/rubocop-performance/1.21/
134
134
  bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
135
135
  rubygems_mfa_required: 'true'
136
136
  post_install_message:
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
150
  requirements: []
151
- rubygems_version: 3.1.6
151
+ rubygems_version: 3.5.3
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Automatic performance checking tool for Ruby code.