rubocop-performance 1.25.0 → 1.26.1
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 +4 -4
- data/README.md +1 -1
- data/config/default.yml +5 -5
- data/lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb +1 -1
- data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +5 -0
- data/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +2 -2
- data/lib/rubocop/cop/performance/count.rb +4 -2
- data/lib/rubocop/cop/performance/double_start_end_with.rb +36 -20
- data/lib/rubocop/cop/performance/inefficient_hash_search.rb +3 -3
- data/lib/rubocop/cop/performance/map_compact.rb +2 -2
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +1 -0
- data/lib/rubocop/cop/performance/regexp_match.rb +1 -1
- data/lib/rubocop/performance/version.rb +1 -1
- data/lib/rubocop-performance.rb +11 -5
- metadata +5 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c834e1a3af5e4cc42c7edd0dfe6bdb4e4cfc6e665e2235277f46b15cdb9ab720
|
|
4
|
+
data.tar.gz: a1e5d0efde3b42af4f7d6293e5595ca30df2d24d7cfb765d6ec87932a331d2ef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 127936d00884ae6749b2b5511466c09dd696662d02bd34b581582e71e3966f9228f4d8e8ff880db96ff7267023dbfd87a5121888e95c41afea930ee98f6d30c3
|
|
7
|
+
data.tar.gz: b6cc50f7db2771e41c6700238fe175a5273916ab7da2c3ab487648aa801d04914afa3f0564f67205d2eba436dc694d4cb011fee72e878dffc2c202bcf8a1f011
|
data/README.md
CHANGED
|
@@ -84,7 +84,7 @@ You can read a lot more about RuboCop Performance in its [official docs](https:/
|
|
|
84
84
|
|
|
85
85
|
## Compatibility
|
|
86
86
|
|
|
87
|
-
RuboCop Performance
|
|
87
|
+
RuboCop Performance is compatible with RuboCop core.
|
|
88
88
|
|
|
89
89
|
See the [compatibility documentation](https://docs.rubocop.org/rubocop/compatibility.html) for further details.
|
|
90
90
|
|
data/config/default.yml
CHANGED
|
@@ -22,8 +22,9 @@ Performance/ArraySemiInfiniteRangeSlice:
|
|
|
22
22
|
|
|
23
23
|
Performance/BigDecimalWithNumericArgument:
|
|
24
24
|
Description: 'Convert numeric literal to string and pass it to `BigDecimal`.'
|
|
25
|
-
Enabled:
|
|
25
|
+
Enabled: false
|
|
26
26
|
VersionAdded: '1.7'
|
|
27
|
+
VersionChanged: '1.26'
|
|
27
28
|
|
|
28
29
|
Performance/BindCall:
|
|
29
30
|
Description: 'Use `bind_call(obj, args, ...)` instead of `bind(obj).call(args, ...)`.'
|
|
@@ -67,7 +68,6 @@ Performance/ChainArrayAllocation:
|
|
|
67
68
|
Description: >-
|
|
68
69
|
Instead of chaining array methods that allocate new arrays, mutate an
|
|
69
70
|
existing array.
|
|
70
|
-
Reference: 'https://twitter.com/schneems/status/1034123879978029057'
|
|
71
71
|
Enabled: false
|
|
72
72
|
VersionAdded: '0.59'
|
|
73
73
|
|
|
@@ -187,14 +187,14 @@ Performance/InefficientHashSearch:
|
|
|
187
187
|
|
|
188
188
|
Performance/IoReadlines:
|
|
189
189
|
Description: 'Use `IO.each_line` (`IO#each_line`) instead of `IO.readlines` (`IO#readlines`).'
|
|
190
|
-
Reference: 'https://docs.gitlab.com/
|
|
190
|
+
Reference: 'https://docs.gitlab.com/development/performance/#reading-from-files-and-other-data-sources'
|
|
191
191
|
Enabled: false
|
|
192
192
|
VersionAdded: '1.7'
|
|
193
193
|
|
|
194
194
|
Performance/MapCompact:
|
|
195
195
|
Description: 'Use `filter_map` instead of `collection.map(&:do_something).compact`.'
|
|
196
196
|
Enabled: pending
|
|
197
|
-
|
|
197
|
+
Safe: false
|
|
198
198
|
VersionAdded: '1.11'
|
|
199
199
|
|
|
200
200
|
Performance/MapMethodChain:
|
|
@@ -356,7 +356,7 @@ Performance/StringReplacement:
|
|
|
356
356
|
Performance/Sum:
|
|
357
357
|
Description: 'Use `sum` instead of a custom array summation.'
|
|
358
358
|
SafeAutoCorrect: false
|
|
359
|
-
Reference: 'https://
|
|
359
|
+
Reference: 'https://www.bigbinary.com/blog/ruby-2-4-introduces-enumerable-sum'
|
|
360
360
|
Enabled: 'pending'
|
|
361
361
|
VersionAdded: '1.8'
|
|
362
362
|
VersionChanged: '1.13'
|
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
|
39
39
|
RESTRICT_ON_SEND = SLICE_METHODS
|
|
40
40
|
|
|
41
41
|
def_node_matcher :endless_range_slice?, <<~PATTERN
|
|
42
|
-
(call $!
|
|
42
|
+
(call $!any_str $%SLICE_METHODS $#endless_range?)
|
|
43
43
|
PATTERN
|
|
44
44
|
|
|
45
45
|
def_node_matcher :endless_range?, <<~PATTERN
|
|
@@ -9,6 +9,11 @@ module RuboCop
|
|
|
9
9
|
# Also identifies places where an integer string argument to BigDecimal should be converted to
|
|
10
10
|
# an integer. Initializing from Integer is faster than from String for BigDecimal.
|
|
11
11
|
#
|
|
12
|
+
# NOTE: This cop is disabled by default because the performance of initializing with a String
|
|
13
|
+
# and Number differ between versions. Additionally, performance depends on the size of the Number,
|
|
14
|
+
# and if it is an Integer or a Float. Since this is very specific to `bigdecimal` internals,
|
|
15
|
+
# suggestions from this cop are not unlikely to result in code that performs worse than before.
|
|
16
|
+
#
|
|
12
17
|
# @example
|
|
13
18
|
# # bad
|
|
14
19
|
# BigDecimal(1.2, 3, exception: true)
|
|
@@ -36,7 +36,7 @@ module RuboCop
|
|
|
36
36
|
def_node_matcher :reassigns_block_arg?, '`(lvasgn %1 ...)'
|
|
37
37
|
|
|
38
38
|
def on_send(node)
|
|
39
|
-
def_node = node.each_ancestor(:
|
|
39
|
+
def_node = node.each_ancestor(:any_def).first
|
|
40
40
|
return unless def_node
|
|
41
41
|
|
|
42
42
|
block_arg = def_node.arguments.find(&:blockarg_type?)
|
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
def self.autocorrect_incompatible_with
|
|
55
|
-
[Lint::UnusedMethodArgument]
|
|
55
|
+
[Lint::UnusedMethodArgument, Naming::BlockForwarding]
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
end
|
|
@@ -81,13 +81,15 @@ module RuboCop
|
|
|
81
81
|
|
|
82
82
|
range = source_starting_at(node) { |n| n.loc.dot.begin_pos }
|
|
83
83
|
|
|
84
|
-
corrector.remove(range)
|
|
84
|
+
corrector.remove(range_with_surrounding_space(range, side: :left))
|
|
85
85
|
corrector.replace(selector_loc, 'count')
|
|
86
86
|
negate_reject(corrector, node) if selector == :reject
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
def eligible_node?(node)
|
|
90
|
-
|
|
90
|
+
return false if node.parent&.block_type?
|
|
91
|
+
|
|
92
|
+
node.receiver.call_type? || node.receiver.body
|
|
91
93
|
end
|
|
92
94
|
|
|
93
95
|
def source_starting_at(node)
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Performance
|
|
6
|
-
# Checks for
|
|
7
|
-
#
|
|
8
|
-
#
|
|
6
|
+
# Checks for consecutive `#start_with?` or `#end_with?` calls.
|
|
7
|
+
# These methods accept multiple arguments, so in some cases like when
|
|
8
|
+
# they are separated by `||`, they can be combined into a single method call.
|
|
9
9
|
#
|
|
10
10
|
# `IncludeActiveSupportAliases` configuration option is used to check for
|
|
11
11
|
# `starts_with?` and `ends_with?`. These methods are defined by Active Support.
|
|
@@ -14,11 +14,13 @@ module RuboCop
|
|
|
14
14
|
# # bad
|
|
15
15
|
# str.start_with?("a") || str.start_with?(Some::CONST)
|
|
16
16
|
# str.start_with?("a", "b") || str.start_with?("c")
|
|
17
|
+
# !str.start_with?(foo) && !str.start_with?(bar)
|
|
17
18
|
# str.end_with?(var1) || str.end_with?(var2)
|
|
18
19
|
#
|
|
19
20
|
# # good
|
|
20
21
|
# str.start_with?("a", Some::CONST)
|
|
21
22
|
# str.start_with?("a", "b", "c")
|
|
23
|
+
# !str.start_with?(foo, bar)
|
|
22
24
|
# str.end_with?(var1, var2)
|
|
23
25
|
#
|
|
24
26
|
# @example IncludeActiveSupportAliases: false (default)
|
|
@@ -43,20 +45,33 @@ module RuboCop
|
|
|
43
45
|
|
|
44
46
|
MSG = 'Use `%<replacement>s` instead of `%<original_code>s`.'
|
|
45
47
|
|
|
48
|
+
METHODS = %i[start_with? end_with?].to_set
|
|
49
|
+
METHODS_WITH_ACTIVE_SUPPORT = %i[start_with? starts_with? end_with? ends_with?].to_set
|
|
50
|
+
|
|
46
51
|
def on_or(node)
|
|
47
|
-
|
|
52
|
+
two_start_end_with_calls(node, methods_to_check: methods) do |*matched|
|
|
53
|
+
check(node, *matched)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def on_and(node)
|
|
58
|
+
two_start_end_with_calls_negated(node, methods_to_check: methods) do |*matched|
|
|
59
|
+
check(node, *matched)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
48
62
|
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def check(node, receiver, method, first_call_args, second_call_args)
|
|
49
66
|
return unless receiver && second_call_args.all?(&:pure?)
|
|
50
67
|
|
|
51
68
|
combined_args = combine_args(first_call_args, second_call_args)
|
|
52
69
|
|
|
53
|
-
add_offense(node, message: message(node, receiver,
|
|
70
|
+
add_offense(node, message: message(node, receiver, method, combined_args)) do |corrector|
|
|
54
71
|
autocorrect(corrector, first_call_args, second_call_args, combined_args)
|
|
55
72
|
end
|
|
56
73
|
end
|
|
57
74
|
|
|
58
|
-
private
|
|
59
|
-
|
|
60
75
|
def autocorrect(corrector, first_call_args, second_call_args, combined_args)
|
|
61
76
|
first_argument = first_call_args.first.source_range
|
|
62
77
|
last_argument = second_call_args.last.source_range
|
|
@@ -65,17 +80,20 @@ module RuboCop
|
|
|
65
80
|
corrector.replace(range, combined_args)
|
|
66
81
|
end
|
|
67
82
|
|
|
68
|
-
def
|
|
83
|
+
def methods
|
|
69
84
|
if check_for_active_support_aliases?
|
|
70
|
-
|
|
85
|
+
METHODS_WITH_ACTIVE_SUPPORT
|
|
71
86
|
else
|
|
72
|
-
|
|
87
|
+
METHODS
|
|
73
88
|
end
|
|
74
89
|
end
|
|
75
90
|
|
|
76
|
-
def message(node, receiver,
|
|
77
|
-
|
|
78
|
-
|
|
91
|
+
def message(node, receiver, method, combined_args)
|
|
92
|
+
parent = receiver.parent
|
|
93
|
+
grandparent = parent.parent
|
|
94
|
+
dot = parent.send_type? ? '.' : '&.'
|
|
95
|
+
bang = grandparent.send_type? && grandparent.prefix_bang? ? '!' : ''
|
|
96
|
+
replacement = "#{bang}#{receiver.source}#{dot}#{method}(#{combined_args})"
|
|
79
97
|
format(MSG, replacement: replacement, original_code: node.source)
|
|
80
98
|
end
|
|
81
99
|
|
|
@@ -89,16 +107,14 @@ module RuboCop
|
|
|
89
107
|
|
|
90
108
|
def_node_matcher :two_start_end_with_calls, <<~PATTERN
|
|
91
109
|
(or
|
|
92
|
-
(call $_recv [
|
|
110
|
+
(call $_recv [%methods_to_check $_method] $...)
|
|
93
111
|
(call _recv _method $...))
|
|
94
112
|
PATTERN
|
|
95
113
|
|
|
96
|
-
def_node_matcher :
|
|
97
|
-
(
|
|
98
|
-
(call $_recv
|
|
99
|
-
|
|
100
|
-
$...)
|
|
101
|
-
(call _recv _method $...))
|
|
114
|
+
def_node_matcher :two_start_end_with_calls_negated, <<~PATTERN
|
|
115
|
+
(and
|
|
116
|
+
(send (call $_recv [%methods_to_check $_method] $...) :!)
|
|
117
|
+
(send (call _recv _method $...) :!))
|
|
102
118
|
PATTERN
|
|
103
119
|
end
|
|
104
120
|
end
|
|
@@ -74,8 +74,8 @@ module RuboCop
|
|
|
74
74
|
|
|
75
75
|
def correct_method(node)
|
|
76
76
|
case current_method(node)
|
|
77
|
-
when :keys then use_long_method ? 'has_key?' : 'key?'
|
|
78
|
-
when :values then use_long_method ? 'has_value?' : 'value?'
|
|
77
|
+
when :keys then use_long_method? ? 'has_key?' : 'key?'
|
|
78
|
+
when :values then use_long_method? ? 'has_value?' : 'value?'
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
81
|
|
|
@@ -83,7 +83,7 @@ module RuboCop
|
|
|
83
83
|
node.receiver.method_name
|
|
84
84
|
end
|
|
85
85
|
|
|
86
|
-
def use_long_method
|
|
86
|
+
def use_long_method?
|
|
87
87
|
config.for_enabled_cop('Style/PreferredHashMethods')['EnforcedStyle'] == 'long'
|
|
88
88
|
end
|
|
89
89
|
|
|
@@ -8,9 +8,9 @@ module RuboCop
|
|
|
8
8
|
# This cop identifies places where `map { ... }.compact` can be replaced by `filter_map`.
|
|
9
9
|
#
|
|
10
10
|
# @safety
|
|
11
|
-
# This cop
|
|
11
|
+
# This cop is unsafe because `map { ... }.compact` might yield
|
|
12
12
|
# different results than `filter_map`. As illustrated in the example, `filter_map`
|
|
13
|
-
# also filters out
|
|
13
|
+
# also filters out falsey values, while `compact` only gets rid of `nil`.
|
|
14
14
|
#
|
|
15
15
|
# [source,ruby]
|
|
16
16
|
# ----
|
|
@@ -54,6 +54,7 @@ module RuboCop
|
|
|
54
54
|
def on_send(node)
|
|
55
55
|
return unless (receiver, method, args = redundant_chars_call?(node))
|
|
56
56
|
return if method == :last && !args.empty?
|
|
57
|
+
return if args.count == 2
|
|
57
58
|
|
|
58
59
|
range = offense_range(receiver, node)
|
|
59
60
|
message = build_message(method, args)
|
data/lib/rubocop-performance.rb
CHANGED
|
@@ -7,10 +7,16 @@ require_relative 'rubocop/performance/version'
|
|
|
7
7
|
require_relative 'rubocop/performance/plugin'
|
|
8
8
|
require_relative 'rubocop/cop/performance_cops'
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
|
|
14
|
-
end
|
|
10
|
+
autocorrect_incompatible_with_block_given_with_explicit_block = Module.new do
|
|
11
|
+
def autocorrect_incompatible_with
|
|
12
|
+
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
|
|
15
13
|
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
|
|
17
|
+
autocorrect_incompatible_with_block_given_with_explicit_block
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
RuboCop::Cop::Naming::BlockForwarding.singleton_class.prepend(
|
|
21
|
+
autocorrect_incompatible_with_block_given_with_explicit_block
|
|
16
22
|
)
|
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.26.1
|
|
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: 2025-
|
|
13
|
+
date: 2025-10-18 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: lint_roller
|
|
@@ -52,7 +52,7 @@ dependencies:
|
|
|
52
52
|
requirements:
|
|
53
53
|
- - ">="
|
|
54
54
|
- !ruby/object:Gem::Version
|
|
55
|
-
version: 1.
|
|
55
|
+
version: 1.47.1
|
|
56
56
|
- - "<"
|
|
57
57
|
- !ruby/object:Gem::Version
|
|
58
58
|
version: '2.0'
|
|
@@ -62,7 +62,7 @@ dependencies:
|
|
|
62
62
|
requirements:
|
|
63
63
|
- - ">="
|
|
64
64
|
- !ruby/object:Gem::Version
|
|
65
|
-
version: 1.
|
|
65
|
+
version: 1.47.1
|
|
66
66
|
- - "<"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: '2.0'
|
|
@@ -146,7 +146,7 @@ metadata:
|
|
|
146
146
|
homepage_uri: https://docs.rubocop.org/rubocop-performance/
|
|
147
147
|
changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
|
|
148
148
|
source_code_uri: https://github.com/rubocop/rubocop-performance/
|
|
149
|
-
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.
|
|
149
|
+
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.26/
|
|
150
150
|
bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
|
|
151
151
|
rubygems_mfa_required: 'true'
|
|
152
152
|
default_lint_roller_plugin: RuboCop::Performance::Plugin
|