rubocop-performance 1.16.0 → 1.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/config/default.yml +1 -0
- data/lib/rubocop/cop/performance/case_when_splat.rb +2 -2
- data/lib/rubocop/cop/performance/count.rb +1 -1
- data/lib/rubocop/cop/performance/detect.rb +3 -6
- data/lib/rubocop/cop/performance/double_start_end_with.rb +2 -2
- data/lib/rubocop/cop/performance/end_with.rb +1 -1
- data/lib/rubocop/cop/performance/flat_map.rb +6 -4
- data/lib/rubocop/cop/performance/inefficient_hash_search.rb +1 -1
- data/lib/rubocop/cop/performance/io_readlines.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_block_call.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb +33 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +2 -2
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +2 -2
- data/lib/rubocop/cop/performance/regexp_match.rb +12 -12
- data/lib/rubocop/cop/performance/reverse_first.rb +1 -1
- data/lib/rubocop/cop/performance/select_map.rb +1 -1
- data/lib/rubocop/cop/performance/squeeze.rb +1 -1
- data/lib/rubocop/cop/performance/start_with.rb +1 -1
- data/lib/rubocop/cop/performance/string_identifier_argument.rb +15 -10
- data/lib/rubocop/cop/performance/string_include.rb +1 -1
- data/lib/rubocop/cop/performance/string_replacement.rb +1 -1
- data/lib/rubocop/cop/performance/sum.rb +1 -1
- data/lib/rubocop/cop/performance/times_map.rb +1 -1
- data/lib/rubocop/cop/performance/uri_default_parser.rb +1 -1
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb37421ad9500c9f9a40c5b46c46712bddee83783481b54ae1d3382f87ab9e2
|
4
|
+
data.tar.gz: 4219c118e704e6ef9b8221716354c734591a9fa8e914707d5271e309e38abb24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec972228315b462c622566b1265f177220cb9fe8390fd601a52556bbe34a05a934832d1fd7de3b2bfcc8b7b0529be33fa4f04a9ca050fedd9b3bdfcbf0d6032c
|
7
|
+
data.tar.gz: f5f50079efd8bb81b25127c9d65453636e15dfc2724482cbb3bc3710baa390050f028d442e122fbb5efde790b6354764202c13b1d6752e88ae7369394e7846cb
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ Performance optimization analysis for your projects, as an extension to [RuboCop
|
|
11
11
|
Just install the `rubocop-performance` gem
|
12
12
|
|
13
13
|
```sh
|
14
|
-
gem install rubocop-performance
|
14
|
+
$ gem install rubocop-performance
|
15
15
|
```
|
16
16
|
|
17
17
|
or if you use bundler put this in your `Gemfile`
|
@@ -47,7 +47,7 @@ cops together with the standard cops.
|
|
47
47
|
### Command line
|
48
48
|
|
49
49
|
```sh
|
50
|
-
rubocop --require rubocop-performance
|
50
|
+
$ rubocop --require rubocop-performance
|
51
51
|
```
|
52
52
|
|
53
53
|
### Rake task
|
data/config/default.yml
CHANGED
@@ -99,7 +99,7 @@ module RuboCop
|
|
99
99
|
|
100
100
|
def inline_fix_branch(corrector, when_node)
|
101
101
|
conditions = when_node.conditions
|
102
|
-
range = range_between(conditions[0].
|
102
|
+
range = range_between(conditions[0].source_range.begin_pos, conditions[-1].source_range.end_pos)
|
103
103
|
|
104
104
|
corrector.replace(range, replacement(conditions))
|
105
105
|
end
|
@@ -110,7 +110,7 @@ module RuboCop
|
|
110
110
|
return if when_branches.one?
|
111
111
|
|
112
112
|
corrector.remove(when_branch_range(when_node))
|
113
|
-
corrector.insert_after(when_branches.last
|
113
|
+
corrector.insert_after(when_branches.last, reordering_correction(when_node))
|
114
114
|
end
|
115
115
|
|
116
116
|
def reordering_correction(when_node)
|
@@ -110,7 +110,7 @@ module RuboCop
|
|
110
110
|
|
111
111
|
def negate_block_pass_reject(corrector, node)
|
112
112
|
corrector.replace(
|
113
|
-
node.receiver.
|
113
|
+
node.receiver.source_range.with(begin_pos: node.receiver.loc.begin.begin_pos),
|
114
114
|
negate_block_pass_as_inline_block(node.receiver)
|
115
115
|
)
|
116
116
|
end
|
@@ -8,12 +8,9 @@ module RuboCop
|
|
8
8
|
# `detect` instead.
|
9
9
|
#
|
10
10
|
# @safety
|
11
|
-
# This cop is unsafe because
|
12
|
-
# `
|
13
|
-
#
|
14
|
-
# frameworks. `Hash` and `ActiveRecord` do not implement a `detect`
|
15
|
-
# method and `find` has its own meaning. Correcting `Hash` and
|
16
|
-
# `ActiveRecord` methods with this cop should be considered unsafe.
|
11
|
+
# This cop is unsafe because it assumes that the receiver is an
|
12
|
+
# `Array` or equivalent, but can't reliably detect it. For example,
|
13
|
+
# if the receiver is a `Hash`, it may report a false positive.
|
17
14
|
#
|
18
15
|
# @example
|
19
16
|
# # bad
|
@@ -58,8 +58,8 @@ module RuboCop
|
|
58
58
|
private
|
59
59
|
|
60
60
|
def autocorrect(corrector, first_call_args, second_call_args, combined_args)
|
61
|
-
first_argument = first_call_args.first.
|
62
|
-
last_argument = second_call_args.last.
|
61
|
+
first_argument = first_call_args.first.source_range
|
62
|
+
last_argument = second_call_args.last.source_range
|
63
63
|
range = first_argument.join(last_argument)
|
64
64
|
|
65
65
|
corrector.replace(range, combined_args)
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
def_node_matcher :flat_map_candidate?, <<~PATTERN
|
29
29
|
(send
|
30
30
|
{
|
31
|
-
(block
|
31
|
+
$(block (send _ ${:collect :map}) ...)
|
32
32
|
$(send _ ${:collect :map} (block_pass _))
|
33
33
|
}
|
34
34
|
${:flatten :flatten!}
|
@@ -60,7 +60,8 @@ module RuboCop
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def register_offense(node, map_node, first_method, flatten, message)
|
63
|
-
|
63
|
+
map_send_node = map_node.block_type? ? map_node.send_node : map_node
|
64
|
+
range = range_between(map_send_node.loc.selector.begin_pos, node.source_range.end_pos)
|
64
65
|
message = format(message, method: first_method, flatten: flatten)
|
65
66
|
|
66
67
|
add_offense(range, message: message) do |corrector|
|
@@ -74,10 +75,11 @@ module RuboCop
|
|
74
75
|
|
75
76
|
return unless flatten_level
|
76
77
|
|
77
|
-
|
78
|
+
map_send_node = map_node.block_type? ? map_node.send_node : map_node
|
79
|
+
range = range_between(map_node.source_range.end_pos, node.source_range.end_pos)
|
78
80
|
|
79
81
|
corrector.remove(range)
|
80
|
-
corrector.replace(
|
82
|
+
corrector.replace(map_send_node.loc.selector, 'flat_map')
|
81
83
|
end
|
82
84
|
end
|
83
85
|
end
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
# Replace `keys.include?` or `values.include?` with the appropriate
|
58
58
|
# `key?`/`value?` method.
|
59
59
|
corrector.replace(
|
60
|
-
node
|
60
|
+
node,
|
61
61
|
"#{autocorrect_hash_expression(node)}.#{autocorrect_method(node)}(#{autocorrect_argument(node)})"
|
62
62
|
)
|
63
63
|
end
|
@@ -10,6 +10,16 @@ module RuboCop
|
|
10
10
|
# behavior is appropriately overridden in subclass. For example,
|
11
11
|
# `Range#===` returns `true` when argument is within the range.
|
12
12
|
#
|
13
|
+
# This cop has `AllowRegexpMatch` option and it is false by default because
|
14
|
+
# `regexp.match?('string')` often used in block changes to the opposite result:
|
15
|
+
#
|
16
|
+
# [source,ruby]
|
17
|
+
# ----
|
18
|
+
# [/pattern/].all? { |regexp| regexp.match?('pattern') } # => true
|
19
|
+
# [/pattern/].all? { |regexp| regexp =~ 'pattern' } # => true
|
20
|
+
# [/pattern/].all?('pattern') # => false
|
21
|
+
# ----
|
22
|
+
#
|
13
23
|
# @safety
|
14
24
|
# This cop is unsafe because `===` and `==` do not always behave the same.
|
15
25
|
#
|
@@ -22,6 +32,19 @@ module RuboCop
|
|
22
32
|
#
|
23
33
|
# # good
|
24
34
|
# items.all?(pattern)
|
35
|
+
# items.all?(Klass)
|
36
|
+
#
|
37
|
+
# @example AllowRegexpMatch: true (default)
|
38
|
+
#
|
39
|
+
# # good
|
40
|
+
# items.all? { |item| item =~ pattern }
|
41
|
+
# items.all? { |item| item.match?(pattern) }
|
42
|
+
#
|
43
|
+
# @example AllowRegexpMatch: false
|
44
|
+
#
|
45
|
+
# # bad
|
46
|
+
# items.all? { |item| item =~ pattern }
|
47
|
+
# items.all? { |item| item.match?(pattern) }
|
25
48
|
#
|
26
49
|
class RedundantEqualityComparisonBlock < Base
|
27
50
|
extend AutoCorrector
|
@@ -33,6 +56,7 @@ module RuboCop
|
|
33
56
|
|
34
57
|
TARGET_METHODS = %i[all? any? one? none?].freeze
|
35
58
|
COMPARISON_METHODS = %i[== === is_a? kind_of?].freeze
|
59
|
+
REGEXP_METHODS = %i[=~ match?].freeze
|
36
60
|
IS_A_METHODS = %i[is_a? kind_of?].freeze
|
37
61
|
|
38
62
|
def on_block(node)
|
@@ -60,7 +84,11 @@ module RuboCop
|
|
60
84
|
end
|
61
85
|
|
62
86
|
def use_equality_comparison_block?(block_body)
|
63
|
-
block_body.send_type?
|
87
|
+
return false unless block_body.send_type?
|
88
|
+
|
89
|
+
method_name = block_body.method_name
|
90
|
+
|
91
|
+
COMPARISON_METHODS.include?(method_name) || (!allow_regexp_match? && REGEXP_METHODS.include?(method_name))
|
64
92
|
end
|
65
93
|
|
66
94
|
def same_block_argument_and_is_a_argument?(block_body, block_argument)
|
@@ -99,6 +127,10 @@ module RuboCop
|
|
99
127
|
def offense_range(node)
|
100
128
|
node.send_node.loc.selector.join(node.source_range.end)
|
101
129
|
end
|
130
|
+
|
131
|
+
def allow_regexp_match?
|
132
|
+
cop_config.fetch('AllowRegexpMatch', true)
|
133
|
+
end
|
102
134
|
end
|
103
135
|
end
|
104
136
|
end
|
@@ -112,11 +112,11 @@ module RuboCop
|
|
112
112
|
new_source.gsub!(/\n/, padding)
|
113
113
|
end
|
114
114
|
|
115
|
-
corrector.replace(node
|
115
|
+
corrector.replace(node, new_source)
|
116
116
|
end
|
117
117
|
|
118
118
|
def correct_single_element(corrector, node, new_source)
|
119
|
-
corrector.replace(node
|
119
|
+
corrector.replace(node, new_source)
|
120
120
|
end
|
121
121
|
|
122
122
|
def to_assignments(receiver, pairs)
|
@@ -69,11 +69,11 @@ module RuboCop
|
|
69
69
|
private
|
70
70
|
|
71
71
|
def offense_range(receiver, node)
|
72
|
-
range_between(receiver.loc.selector.begin_pos, node.
|
72
|
+
range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos)
|
73
73
|
end
|
74
74
|
|
75
75
|
def correction_range(receiver, node)
|
76
|
-
range_between(receiver.loc.dot.begin_pos, node.
|
76
|
+
range_between(receiver.loc.dot.begin_pos, node.source_range.end_pos)
|
77
77
|
end
|
78
78
|
|
79
79
|
def build_message(method, args)
|
@@ -185,9 +185,9 @@ module RuboCop
|
|
185
185
|
|
186
186
|
def range_to_search_for_last_matches(match_node, body, scope_root)
|
187
187
|
expression = if modifier_form?(match_node)
|
188
|
-
match_node.parent.if_branch.
|
188
|
+
match_node.parent.if_branch.source_range
|
189
189
|
else
|
190
|
-
match_node.
|
190
|
+
match_node.source_range
|
191
191
|
end
|
192
192
|
|
193
193
|
match_node_pos = expression.begin_pos
|
@@ -199,15 +199,15 @@ module RuboCop
|
|
199
199
|
def next_match_pos(body, match_node_pos, scope_root)
|
200
200
|
node = search_match_nodes(body).find do |match|
|
201
201
|
begin_pos = if modifier_form?(match)
|
202
|
-
match.parent.if_branch.
|
202
|
+
match.parent.if_branch.source_range.begin_pos
|
203
203
|
else
|
204
|
-
match.
|
204
|
+
match.source_range.begin_pos
|
205
205
|
end
|
206
206
|
|
207
207
|
begin_pos > match_node_pos && scope_root(match) == scope_root
|
208
208
|
end
|
209
209
|
|
210
|
-
node ? node.
|
210
|
+
node ? node.source_range.begin_pos : Float::INFINITY
|
211
211
|
end
|
212
212
|
|
213
213
|
def modifier_form?(match_node)
|
@@ -216,7 +216,7 @@ module RuboCop
|
|
216
216
|
|
217
217
|
def find_last_match(body, range, scope_root)
|
218
218
|
last_matches(body).find do |ref|
|
219
|
-
ref_pos = ref.
|
219
|
+
ref_pos = ref.source_range.begin_pos
|
220
220
|
range.cover?(ref_pos) && scope_root(ref) == scope_root
|
221
221
|
end
|
222
222
|
end
|
@@ -248,8 +248,8 @@ module RuboCop
|
|
248
248
|
|
249
249
|
replace_with_match_predicate_method(corrector, recv, arg, op_range)
|
250
250
|
|
251
|
-
corrector.insert_after(arg
|
252
|
-
corrector.insert_before(recv
|
251
|
+
corrector.insert_after(arg, ')') unless op_range.source.end_with?('(')
|
252
|
+
corrector.insert_before(recv, '!') if oper == :!~
|
253
253
|
end
|
254
254
|
|
255
255
|
def replace_with_match_predicate_method(corrector, recv, arg, op_range)
|
@@ -264,14 +264,14 @@ module RuboCop
|
|
264
264
|
end
|
265
265
|
|
266
266
|
def swap_receiver_and_arg(corrector, recv, arg)
|
267
|
-
corrector.replace(recv
|
268
|
-
corrector.replace(arg
|
267
|
+
corrector.replace(recv, arg.source)
|
268
|
+
corrector.replace(arg, recv.source)
|
269
269
|
end
|
270
270
|
|
271
271
|
def correction_range(recv, arg)
|
272
272
|
buffer = processed_source.buffer
|
273
|
-
op_begin_pos = recv.
|
274
|
-
op_end_pos = arg.
|
273
|
+
op_begin_pos = recv.source_range.end_pos
|
274
|
+
op_end_pos = arg.source_range.begin_pos
|
275
275
|
Parser::Source::Range.new(buffer, op_begin_pos, op_end_pos)
|
276
276
|
end
|
277
277
|
end
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
private
|
44
44
|
|
45
45
|
def correction_range(receiver, node)
|
46
|
-
range_between(receiver.loc.selector.begin_pos, node.
|
46
|
+
range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos)
|
47
47
|
end
|
48
48
|
|
49
49
|
def build_message(node)
|
@@ -27,28 +27,33 @@ module RuboCop
|
|
27
27
|
|
28
28
|
MSG = 'Use `%<symbol_arg>s` instead of `%<string_arg>s`.'
|
29
29
|
|
30
|
+
COMMAND_METHODS = %i[
|
31
|
+
alias_method attr_accessor attr_reader attr_writer autoload autoload? private private_constant
|
32
|
+
protected public public_constant module_function
|
33
|
+
].freeze
|
34
|
+
|
30
35
|
# NOTE: `attr` method is not included in this list as it can cause false positives in Nokogiri API.
|
31
36
|
# And `attr` may not be used because `Style/Attr` registers an offense.
|
32
37
|
# https://github.com/rubocop/rubocop-performance/issues/278
|
33
|
-
RESTRICT_ON_SEND = %i[
|
34
|
-
alias_method attr_accessor attr_reader attr_writer autoload autoload?
|
38
|
+
RESTRICT_ON_SEND = (%i[
|
35
39
|
class_variable_defined? const_defined? const_get const_set const_source_location
|
36
40
|
define_method instance_method method_defined? private_class_method? private_method_defined?
|
37
41
|
protected_method_defined? public_class_method public_instance_method public_method_defined?
|
38
42
|
remove_class_variable remove_method undef_method class_variable_get class_variable_set
|
39
|
-
deprecate_constant
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
__send__
|
44
|
-
].freeze
|
43
|
+
deprecate_constant remove_const ruby2_keywords define_singleton_method instance_variable_defined?
|
44
|
+
instance_variable_get instance_variable_set method public_method public_send remove_instance_variable
|
45
|
+
respond_to? send singleton_method __send__
|
46
|
+
] + COMMAND_METHODS).freeze
|
45
47
|
|
46
48
|
def on_send(node)
|
49
|
+
return if COMMAND_METHODS.include?(node.method_name) && node.receiver
|
47
50
|
return unless (first_argument = node.first_argument)
|
48
51
|
return unless first_argument.str_type?
|
49
|
-
return if first_argument.value.include?(' ') || first_argument.value.include?('::')
|
50
52
|
|
51
|
-
|
53
|
+
first_argument_value = first_argument.value
|
54
|
+
return if first_argument_value.include?(' ') || first_argument_value.include?('::')
|
55
|
+
|
56
|
+
replacement = first_argument_value.to_sym.inspect
|
52
57
|
|
53
58
|
message = format(MSG, symbol_arg: replacement, string_arg: first_argument.source)
|
54
59
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
|
44
44
|
new_source = "#{'!' if negation}#{receiver.source}.include?(#{to_string_literal(regex_str)})"
|
45
45
|
|
46
|
-
corrector.replace(node
|
46
|
+
corrector.replace(node, new_source)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
alias on_match_with_lvasgn on_send
|
@@ -70,7 +70,7 @@ module RuboCop
|
|
70
70
|
replacement_method = replacement_method(node, first_source, second_source)
|
71
71
|
|
72
72
|
corrector.replace(node.loc.selector, replacement_method)
|
73
|
-
corrector.replace(first_param
|
73
|
+
corrector.replace(first_param, to_string_literal(first_source)) unless first_param.str_type?
|
74
74
|
|
75
75
|
remove_second_param(corrector, node, first_param) if second_source.empty? && first_source.length == 1
|
76
76
|
end
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
52
52
|
add_offense(node, message: message(map_or_collect, count)) do |corrector|
|
53
53
|
replacement = "Array.new(#{count.source}#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
|
54
54
|
|
55
|
-
corrector.replace(map_or_collect
|
55
|
+
corrector.replace(map_or_collect, replacement)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
message = format(MSG, double_colon: double_colon)
|
31
31
|
|
32
32
|
add_offense(node, message: message) do |corrector|
|
33
|
-
corrector.replace(node
|
33
|
+
corrector.replace(node, "#{double_colon}URI::DEFAULT_PARSER")
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
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.
|
4
|
+
version: 1.17.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-
|
13
|
+
date: 2023-04-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -123,7 +123,7 @@ metadata:
|
|
123
123
|
homepage_uri: https://docs.rubocop.org/rubocop-performance/
|
124
124
|
changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
|
125
125
|
source_code_uri: https://github.com/rubocop/rubocop-performance/
|
126
|
-
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.
|
126
|
+
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.17/
|
127
127
|
bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
|
128
128
|
rubygems_mfa_required: 'true'
|
129
129
|
post_install_message:
|
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
141
|
- !ruby/object:Gem::Version
|
142
142
|
version: '0'
|
143
143
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
144
|
+
rubygems_version: 3.4.6
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: Automatic performance checking tool for Ruby code.
|