rubocop-performance 1.19.0 → 1.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb +1 -1
- data/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +4 -0
- data/lib/rubocop/cop/performance/range_include.rb +6 -5
- data/lib/rubocop/cop/performance/redundant_match.rb +30 -1
- data/lib/rubocop/cop/performance/unfreeze_string.rb +3 -0
- data/lib/rubocop/performance/version.rb +1 -1
- data/lib/rubocop-performance.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fa48a119a4c26e989f4d73366c0cbac4bbe854453a702e49f0af9348b1cf798
|
4
|
+
data.tar.gz: e63e391f5f6e2ab2500033cd83df591b9d48254e7f295ce69bbb352e5242c67a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98939001b2b61212c3b4bab44c8b9f9ae8bd906eeb873e2a9516b075f896679fbd513c23c874555982a14d390d265fe01451c218860249ff18376d89f1f59709
|
7
|
+
data.tar.gz: ce73767335d2fe19418afa72ca4a8c5c15632960a2d77282b1042c002c65ec1d744af929fc3b612ba5f7c7e7a5cbaa35aab978fcc8a57e7dde02b114c8fc873b
|
@@ -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 $!{str dstr xstr} $%SLICE_METHODS $#endless_range?)
|
43
43
|
PATTERN
|
44
44
|
|
45
45
|
def_node_matcher :endless_range?, <<~PATTERN
|
@@ -12,6 +12,12 @@ module RuboCop
|
|
12
12
|
# @safety
|
13
13
|
# This cop is unsafe because `Range#include?` (or `Range#member?`) and `Range#cover?`
|
14
14
|
# are not equivalent behavior.
|
15
|
+
# Example of a case where `Range#cover?` may not provide the desired result:
|
16
|
+
#
|
17
|
+
# [source,ruby]
|
18
|
+
# ----
|
19
|
+
# ('a'..'z').cover?('yellow') # => true
|
20
|
+
# ----
|
15
21
|
#
|
16
22
|
# @example
|
17
23
|
# # bad
|
@@ -20,11 +26,6 @@ module RuboCop
|
|
20
26
|
#
|
21
27
|
# # good
|
22
28
|
# ('a'..'z').cover?('b') # => true
|
23
|
-
#
|
24
|
-
# # Example of a case where `Range#cover?` may not provide
|
25
|
-
# # the desired result:
|
26
|
-
#
|
27
|
-
# ('a'..'z').cover?('yellow') # => true
|
28
29
|
class RangeInclude < Base
|
29
30
|
extend AutoCorrector
|
30
31
|
|
@@ -23,6 +23,8 @@ module RuboCop
|
|
23
23
|
MSG = 'Use `=~` in places where the `MatchData` returned by `#match` will not be used.'
|
24
24
|
RESTRICT_ON_SEND = %i[match].freeze
|
25
25
|
|
26
|
+
HIGHER_PRECEDENCE_OPERATOR_METHODS = %i[| ^ & + - * / % ** > >= < <= << >>].freeze
|
27
|
+
|
26
28
|
# 'match' is a fairly generic name, so we don't flag it unless we see
|
27
29
|
# a string or regexp literal on one side or the other
|
28
30
|
def_node_matcher :match_call?, <<~PATTERN
|
@@ -47,7 +49,7 @@ module RuboCop
|
|
47
49
|
private
|
48
50
|
|
49
51
|
def autocorrect(corrector, node)
|
50
|
-
new_source = "#{node.receiver.source} =~ #{node
|
52
|
+
new_source = "#{node.receiver.source} =~ #{replacement(node)}"
|
51
53
|
|
52
54
|
corrector.replace(node, new_source)
|
53
55
|
end
|
@@ -57,6 +59,33 @@ module RuboCop
|
|
57
59
|
# register an offense in that case
|
58
60
|
node.receiver.regexp_type? || node.first_argument.regexp_type?
|
59
61
|
end
|
62
|
+
|
63
|
+
def replacement(node)
|
64
|
+
arg = node.first_argument
|
65
|
+
|
66
|
+
if requires_parentheses?(arg)
|
67
|
+
"(#{arg.source})"
|
68
|
+
else
|
69
|
+
arg.source
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def requires_parentheses?(arg)
|
74
|
+
return true if arg.if_type? && arg.ternary?
|
75
|
+
return true if arg.and_type? || arg.or_type? || arg.range_type?
|
76
|
+
|
77
|
+
call_like?(arg) && requires_parentheses_for_call_like?(arg)
|
78
|
+
end
|
79
|
+
|
80
|
+
def requires_parentheses_for_call_like?(arg)
|
81
|
+
return false if arg.parenthesized? || !arg.arguments?
|
82
|
+
|
83
|
+
!HIGHER_PRECEDENCE_OPERATOR_METHODS.include?(arg.method_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def call_like?(arg)
|
87
|
+
arg.call_type? || arg.yield_type? || arg.super_type?
|
88
|
+
end
|
60
89
|
end
|
61
90
|
end
|
62
91
|
end
|
data/lib/rubocop-performance.rb
CHANGED
@@ -9,3 +9,11 @@ require_relative 'rubocop/performance/inject'
|
|
9
9
|
RuboCop::Performance::Inject.defaults!
|
10
10
|
|
11
11
|
require_relative 'rubocop/cop/performance_cops'
|
12
|
+
|
13
|
+
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
|
14
|
+
Module.new do
|
15
|
+
def autocorrect_incompatible_with
|
16
|
+
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
)
|
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.19.
|
4
|
+
version: 1.19.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: 2023-
|
13
|
+
date: 2023-09-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|