rubocop-performance 1.22.0 → 1.23.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: 786f01d21be4d9f083fc007c02f4138145970d85b68776aca83fc305bd4f5152
4
- data.tar.gz: b15886f0f524d72de2319644d88a46058fe056476a52eef78eca7349e4e142eb
3
+ metadata.gz: 40d68e8bad13725b44f9a32555e63071765f0d4720439aba66f8b1c2ae61d803
4
+ data.tar.gz: 02f248b67961365ac64c7f5f99337d03d7a5dc9d57e36dd9ddccd3d2a16b75fb
5
5
  SHA512:
6
- metadata.gz: 940f52f35280335d6417096c43344e9d582fa22e9a09e7dd60cd08f19f1c68740052a2810dc9d082e5ee66b56c6c631eeb2cc1ee45ccd896bd58d654fdd4b523
7
- data.tar.gz: 483d5eedb23cc666c15d5c4bdb6b30f0c7b001547d38406c89ecf18547e0a0ae6c73fa9795f00b260921787b4ffee1c4d628434394c587d7744253759071a2b0
6
+ metadata.gz: 0bc99f145e03f6942e915b3edb51280821f66118d86d3b41bc87bc02ca4225ab8696510b7e77575fc2f05601cdc571a207b7d3c9eddc93e7ad238bc606ce3746
7
+ data.tar.gz: 91b71a5204257159d636b44dc003f2cc1c2b2bc6f67a17d9054bcdef2921926e3e5b729191107422abe807ac9628d3efa9db9550866233e07a88fb67f4d252b6
data/config/default.yml CHANGED
@@ -326,6 +326,12 @@ Performance/StartWith:
326
326
  VersionAdded: '0.36'
327
327
  VersionChanged: '1.10'
328
328
 
329
+ Performance/StringBytesize:
330
+ Description: "Use `String#bytesize` instead of calculating the size of the bytes array."
331
+ Safe: false
332
+ Enabled: 'pending'
333
+ VersionAdded: '1.23'
334
+
329
335
  Performance/StringIdentifierArgument:
330
336
  Description: 'Use symbol identifier argument instead of string identifier argument.'
331
337
  Enabled: pending
@@ -3,22 +3,28 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Performance
6
- # Identifies places where string argument to `BigDecimal` should be
7
- # converted to numeric. Initializing from Integer is faster
8
- # than from String for BigDecimal.
6
+ # Identifies places where a float argument to BigDecimal should be converted to a string.
7
+ # Initializing from String is faster than from Float for BigDecimal.
8
+ #
9
+ # Also identifies places where an integer string argument to BigDecimal should be converted to
10
+ # an integer. Initializing from Integer is faster than from String for BigDecimal.
9
11
  #
10
12
  # @example
11
13
  # # bad
12
- # BigDecimal('1', 2)
13
- # BigDecimal('4', 6)
14
+ # BigDecimal(1.2, 3, exception: true)
15
+ # 4.5.to_d(6, exception: true)
16
+ #
17
+ # # good
14
18
  # BigDecimal('1.2', 3, exception: true)
15
19
  # BigDecimal('4.5', 6, exception: true)
16
20
  #
21
+ # # bad
22
+ # BigDecimal('1', 2)
23
+ # BigDecimal('4', 6)
24
+ #
17
25
  # # good
18
26
  # BigDecimal(1, 2)
19
27
  # 4.to_d(6)
20
- # BigDecimal(1.2, 3, exception: true)
21
- # 4.5.to_d(6, exception: true)
22
28
  #
23
29
  class BigDecimalWithNumericArgument < Base
24
30
  extend AutoCorrector
@@ -26,30 +32,45 @@ module RuboCop
26
32
 
27
33
  minimum_target_ruby_version 3.1
28
34
 
29
- MSG = 'Convert string literal to numeric and pass it to `BigDecimal`.'
35
+ MSG_FROM_FLOAT_TO_STRING = 'Convert float literal to string and pass it to `BigDecimal`.'
36
+ MSG_FROM_INTEGER_TO_STRING = 'Convert string literal to integer and pass it to `BigDecimal`.'
30
37
  RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze
31
38
 
32
- def_node_matcher :big_decimal_with_numeric_argument?, <<~PATTERN
33
- (send nil? :BigDecimal $str_type? ...)
39
+ def_node_matcher :big_decimal_with_numeric_argument, <<~PATTERN
40
+ (send nil? :BigDecimal ${float_type? str_type?} ...)
34
41
  PATTERN
35
42
 
36
- def_node_matcher :to_d?, <<~PATTERN
37
- (send [!nil? $str_type?] :to_d ...)
43
+ def_node_matcher :to_d, <<~PATTERN
44
+ (send [!nil? ${float_type? str_type?}] :to_d ...)
38
45
  PATTERN
39
46
 
47
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
40
48
  def on_send(node)
41
- if (string = big_decimal_with_numeric_argument?(node))
42
- add_offense(string.source_range) do |corrector|
43
- corrector.replace(string, string.value)
49
+ if (numeric = big_decimal_with_numeric_argument(node))
50
+ if numeric.numeric_type?
51
+ add_offense(numeric, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
52
+ corrector.wrap(numeric, "'", "'")
53
+ end
54
+ elsif numeric.value.match?(/\A\d+\z/)
55
+ add_offense(numeric, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
56
+ corrector.replace(numeric, numeric.value)
57
+ end
44
58
  end
45
- elsif (string_to_d = to_d?(node))
46
- add_offense(string_to_d.source_range) do |corrector|
47
- big_decimal_args = node.arguments.map(&:source).unshift(string_to_d.value).join(', ')
59
+ elsif (numeric_to_d = to_d(node))
60
+ if numeric_to_d.numeric_type?
61
+ add_offense(numeric_to_d, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
62
+ big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')
48
63
 
49
- corrector.replace(node, "BigDecimal(#{big_decimal_args})")
64
+ corrector.replace(node, "BigDecimal(#{big_decimal_args})")
65
+ end
66
+ elsif numeric_to_d.value.match?(/\A\d+\z/)
67
+ add_offense(numeric_to_d, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
68
+ corrector.replace(node, "#{numeric_to_d.value}.to_d")
69
+ end
50
70
  end
51
71
  end
52
72
  end
73
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
53
74
  end
54
75
  end
55
76
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Performance
6
+ # Checks for calls to `#bytes` counting method and suggests using `bytesize` instead.
7
+ # The `bytesize` method is more efficient and directly returns the size in bytes,
8
+ # avoiding the intermediate array allocation that `bytes.size` incurs.
9
+ #
10
+ # @safety
11
+ # This cop is unsafe because it assumes that the receiver
12
+ # responds to `#bytesize` method.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # string_var.bytes.count
17
+ # "foobar".bytes.size
18
+ #
19
+ # # good
20
+ # string_var.bytesize
21
+ # "foobar".bytesize
22
+ class StringBytesize < Base
23
+ extend AutoCorrector
24
+
25
+ MSG = 'Use `String#bytesize` instead of calculating the size of the bytes array.'
26
+ RESTRICT_ON_SEND = %i[size length count].freeze
27
+
28
+ def_node_matcher :string_bytes_method?, <<~MATCHER
29
+ (call (call !{nil? int} :bytes) {:size :length :count})
30
+ MATCHER
31
+
32
+ def on_send(node)
33
+ string_bytes_method?(node) do
34
+ range = node.receiver.loc.selector.begin.join(node.source_range.end)
35
+
36
+ add_offense(range) do |corrector|
37
+ corrector.replace(range, 'bytesize')
38
+ end
39
+ end
40
+ end
41
+ alias on_csend on_send
42
+ end
43
+ end
44
+ end
45
+ end
@@ -159,7 +159,7 @@ module RuboCop
159
159
 
160
160
  def array_literal?(node)
161
161
  receiver = node.children.first
162
- receiver&.literal? && receiver&.array_type?
162
+ receiver&.literal? && receiver.array_type?
163
163
  end
164
164
 
165
165
  def autocorrect(corrector, init, range)
@@ -44,6 +44,7 @@ require_relative 'performance/select_map'
44
44
  require_relative 'performance/size'
45
45
  require_relative 'performance/sort_reverse'
46
46
  require_relative 'performance/squeeze'
47
+ require_relative 'performance/string_bytesize'
47
48
  require_relative 'performance/start_with'
48
49
  require_relative 'performance/string_identifier_argument'
49
50
  require_relative 'performance/string_include'
@@ -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.22.0'
7
+ STRING = '1.23.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-performance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.22.0
4
+ version: 1.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2024-09-16 00:00:00.000000000 Z
12
+ date: 2024-11-14 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rubocop
@@ -112,6 +111,7 @@ files:
112
111
  - lib/rubocop/cop/performance/sort_reverse.rb
113
112
  - lib/rubocop/cop/performance/squeeze.rb
114
113
  - lib/rubocop/cop/performance/start_with.rb
114
+ - lib/rubocop/cop/performance/string_bytesize.rb
115
115
  - lib/rubocop/cop/performance/string_identifier_argument.rb
116
116
  - lib/rubocop/cop/performance/string_include.rb
117
117
  - lib/rubocop/cop/performance/string_replacement.rb
@@ -130,10 +130,9 @@ 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.22/
133
+ documentation_uri: https://docs.rubocop.org/rubocop-performance/1.23/
134
134
  bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
135
135
  rubygems_mfa_required: 'true'
136
- post_install_message:
137
136
  rdoc_options: []
138
137
  require_paths:
139
138
  - lib
@@ -148,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
147
  - !ruby/object:Gem::Version
149
148
  version: '0'
150
149
  requirements: []
151
- rubygems_version: 3.2.33
152
- signing_key:
150
+ rubygems_version: 3.6.0.dev
153
151
  specification_version: 4
154
152
  summary: Automatic performance checking tool for Ruby code.
155
153
  test_files: []