rubocop-performance 1.21.1 → 1.22.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +4 -1
- data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +43 -19
- data/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +3 -0
- data/lib/rubocop/cop/performance/double_start_end_with.rb +10 -10
- data/lib/rubocop/cop/performance/redundant_merge.rb +1 -3
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f39eb7cfffa29b6374302e0fae0b46f55a1ccce24798fa2609c4f80c3dea277
|
4
|
+
data.tar.gz: 87b1a993f80ae214d981e95cc5a7645f4ea92dca4255f64fa53ec29f5959500e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99a4776f28e154fe3017ed3e97be8b9121a262e8f40af1aed8d4085fc5ba4e1be7499ea12fe3d76e6858ac45ff88e031dfce6dc129743b52d5fd5815da596775
|
7
|
+
data.tar.gz: 9160234e8fcdf23f550d41d6429910e09276806f40ec494331d52e5f8480b2d066ac8ede04bc0144c8dc9e9e3a2fce16a8b64746e9ed0c794c278e3426d76a7b
|
data/config/default.yml
CHANGED
@@ -32,8 +32,11 @@ Performance/BindCall:
|
|
32
32
|
|
33
33
|
Performance/BlockGivenWithExplicitBlock:
|
34
34
|
Description: 'Check block argument explicitly instead of using `block_given?`.'
|
35
|
-
|
35
|
+
# This cop was created due to a mistake in microbenchmark.
|
36
|
+
# https://github.com/rubocop/rubocop-performance/issues/385
|
37
|
+
Enabled: false
|
36
38
|
VersionAdded: '1.9'
|
39
|
+
VersionChanged: '1.22'
|
37
40
|
|
38
41
|
Performance/Caller:
|
39
42
|
Description: >-
|
@@ -3,50 +3,74 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Performance
|
6
|
-
# Identifies places where
|
7
|
-
#
|
8
|
-
#
|
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
|
-
# 4.to_d(6)
|
14
14
|
# BigDecimal(1.2, 3, exception: true)
|
15
15
|
# 4.5.to_d(6, exception: true)
|
16
16
|
#
|
17
17
|
# # good
|
18
|
-
# BigDecimal('1', 2)
|
19
|
-
# BigDecimal('4', 6)
|
20
18
|
# BigDecimal('1.2', 3, exception: true)
|
21
19
|
# BigDecimal('4.5', 6, exception: true)
|
22
20
|
#
|
21
|
+
# # bad
|
22
|
+
# BigDecimal('1', 2)
|
23
|
+
# BigDecimal('4', 6)
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# BigDecimal(1, 2)
|
27
|
+
# 4.to_d(6)
|
28
|
+
#
|
23
29
|
class BigDecimalWithNumericArgument < Base
|
24
30
|
extend AutoCorrector
|
31
|
+
extend TargetRubyVersion
|
32
|
+
|
33
|
+
minimum_target_ruby_version 3.1
|
25
34
|
|
26
|
-
|
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`.'
|
27
37
|
RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze
|
28
38
|
|
29
|
-
def_node_matcher :big_decimal_with_numeric_argument
|
30
|
-
(send nil? :BigDecimal $
|
39
|
+
def_node_matcher :big_decimal_with_numeric_argument, <<~PATTERN
|
40
|
+
(send nil? :BigDecimal ${float_type? str_type?} ...)
|
31
41
|
PATTERN
|
32
42
|
|
33
|
-
def_node_matcher :to_d
|
34
|
-
(send [!nil? $
|
43
|
+
def_node_matcher :to_d, <<~PATTERN
|
44
|
+
(send [!nil? ${float_type? str_type?}] :to_d ...)
|
35
45
|
PATTERN
|
36
46
|
|
47
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
37
48
|
def on_send(node)
|
38
|
-
if (numeric = big_decimal_with_numeric_argument
|
39
|
-
|
40
|
-
|
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
|
41
58
|
end
|
42
|
-
elsif (numeric_to_d = to_d
|
43
|
-
|
44
|
-
|
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(', ')
|
45
63
|
|
46
|
-
|
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
|
47
70
|
end
|
48
71
|
end
|
49
72
|
end
|
73
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
50
74
|
end
|
51
75
|
end
|
52
76
|
end
|
@@ -6,6 +6,9 @@ module RuboCop
|
|
6
6
|
# Identifies unnecessary use of a `block_given?` where explicit check
|
7
7
|
# of block argument would suffice.
|
8
8
|
#
|
9
|
+
# NOTE: This cop produces code with significantly worse performance when a
|
10
|
+
# block is being passed to the method and as such should not be enabled.
|
11
|
+
#
|
9
12
|
# @example
|
10
13
|
# # bad
|
11
14
|
# def method(&block)
|
@@ -41,7 +41,7 @@ module RuboCop
|
|
41
41
|
class DoubleStartEndWith < Base
|
42
42
|
extend AutoCorrector
|
43
43
|
|
44
|
-
MSG = 'Use `%<
|
44
|
+
MSG = 'Use `%<replacement>s` instead of `%<original_code>s`.'
|
45
45
|
|
46
46
|
def on_or(node)
|
47
47
|
receiver, method, first_call_args, second_call_args = process_source(node)
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
50
50
|
|
51
51
|
combined_args = combine_args(first_call_args, second_call_args)
|
52
52
|
|
53
|
-
add_offense(node, message: message(node, receiver, method, combined_args)) do |corrector|
|
53
|
+
add_offense(node, message: message(node, receiver, first_call_args, method, combined_args)) do |corrector|
|
54
54
|
autocorrect(corrector, first_call_args, second_call_args, combined_args)
|
55
55
|
end
|
56
56
|
end
|
@@ -73,10 +73,10 @@ module RuboCop
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
def message(node, receiver, method, combined_args)
|
77
|
-
|
78
|
-
|
79
|
-
)
|
76
|
+
def message(node, receiver, first_call_args, method, combined_args)
|
77
|
+
dot = first_call_args.first.parent.send_type? ? '.' : '&.'
|
78
|
+
replacement = "#{receiver.source}#{dot}#{method}(#{combined_args})"
|
79
|
+
format(MSG, replacement: replacement, original_code: node.source)
|
80
80
|
end
|
81
81
|
|
82
82
|
def combine_args(first_call_args, second_call_args)
|
@@ -89,16 +89,16 @@ module RuboCop
|
|
89
89
|
|
90
90
|
def_node_matcher :two_start_end_with_calls, <<~PATTERN
|
91
91
|
(or
|
92
|
-
(
|
93
|
-
(
|
92
|
+
(call $_recv [{:start_with? :end_with?} $_method] $...)
|
93
|
+
(call _recv _method $...))
|
94
94
|
PATTERN
|
95
95
|
|
96
96
|
def_node_matcher :check_with_active_support_aliases, <<~PATTERN
|
97
97
|
(or
|
98
|
-
(
|
98
|
+
(call $_recv
|
99
99
|
[{:start_with? :starts_with? :end_with? :ends_with?} $_method]
|
100
100
|
$...)
|
101
|
-
(
|
101
|
+
(call _recv _method $...))
|
102
102
|
PATTERN
|
103
103
|
end
|
104
104
|
end
|
@@ -130,9 +130,7 @@ module RuboCop
|
|
130
130
|
end
|
131
131
|
|
132
132
|
def rewrite_with_modifier(node, parent, new_source)
|
133
|
-
|
134
|
-
# https://github.com/rubocop/rubocop/commit/02d1e5b
|
135
|
-
indent = ' ' * (configured_indentation_width || 2)
|
133
|
+
indent = ' ' * configured_indentation_width
|
136
134
|
padding = "\n#{indent + leading_spaces(node)}"
|
137
135
|
new_source.gsub!("\n", padding)
|
138
136
|
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-performance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.22.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
- Jonas Arvidsson
|
9
9
|
- Yuji Nakayama
|
10
|
+
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2024-
|
13
|
+
date: 2024-09-17 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rubocop
|
@@ -129,9 +130,10 @@ metadata:
|
|
129
130
|
homepage_uri: https://docs.rubocop.org/rubocop-performance/
|
130
131
|
changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
|
131
132
|
source_code_uri: https://github.com/rubocop/rubocop-performance/
|
132
|
-
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.
|
133
|
+
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.22/
|
133
134
|
bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
|
134
135
|
rubygems_mfa_required: 'true'
|
136
|
+
post_install_message:
|
135
137
|
rdoc_options: []
|
136
138
|
require_paths:
|
137
139
|
- lib
|
@@ -146,7 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
148
|
- !ruby/object:Gem::Version
|
147
149
|
version: '0'
|
148
150
|
requirements: []
|
149
|
-
rubygems_version: 3.
|
151
|
+
rubygems_version: 3.2.33
|
152
|
+
signing_key:
|
150
153
|
specification_version: 4
|
151
154
|
summary: Automatic performance checking tool for Ruby code.
|
152
155
|
test_files: []
|