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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 116e2f10caf814178e8afe49e7bac931cbb232dfd1ce8bb478057b7e0a0ce056
4
- data.tar.gz: 0c9c7d2b6180607cc6988eca34ba7364a8295565ab07ddbeaa0c81027f5d3000
3
+ metadata.gz: ab8b6af1dd52db3d8f00efc6428d397474a9c06b9d950b2aa7afa29b2d7feb8e
4
+ data.tar.gz: 80102c530b212f682d6e48d1eff740446821a37757b2a94726669f41fd6a6978
5
5
  SHA512:
6
- metadata.gz: fd9c4b591e4193d5ef640a586c71b7cb0b27dbde63022128119297ebfc9e09741f5a26ee8ef2c1cea876c4f919f48b8fbd364285149c45b89818c6e0ecf1a3c6
7
- data.tar.gz: 8984a58f416a279e7a6f74f4917600a9ba97a90fca81160b0899a66123612736576c870668e8b5e52bb5de0aae5c34ea5874105fa58f3aef63987d5cea319198
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 a blank string, it will be incompatible.
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 `String#include?` instead of a regex match with literal-only pattern.'
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
- add_offense(node) do |corrector|
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
@@ -70,6 +70,9 @@ module RuboCop
70
70
  class Sum < Base
71
71
  include RangeHelp
72
72
  extend AutoCorrector
73
+ extend TargetRubyVersion
74
+
75
+ minimum_target_ruby_version 2.4
73
76
 
74
77
  MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
75
78
  MSG_IF_NO_INIT_VALUE =
@@ -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.15.1'
7
+ STRING = '1.15.2'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
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.1
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-11-16 00:00:00.000000000 Z
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.3.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.