rubocop-performance 1.15.1 → 1.15.2
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/lib/rubocop/cop/performance/redundant_string_chars.rb +7 -3
- data/lib/rubocop/cop/performance/regexp_match.rb +2 -2
- data/lib/rubocop/cop/performance/string_include.rb +8 -5
- data/lib/rubocop/cop/performance/sum.rb +3 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab8b6af1dd52db3d8f00efc6428d397474a9c06b9d950b2aa7afa29b2d7feb8e
|
4
|
+
data.tar.gz: 80102c530b212f682d6e48d1eff740446821a37757b2a94726669f41fd6a6978
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36f622e84aec25aae177cbfee56b9c530c6843cbc547e960947d21ab46cc8df87774aa717b3daa75a41a823ef7c8836c7b6acd0c0f45415fc4b216a6901ef617
|
7
|
+
data.tar.gz: c46677e2f07fbd77f24690218b377b308299a8558c664cf21e75009a011638da6aaece36e89ba2c2b42b003ba310233d20bcac6fed5d1ae7d1f5d3f48ada47e0
|
@@ -9,6 +9,7 @@ module RuboCop
|
|
9
9
|
# # bad
|
10
10
|
# str.chars[0..2]
|
11
11
|
# str.chars.slice(0..2)
|
12
|
+
# str.chars.last
|
12
13
|
#
|
13
14
|
# # good
|
14
15
|
# str[0..2].chars
|
@@ -20,6 +21,7 @@ module RuboCop
|
|
20
21
|
# # good
|
21
22
|
# str[0]
|
22
23
|
# str[0...2].chars
|
24
|
+
# str[-1]
|
23
25
|
#
|
24
26
|
# # bad
|
25
27
|
# str.chars.take(2)
|
@@ -33,9 +35,8 @@ module RuboCop
|
|
33
35
|
# str.size
|
34
36
|
# str.empty?
|
35
37
|
#
|
36
|
-
# # For example, if the receiver is
|
38
|
+
# # For example, if the receiver is an empty string, it will be incompatible.
|
37
39
|
# # If a negative value is specified for the receiver, `nil` is returned.
|
38
|
-
# str.chars.last # Incompatible with `str[-1]`.
|
39
40
|
# str.chars.last(2) # Incompatible with `str[-2..-1].chars`.
|
40
41
|
# str.chars.drop(2) # Incompatible with `str[2..-1].chars`.
|
41
42
|
#
|
@@ -44,7 +45,7 @@ module RuboCop
|
|
44
45
|
extend AutoCorrector
|
45
46
|
|
46
47
|
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
|
47
|
-
RESTRICT_ON_SEND = %i[[] slice first take length size empty?].freeze
|
48
|
+
RESTRICT_ON_SEND = %i[[] slice first last take length size empty?].freeze
|
48
49
|
|
49
50
|
def_node_matcher :redundant_chars_call?, <<~PATTERN
|
50
51
|
(send $(send _ :chars) $_ $...)
|
@@ -52,6 +53,7 @@ module RuboCop
|
|
52
53
|
|
53
54
|
def on_send(node)
|
54
55
|
return unless (receiver, method, args = redundant_chars_call?(node))
|
56
|
+
return if method == :last && !args.empty?
|
55
57
|
|
56
58
|
range = offense_range(receiver, node)
|
57
59
|
message = build_message(method, args)
|
@@ -86,6 +88,8 @@ module RuboCop
|
|
86
88
|
"[#{build_call_args(args)}].chars"
|
87
89
|
when :[], :first
|
88
90
|
build_good_method_for_brackets_or_first_method(method, args)
|
91
|
+
when :last
|
92
|
+
'[-1]'
|
89
93
|
when :take
|
90
94
|
"[0...#{args.first.source}].chars"
|
91
95
|
else
|
@@ -124,8 +124,8 @@ module RuboCop
|
|
124
124
|
|
125
125
|
def_node_search :last_matches, <<~PATTERN
|
126
126
|
{
|
127
|
-
(send (const nil? :Regexp) :last_match)
|
128
|
-
(send (const nil? :Regexp) :last_match _)
|
127
|
+
(send (const {nil? cbase} :Regexp) :last_match)
|
128
|
+
(send (const {nil? cbase} :Regexp) :last_match _)
|
129
129
|
({back_ref nth_ref} _)
|
130
130
|
(gvar #match_gvar?)
|
131
131
|
}
|
@@ -22,11 +22,11 @@ module RuboCop
|
|
22
22
|
class StringInclude < Base
|
23
23
|
extend AutoCorrector
|
24
24
|
|
25
|
-
MSG = 'Use
|
26
|
-
RESTRICT_ON_SEND = %i[match =~ match?].freeze
|
25
|
+
MSG = 'Use `%<negation>sString#include?` instead of a regex match with literal-only pattern.'
|
26
|
+
RESTRICT_ON_SEND = %i[match =~ !~ match?].freeze
|
27
27
|
|
28
28
|
def_node_matcher :redundant_regex?, <<~PATTERN
|
29
|
-
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal?) (regopt)))
|
29
|
+
{(send $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
|
30
30
|
(send (regexp (str $#literal?) (regopt)) {:match :match?} $str)
|
31
31
|
(match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
|
32
32
|
PATTERN
|
@@ -34,11 +34,14 @@ module RuboCop
|
|
34
34
|
def on_send(node)
|
35
35
|
return unless (receiver, regex_str = redundant_regex?(node))
|
36
36
|
|
37
|
-
|
37
|
+
negation = node.send_type? && node.method?(:!~)
|
38
|
+
message = format(MSG, negation: ('!' if negation))
|
39
|
+
|
40
|
+
add_offense(node, message: message) do |corrector|
|
38
41
|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
|
39
42
|
regex_str = interpret_string_escapes(regex_str)
|
40
43
|
|
41
|
-
new_source = "#{receiver.source}.include?(#{to_string_literal(regex_str)})"
|
44
|
+
new_source = "#{'!' if negation}#{receiver.source}.include?(#{to_string_literal(regex_str)})"
|
42
45
|
|
43
46
|
corrector.replace(node.source_range, new_source)
|
44
47
|
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.15.
|
4
|
+
version: 1.15.2
|
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: 2022-
|
13
|
+
date: 2022-12-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -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.0.dev
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: Automatic performance checking tool for Ruby code.
|