rubocop-performance 1.7.1 → 1.8.0

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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +18 -7
  3. data/lib/rubocop/cop/performance/ancestors_include.rb +14 -13
  4. data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +7 -12
  5. data/lib/rubocop/cop/performance/bind_call.rb +8 -18
  6. data/lib/rubocop/cop/performance/caller.rb +3 -2
  7. data/lib/rubocop/cop/performance/case_when_splat.rb +18 -11
  8. data/lib/rubocop/cop/performance/casecmp.rb +12 -20
  9. data/lib/rubocop/cop/performance/chain_array_allocation.rb +4 -10
  10. data/lib/rubocop/cop/performance/collection_literal_in_loop.rb +140 -0
  11. data/lib/rubocop/cop/performance/compare_with_block.rb +10 -21
  12. data/lib/rubocop/cop/performance/count.rb +13 -16
  13. data/lib/rubocop/cop/performance/delete_prefix.rb +13 -22
  14. data/lib/rubocop/cop/performance/delete_suffix.rb +13 -22
  15. data/lib/rubocop/cop/performance/detect.rb +29 -26
  16. data/lib/rubocop/cop/performance/double_start_end_with.rb +16 -24
  17. data/lib/rubocop/cop/performance/end_with.rb +8 -13
  18. data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
  19. data/lib/rubocop/cop/performance/flat_map.rb +20 -22
  20. data/lib/rubocop/cop/performance/inefficient_hash_search.rb +13 -14
  21. data/lib/rubocop/cop/performance/io_readlines.rb +25 -36
  22. data/lib/rubocop/cop/performance/open_struct.rb +2 -2
  23. data/lib/rubocop/cop/performance/range_include.rb +7 -6
  24. data/lib/rubocop/cop/performance/redundant_block_call.rb +11 -6
  25. data/lib/rubocop/cop/performance/redundant_match.rb +11 -6
  26. data/lib/rubocop/cop/performance/redundant_merge.rb +18 -17
  27. data/lib/rubocop/cop/performance/redundant_sort_block.rb +6 -16
  28. data/lib/rubocop/cop/performance/redundant_string_chars.rb +8 -12
  29. data/lib/rubocop/cop/performance/regexp_match.rb +20 -20
  30. data/lib/rubocop/cop/performance/reverse_each.rb +9 -5
  31. data/lib/rubocop/cop/performance/reverse_first.rb +4 -10
  32. data/lib/rubocop/cop/performance/size.rb +6 -6
  33. data/lib/rubocop/cop/performance/sort_reverse.rb +6 -15
  34. data/lib/rubocop/cop/performance/squeeze.rb +6 -10
  35. data/lib/rubocop/cop/performance/start_with.rb +8 -13
  36. data/lib/rubocop/cop/performance/string_include.rb +9 -13
  37. data/lib/rubocop/cop/performance/string_replacement.rb +23 -27
  38. data/lib/rubocop/cop/performance/sum.rb +129 -0
  39. data/lib/rubocop/cop/performance/times_map.rb +11 -18
  40. data/lib/rubocop/cop/performance/unfreeze_string.rb +1 -1
  41. data/lib/rubocop/cop/performance/uri_default_parser.rb +6 -12
  42. data/lib/rubocop/cop/performance_cops.rb +2 -0
  43. data/lib/rubocop/performance/version.rb +1 -1
  44. metadata +6 -4
@@ -17,7 +17,9 @@ module RuboCop
17
17
  # Array.new(9) do |i|
18
18
  # i.to_s
19
19
  # end
20
- class TimesMap < Cop
20
+ class TimesMap < Base
21
+ extend AutoCorrector
22
+
21
23
  MESSAGE = 'Use `Array.new(%<count>s)` with a block ' \
22
24
  'instead of `.times.%<map_or_collect>s`'
23
25
  MESSAGE_ONLY_IF = 'only if `%<count>s` is always 0 or more'
@@ -30,35 +32,26 @@ module RuboCop
30
32
  check(node)
31
33
  end
32
34
 
33
- def autocorrect(node)
34
- map_or_collect, count = times_map_call(node)
35
-
36
- replacement =
37
- "Array.new(#{count.source}" \
38
- "#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
39
-
40
- lambda do |corrector|
41
- corrector.replace(map_or_collect.loc.expression, replacement)
42
- end
43
- end
44
-
45
35
  private
46
36
 
47
37
  def check(node)
48
38
  times_map_call(node) do |map_or_collect, count|
49
- add_offense(node, message: message(map_or_collect, count))
39
+ add_offense(node, message: message(map_or_collect, count)) do |corrector|
40
+ replacement = "Array.new(#{count.source}" \
41
+ "#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
42
+
43
+ corrector.replace(map_or_collect.loc.expression, replacement)
44
+ end
50
45
  end
51
46
  end
52
47
 
53
48
  def message(map_or_collect, count)
54
49
  template = if count.literal?
55
- MESSAGE + '.'
50
+ "#{MESSAGE}."
56
51
  else
57
52
  "#{MESSAGE} #{MESSAGE_ONLY_IF}."
58
53
  end
59
- format(template,
60
- count: count.source,
61
- map_or_collect: map_or_collect.method_name)
54
+ format(template, count: count.source, map_or_collect: map_or_collect.method_name)
62
55
  end
63
56
 
64
57
  def_node_matcher :times_map_call, <<~PATTERN
@@ -23,7 +23,7 @@ module RuboCop
23
23
  # # good
24
24
  # +'something'
25
25
  # +''
26
- class UnfreezeString < Cop
26
+ class UnfreezeString < Base
27
27
  MSG = 'Use unary plus to get an unfrozen string literal.'
28
28
 
29
29
  def_node_matcher :dup_string?, <<~PATTERN
@@ -13,7 +13,9 @@ module RuboCop
13
13
  # # good
14
14
  # URI::DEFAULT_PARSER
15
15
  #
16
- class UriDefaultParser < Cop
16
+ class UriDefaultParser < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
18
20
  '`%<double_colon>sURI::Parser.new`.'
19
21
 
@@ -28,17 +30,9 @@ module RuboCop
28
30
  double_colon = captured_value ? '::' : ''
29
31
  message = format(MSG, double_colon: double_colon)
30
32
 
31
- add_offense(node, message: message)
32
- end
33
- end
34
-
35
- def autocorrect(node)
36
- lambda do |corrector|
37
- double_colon = uri_parser_new?(node) ? '::' : ''
38
-
39
- corrector.replace(
40
- node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER"
41
- )
33
+ add_offense(node, message: message) do |corrector|
34
+ corrector.replace(node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER")
35
+ end
42
36
  end
43
37
  end
44
38
  end
@@ -9,6 +9,7 @@ require_relative 'performance/bind_call'
9
9
  require_relative 'performance/caller'
10
10
  require_relative 'performance/case_when_splat'
11
11
  require_relative 'performance/casecmp'
12
+ require_relative 'performance/collection_literal_in_loop'
12
13
  require_relative 'performance/compare_with_block'
13
14
  require_relative 'performance/count'
14
15
  require_relative 'performance/delete_prefix'
@@ -36,6 +37,7 @@ require_relative 'performance/squeeze'
36
37
  require_relative 'performance/start_with'
37
38
  require_relative 'performance/string_include'
38
39
  require_relative 'performance/string_replacement'
40
+ require_relative 'performance/sum'
39
41
  require_relative 'performance/times_map'
40
42
  require_relative 'performance/unfreeze_string'
41
43
  require_relative 'performance/uri_default_parser'
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Performance
5
5
  module Version
6
- STRING = '1.7.1'
6
+ STRING = '1.8.0'
7
7
  end
8
8
  end
9
9
  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.7.1
4
+ version: 1.8.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: 2020-07-17 00:00:00.000000000 Z
13
+ date: 2020-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 0.82.0
21
+ version: 0.87.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 0.82.0
28
+ version: 0.87.0
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: simplecov
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +63,7 @@ files:
63
63
  - lib/rubocop/cop/performance/case_when_splat.rb
64
64
  - lib/rubocop/cop/performance/casecmp.rb
65
65
  - lib/rubocop/cop/performance/chain_array_allocation.rb
66
+ - lib/rubocop/cop/performance/collection_literal_in_loop.rb
66
67
  - lib/rubocop/cop/performance/compare_with_block.rb
67
68
  - lib/rubocop/cop/performance/count.rb
68
69
  - lib/rubocop/cop/performance/delete_prefix.rb
@@ -90,6 +91,7 @@ files:
90
91
  - lib/rubocop/cop/performance/start_with.rb
91
92
  - lib/rubocop/cop/performance/string_include.rb
92
93
  - lib/rubocop/cop/performance/string_replacement.rb
94
+ - lib/rubocop/cop/performance/sum.rb
93
95
  - lib/rubocop/cop/performance/times_map.rb
94
96
  - lib/rubocop/cop/performance/unfreeze_string.rb
95
97
  - lib/rubocop/cop/performance/uri_default_parser.rb