rubocop 1.58.0 → 1.60.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +3 -3
  4. data/config/default.yml +9 -1
  5. data/lib/rubocop/config.rb +0 -2
  6. data/lib/rubocop/config_loader.rb +0 -1
  7. data/lib/rubocop/config_validator.rb +0 -2
  8. data/lib/rubocop/cop/base.rb +6 -0
  9. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  10. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  11. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -1
  12. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  13. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
  14. data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -1
  15. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -0
  16. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
  17. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  18. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
  19. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
  20. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +23 -2
  21. data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
  22. data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
  23. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  24. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  25. data/lib/rubocop/cop/lint/self_assignment.rb +1 -0
  26. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  27. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  28. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
  29. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  30. data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
  31. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  32. data/lib/rubocop/cop/naming/block_forwarding.rb +10 -2
  33. data/lib/rubocop/cop/naming/constant_name.rb +1 -2
  34. data/lib/rubocop/cop/registry.rb +4 -4
  35. data/lib/rubocop/cop/security/open.rb +2 -2
  36. data/lib/rubocop/cop/style/arguments_forwarding.rb +16 -3
  37. data/lib/rubocop/cop/style/array_first_last.rb +1 -1
  38. data/lib/rubocop/cop/style/class_check.rb +1 -0
  39. data/lib/rubocop/cop/style/collection_compact.rb +7 -6
  40. data/lib/rubocop/cop/style/combinable_loops.rb +11 -0
  41. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
  42. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  43. data/lib/rubocop/cop/style/date_time.rb +5 -4
  44. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
  45. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  46. data/lib/rubocop/cop/style/eval_with_location.rb +0 -11
  47. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  48. data/lib/rubocop/cop/style/hash_each_methods.rb +29 -4
  49. data/lib/rubocop/cop/style/hash_except.rb +2 -1
  50. data/lib/rubocop/cop/style/identical_conditional_branches.rb +4 -1
  51. data/lib/rubocop/cop/style/invertible_unless_condition.rb +39 -2
  52. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +3 -2
  53. data/lib/rubocop/cop/style/map_to_hash.rb +9 -4
  54. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +14 -5
  55. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -3
  56. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
  57. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -3
  58. data/lib/rubocop/cop/style/next.rb +1 -1
  59. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  60. data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
  61. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  62. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  63. data/lib/rubocop/cop/style/redundant_argument.rb +1 -0
  64. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  65. data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
  66. data/lib/rubocop/cop/style/redundant_line_continuation.rb +8 -1
  67. data/lib/rubocop/cop/style/redundant_parentheses.rb +15 -7
  68. data/lib/rubocop/cop/style/redundant_self.rb +17 -2
  69. data/lib/rubocop/cop/style/redundant_sort.rb +8 -7
  70. data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
  71. data/lib/rubocop/cop/style/sample.rb +2 -1
  72. data/lib/rubocop/cop/style/select_by_regexp.rb +6 -5
  73. data/lib/rubocop/cop/style/single_argument_dig.rb +5 -2
  74. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  75. data/lib/rubocop/cop/style/string_chars.rb +1 -0
  76. data/lib/rubocop/cop/style/strip.rb +7 -4
  77. data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
  78. data/lib/rubocop/cop/style/unpack_first.rb +11 -14
  79. data/lib/rubocop/cops_documentation_generator.rb +11 -1
  80. data/lib/rubocop/ext/regexp_node.rb +9 -4
  81. data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
  82. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  83. data/lib/rubocop/formatter.rb +1 -1
  84. data/lib/rubocop/lsp/routes.rb +1 -1
  85. data/lib/rubocop/options.rb +0 -8
  86. data/lib/rubocop/rspec/shared_contexts.rb +6 -0
  87. data/lib/rubocop/rspec/support.rb +1 -0
  88. data/lib/rubocop/server/cache.rb +0 -1
  89. data/lib/rubocop/version.rb +1 -1
  90. data/lib/rubocop.rb +1 -0
  91. metadata +8 -7
  92. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b262d82d17790d0993cf662a3ede317cbddfbc38704ae0bbf3108d411fac02d1
4
- data.tar.gz: e9e7fd1969decd87a3b5e4760331f98bb5b12a48b44b91d9f5a71cb5b79edb89
3
+ metadata.gz: 8154c812a06c819d2014cca8c0967d9b9eb01ff0481b33318accfb61781b04ce
4
+ data.tar.gz: d39b524c83cd84df683815b59ac1377fb9dc235dcce337507a56bf611eaa90af
5
5
  SHA512:
6
- metadata.gz: 9e6a605d355ff38cdd29726c5e81aeb884757f3fb431beff0dd64f83b05e015f56ec429eccca953142fd85745f3bdbc33b838f1541f7253d422c30ab38b5041d
7
- data.tar.gz: 2fb1647376fcb6c193fecd74478a3d8913b8efa2ede1bac4c7895d0c42ee94f0f5ec52628e85c3461f930cb9795d18240116f2972d406df136cf4a2fc73f1454
6
+ metadata.gz: '0787d7ca9354c20ef1fbbee33dc551cd2e45e8c3ea4aac96a51f8348b3a03a3ae1cc5262e0e6d00f61591f48ce4ffbf9140d7f1ddd19b9716d0596df3bbaa55c'
7
+ data.tar.gz: de7a48105e8126e9f2c323dc516d4c9862b8c239d881e8285496eb3e79cefdecf487a37e0cb3e0b98fc25264f33c3a2d104fb1e6a4e382d1619f21dace50cc99
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-23 Bozhidar Batsov
1
+ Copyright (c) 2012-24 Bozhidar Batsov
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://raw.githubusercontent.com/rubocop/rubocop/master/logo/rubo-logo-horizontal.png" alt="RuboCop Logo"/>
2
+ <img src="https://raw.githubusercontent.com/rubocop/rubocop/master/logo/rubo-logo-horizontal-white.png" alt="RuboCop Logo"/>
3
3
  </p>
4
4
 
5
5
  ----------
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.58', require: false
56
+ gem 'rubocop', '~> 1.60', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
@@ -248,5 +248,5 @@ RuboCop's changelog is available [here](CHANGELOG.md).
248
248
 
249
249
  ## Copyright
250
250
 
251
- Copyright (c) 2012-2023 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
251
+ Copyright (c) 2012-2024 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
252
252
  further details.
data/config/default.yml CHANGED
@@ -1963,6 +1963,12 @@ Lint/InterpolationCheck:
1963
1963
  VersionAdded: '0.50'
1964
1964
  VersionChanged: '1.40'
1965
1965
 
1966
+ Lint/ItWithoutArgumentsInBlock:
1967
+ Description: 'Checks uses of `it` calls without arguments in block.'
1968
+ Reference: 'https://bugs.ruby-lang.org/issues/18980'
1969
+ Enabled: pending
1970
+ VersionAdded: '1.59'
1971
+
1966
1972
  Lint/LambdaWithoutLiteralBlock:
1967
1973
  Description: 'Checks uses of lambda without a literal block.'
1968
1974
  Enabled: pending
@@ -4706,6 +4712,7 @@ Style/OperatorMethodCall:
4706
4712
 
4707
4713
  Style/OptionHash:
4708
4714
  Description: "Don't use option hashes when you can use keyword arguments."
4715
+ StyleGuide: '#keyword-arguments-vs-option-hashes'
4709
4716
  Enabled: false
4710
4717
  VersionAdded: '0.33'
4711
4718
  VersionChanged: '0.34'
@@ -5231,7 +5238,8 @@ Style/SingleLineMethods:
5231
5238
  AllowIfMethodIsEmpty: true
5232
5239
 
5233
5240
  Style/SlicingWithRange:
5234
- Description: 'Checks array slicing is done with endless ranges when suitable.'
5241
+ Description: 'Checks array slicing is done with redundant, endless, and beginless ranges when suitable.'
5242
+ StyleGuide: '#slicing-with-ranges'
5235
5243
  Enabled: true
5236
5244
  VersionAdded: '0.83'
5237
5245
  Safe: false
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
4
-
5
3
  # FIXME: Moving Rails department code to RuboCop Rails will remove
6
4
  # the following rubocop:disable comment.
7
5
  # rubocop:disable Metrics/ClassLength
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erb'
4
- require 'pathname'
5
4
  require 'yaml'
6
5
  require_relative 'config_finder'
7
6
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
4
-
5
3
  module RuboCop
6
4
  # Handles validation of configuration, for example cop names, parameter
7
5
  # names, and Ruby versions.
@@ -481,6 +481,12 @@ module RuboCop
481
481
  range.end_pos + @current_offset
482
482
  )
483
483
  end
484
+
485
+ # This experimental feature has been under consideration for a while.
486
+ # @api private
487
+ def lsp_mode?
488
+ ARGV.include?('--lsp')
489
+ end
484
490
  end
485
491
  end
486
492
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # The parameter name given is transformed into a method name (eg. `Max`
9
9
  # becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
10
10
  def exclude_limit(parameter_name, method_name: transform(parameter_name))
11
- define_method("#{method_name}=") do |value|
11
+ define_method(:"#{method_name}=") do |value|
12
12
  cfg = config_to_allow_offenses
13
13
  cfg[:exclude_limit] ||= {}
14
14
  current_max = cfg[:exclude_limit][parameter_name]
@@ -83,7 +83,11 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def on_sclass(node)
86
- check_other_alignment(node)
86
+ if node.parent&.assignment?
87
+ check_asgn_alignment(node.parent, node)
88
+ else
89
+ check_other_alignment(node)
90
+ end
87
91
  end
88
92
 
89
93
  def on_module(node)
@@ -5,7 +5,10 @@ module RuboCop
5
5
  module Layout
6
6
  # Checks the indentation of the first element in an array literal
7
7
  # where the opening bracket and the first element are on separate lines.
8
- # The other elements' indentations are handled by the ArrayAlignment cop.
8
+ # The other elements' indentations are handled by `Layout/ArrayAlignment` cop.
9
+ #
10
+ # This cop will respect `Layout/ArrayAlignment` and will not work when
11
+ # `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArrayAlignment`.
9
12
  #
10
13
  # By default, array literals that are arguments in a method call with
11
14
  # parentheses, and where the opening square bracket of the array is on the
@@ -93,6 +96,8 @@ module RuboCop
93
96
  end
94
97
 
95
98
  def on_send(node)
99
+ return if style != :consistent && enforce_first_argument_with_fixed_indentation?
100
+
96
101
  each_argument_node(node, :array) do |array_node, left_parenthesis|
97
102
  check(array_node, left_parenthesis)
98
103
  end
@@ -174,6 +179,16 @@ module RuboCop
174
179
  'where the left bracket is.'
175
180
  end
176
181
  end
182
+
183
+ def enforce_first_argument_with_fixed_indentation?
184
+ return false unless array_alignment_config['Enabled']
185
+
186
+ array_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
187
+ end
188
+
189
+ def array_alignment_config
190
+ config.for_cop('Layout/ArrayAlignment')
191
+ end
177
192
  end
178
193
  end
179
194
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
  #
9
9
  # Note: When ``Layout/LineLength``'s `AllowHeredoc` is false (not default),
10
10
  # this cop does not add any offenses for long here documents to
11
- # avoid `Layout/LineLength`'s offenses.
11
+ # avoid ``Layout/LineLength``'s offenses.
12
12
  #
13
13
  # @example
14
14
  # # bad
@@ -107,7 +107,7 @@ module RuboCop
107
107
  return false unless line.end_with?("\\\n")
108
108
 
109
109
  # Ensure backslash isn't part of a token spanning to the next line.
110
- node.children.none? { |c| c.first_line == line_num && c.multiline? }
110
+ node.children.none? { |c| (c.first_line...c.last_line).cover?(line_num) && c.multiline? }
111
111
  end
112
112
 
113
113
  def autocorrect(corrector, offense_range, insert_pos, spaces)
@@ -62,6 +62,7 @@ module RuboCop
62
62
 
63
63
  register_offense(node)
64
64
  end
65
+ alias on_csend on_send
65
66
 
66
67
  private
67
68
 
@@ -135,7 +136,7 @@ module RuboCop
135
136
  .gsub(/" *\\\n\s*'/, %q(" + ')) # Double quote, backslash, and then single quote
136
137
  .gsub(/' *\\\n\s*"/, %q(' + ")) # Single quote, backslash, and then double quote
137
138
  .gsub(/(["']) *\\\n\s*\1/, '') # Double or single quote, backslash, then same quote
138
- .gsub(/\n\s*(?=\.\w)/, '') # Extra space within method chaining
139
+ .gsub(/\n\s*(?=(&)?\.\w)/, '') # Extra space within method chaining which includes `&.`
139
140
  .gsub(/\s*\\?\n\s*/, ' ') # Any other line break, with or without backslash
140
141
  end
141
142
 
@@ -33,6 +33,7 @@ module RuboCop
33
33
  range = offending_range(node)
34
34
  add_offense(range) { |corrector| corrector.insert_before(range, "\n") } if range
35
35
  end
36
+ alias on_csend on_send
36
37
 
37
38
  private
38
39
 
@@ -6,10 +6,10 @@ module RuboCop
6
6
  # Checks for places where binary operator has identical operands.
7
7
  #
8
8
  # It covers arithmetic operators: `-`, `/`, `%`;
9
- # comparison operators: `==`, `===`, `=~`, `>`, `>=`, `<`, `<=`;
9
+ # comparison operators: `==`, `===`, `=~`, `>`, `>=`, `<`, ``<=``;
10
10
  # bitwise operators: `|`, `^`, `&`;
11
11
  # boolean operators: `&&`, `||`
12
- # and "spaceship" operator - `<=>`.
12
+ # and "spaceship" operator - ``<=>``.
13
13
  #
14
14
  # Simple arithmetic operations are allowed by this cop: `+`, `*`, `**`, `<<` and `>>`.
15
15
  # Although these can be rewritten in a different way, it should not be necessary to
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # Checks for overwriting an exception with an exception result by use `rescue =>`.
6
+ # Checks for overwriting an exception with an exception result by use ``rescue =>``.
7
7
  #
8
8
  # You intended to write as `rescue StandardError`.
9
9
  # However, you have written `rescue => StandardError`.
@@ -35,12 +35,13 @@ module RuboCop
35
35
 
36
36
  # @!method id_as_hash_key?(node)
37
37
  def_node_matcher :id_as_hash_key?, <<~PATTERN
38
- (send _ {:key? :has_key? :fetch :[] :[]=} (send _ :object_id) ...)
38
+ (call _ {:key? :has_key? :fetch :[] :[]=} (send _ :object_id) ...)
39
39
  PATTERN
40
40
 
41
41
  def on_send(node)
42
42
  add_offense(node) if id_as_hash_key?(node)
43
43
  end
44
+ alias on_csend on_send
44
45
  end
45
46
  end
46
47
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Emulates the following Ruby warning in Ruby 3.3.
7
+ #
8
+ # [source,ruby]
9
+ # ----
10
+ # $ ruby -e '0.times { it }'
11
+ # -e:1: warning: `it` calls without arguments will refer to the first block param in Ruby 3.4;
12
+ # use it() or self.it
13
+ # ----
14
+ #
15
+ # `it` calls without arguments will refer to the first block param in Ruby 3.4.
16
+ # So use `it()` or `self.it` to ensure compatibility.
17
+ #
18
+ # @example
19
+ #
20
+ # # bad
21
+ # do_something { it }
22
+ #
23
+ # # good
24
+ # do_something { it() }
25
+ # do_something { self.it }
26
+ #
27
+ class ItWithoutArgumentsInBlock < Base
28
+ include NodePattern::Macros
29
+
30
+ MSG = '`it` calls without arguments will refer to the first block param in Ruby 3.4; ' \
31
+ 'use `it()` or `self.it`.'
32
+
33
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
34
+ return unless (body = node.body)
35
+ return unless node.arguments.empty_and_without_delimiters?
36
+
37
+ if body.send_type? && deprecated_it_method?(body)
38
+ add_offense(body)
39
+ else
40
+ body.each_descendant(:send).each do |send_node|
41
+ next unless deprecated_it_method?(send_node)
42
+
43
+ add_offense(send_node)
44
+ end
45
+ end
46
+ end
47
+
48
+ def deprecated_it_method?(node)
49
+ return false unless node.method?(:it)
50
+
51
+ !node.receiver && node.arguments.empty? && !node.parenthesized? && !node.block_literal?
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -41,9 +41,9 @@ module RuboCop
41
41
  next unless asgn_node.loc.operator
42
42
 
43
43
  rhs = asgn_node.to_a.last
44
- next unless rhs.respond_to?(:literal?) && rhs.literal?
44
+ next if !all_literals?(rhs) || parallel_assignment_with_splat_operator?(rhs)
45
45
 
46
- range = asgn_node.loc.operator.join(rhs.source_range.end)
46
+ range = offense_range(asgn_node, rhs)
47
47
 
48
48
  add_offense(range, message: format(MSG, literal: rhs.source))
49
49
  end
@@ -58,6 +58,27 @@ module RuboCop
58
58
 
59
59
  node.each_child_node { |child| traverse_node(child, &block) }
60
60
  end
61
+
62
+ def all_literals?(node)
63
+ case node.type
64
+ when :dstr, :xstr
65
+ false
66
+ when :array
67
+ node.values.all? { |value| all_literals?(value) }
68
+ when :hash
69
+ (node.values + node.keys).all? { |item| all_literals?(item) }
70
+ else
71
+ node.respond_to?(:literal?) && node.literal?
72
+ end
73
+ end
74
+
75
+ def parallel_assignment_with_splat_operator?(node)
76
+ node.array_type? && node.values.first&.splat_type?
77
+ end
78
+
79
+ def offense_range(asgn_node, rhs)
80
+ asgn_node.loc.operator.join(rhs.source_range.end)
81
+ end
61
82
  end
62
83
  end
63
84
  end
@@ -34,35 +34,20 @@ module RuboCop
34
34
  add_offense(void_next) if void_next
35
35
  end
36
36
  end
37
-
38
- def on_numblock(node)
39
- on_numblock_body_of_reduce(node) do |body|
40
- void_next = body.each_node(:next).find do |n|
41
- n.children.empty? && parent_numblock_node(n) == node
42
- end
43
-
44
- add_offense(void_next) if void_next
45
- end
46
- end
37
+ alias on_numblock on_block
47
38
 
48
39
  private
49
40
 
50
41
  # @!method on_block_body_of_reduce(node)
51
42
  def_node_matcher :on_block_body_of_reduce, <<~PATTERN
52
- (block (send _recv {:reduce :inject} !sym) _blockargs $(begin ...))
53
- PATTERN
54
-
55
- # @!method on_numblock_body_of_reduce(node)
56
- def_node_matcher :on_numblock_body_of_reduce, <<~PATTERN
57
- (numblock (send _recv {:reduce :inject} !sym) _argscount $(begin ...))
43
+ {
44
+ (block (call _recv {:reduce :inject} !sym) _blockargs $(begin ...))
45
+ (numblock (call _recv {:reduce :inject} !sym) _argscount $(begin ...))
46
+ }
58
47
  PATTERN
59
48
 
60
49
  def parent_block_node(node)
61
- node.each_ancestor(:block).first
62
- end
63
-
64
- def parent_numblock_node(node)
65
- node.each_ancestor(:numblock).first
50
+ node.each_ancestor(:block, :numblock).first
66
51
  end
67
52
  end
68
53
  end
@@ -9,7 +9,7 @@ module RuboCop
9
9
  #
10
10
  # Conversion with `Integer`, `Float`, etc. will raise an `ArgumentError`
11
11
  # if given input that is not numeric (eg. an empty string), whereas
12
- # `to_i`, etc. will try to convert regardless of input (`''.to_i => 0`).
12
+ # `to_i`, etc. will try to convert regardless of input (``''.to_i => 0``).
13
13
  # As such, this cop is disabled by default because it's not necessarily
14
14
  # always correct to raise if a value is not numeric.
15
15
  #
@@ -91,19 +91,24 @@ module RuboCop
91
91
 
92
92
  # @!method to_method(node)
93
93
  def_node_matcher :to_method, <<~PATTERN
94
- (send $_ ${#{METHODS}})
94
+ (call $_ ${#{METHODS}})
95
95
  PATTERN
96
96
 
97
97
  # @!method to_method_symbol(node)
98
98
  def_node_matcher :to_method_symbol, <<~PATTERN
99
- {(send _ $_ ${(sym ${#{METHODS}})} ...)
100
- (send _ $_ ${(block_pass (sym ${#{METHODS}}))} ...)}
99
+ (call _ $_ ${
100
+ {
101
+ (sym ${#{METHODS}})
102
+ (block_pass (sym ${#{METHODS}}))
103
+ }
104
+ } ...)
101
105
  PATTERN
102
106
 
103
107
  def on_send(node)
104
108
  handle_conversion_method(node)
105
109
  handle_as_symbol(node)
106
110
  end
111
+ alias on_csend on_send
107
112
 
108
113
  private
109
114
 
@@ -56,10 +56,10 @@ module RuboCop
56
56
  def_node_matcher :redundant_with_index?, <<~PATTERN
57
57
  {
58
58
  (block
59
- $(send _ {:each_with_index :with_index} ...)
59
+ $(call _ {:each_with_index :with_index} ...)
60
60
  (args (arg _)) ...)
61
61
  (numblock
62
- $(send _ {:each_with_index :with_index} ...) 1 ...)
62
+ $(call _ {:each_with_index :with_index} ...) 1 ...)
63
63
  }
64
64
  PATTERN
65
65
 
@@ -56,9 +56,9 @@ module RuboCop
56
56
  def_node_matcher :redundant_with_object?, <<~PATTERN
57
57
  {
58
58
  (block
59
- $(send _ {:each_with_object :with_object} _) (args (arg _)) ...)
59
+ $(call _ {:each_with_object :with_object} _) (args (arg _)) ...)
60
60
  (numblock
61
- $(send _ {:each_with_object :with_object} _) 1 ...)
61
+ $(call _ {:each_with_object :with_object} _) 1 ...)
62
62
  }
63
63
  PATTERN
64
64
 
@@ -102,6 +102,7 @@ module RuboCop
102
102
 
103
103
  def handle_attribute_assignment(node)
104
104
  first_argument = node.first_argument
105
+ return unless first_argument.respond_to?(:arguments) && first_argument.arguments.empty?
105
106
 
106
107
  if first_argument.call_type? &&
107
108
  node.receiver == first_argument.receiver &&
@@ -123,6 +123,7 @@ module RuboCop
123
123
 
124
124
  # Shorthand assignments always use their arguments
125
125
  next false if assignment_node.shorthand_asgn?
126
+ next false unless assignment_node.parent
126
127
 
127
128
  node_within_block_or_conditional =
128
129
  node_within_block_or_conditional?(assignment_node.parent, argument.scope.node)
@@ -17,9 +17,12 @@ module RuboCop
17
17
  private
18
18
 
19
19
  def add_offense_from_diagnostic(diagnostic, ruby_version)
20
- message =
21
- "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
22
- 'configure using `TargetRubyVersion` parameter, under `AllCops`)'
20
+ message = if lsp_mode?
21
+ diagnostic.message
22
+ else
23
+ "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
24
+ 'configure using `TargetRubyVersion` parameter, under `AllCops`)'
25
+ end
23
26
  add_offense(diagnostic.location, message: message, severity: diagnostic.level)
24
27
  end
25
28
 
@@ -69,8 +69,8 @@ module RuboCop
69
69
  # @!method reduce_with_block?(node)
70
70
  def_node_matcher :reduce_with_block?, <<~PATTERN
71
71
  {
72
- (block (send _recv {:reduce :inject} ...) args ...)
73
- (numblock (send _recv {:reduce :inject} ...) ...)
72
+ (block (call _recv {:reduce :inject} ...) args ...)
73
+ (numblock (call _recv {:reduce :inject} ...) ...)
74
74
  }
75
75
  PATTERN
76
76
 
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Lint
6
6
  # Checks for uses of `Integer#times` that will never yield
7
- # (when the integer <= 0) or that will only ever yield once
7
+ # (when the integer ``<= 0``) or that will only ever yield once
8
8
  # (`1.times`).
9
9
  #
10
10
  # @safety
@@ -10,9 +10,9 @@ module RuboCop
10
10
  #
11
11
  # Interpreting ABC size:
12
12
  #
13
- # * <= 17 satisfactory
14
- # * 18..30 unsatisfactory
15
- # * > 30 dangerous
13
+ # * ``<= 17`` satisfactory
14
+ # * `18..30` unsatisfactory
15
+ # * `>` 30 dangerous
16
16
  #
17
17
  # You can have repeated "attributes" calls count as a single "branch".
18
18
  # For this purpose, attributes are any method with no argument; no attempt
@@ -18,6 +18,7 @@ module RuboCop
18
18
  alternative_styles.each do |alternative|
19
19
  return unexpected_style_detected(alternative) if valid_name?(node, name, alternative)
20
20
  end
21
+ unrecognized_style_detected
21
22
  end
22
23
 
23
24
  def valid_name?(node, name, given_style = style)
@@ -51,21 +51,29 @@ module RuboCop
51
51
  [Lint::AmbiguousOperator, Style::ArgumentsForwarding]
52
52
  end
53
53
 
54
+ # rubocop:disable Metrics/CyclomaticComplexity
54
55
  def on_def(node)
55
56
  return if node.arguments.empty?
56
57
 
57
58
  last_argument = node.last_argument
58
59
  return if expected_block_forwarding_style?(node, last_argument)
59
60
 
60
- register_offense(last_argument, node)
61
-
61
+ invalid_syntax = false
62
62
  node.each_descendant(:block_pass) do |block_pass_node|
63
63
  next if block_pass_node.children.first&.sym_type? ||
64
64
  last_argument.source != block_pass_node.source
65
65
 
66
+ if block_pass_node.each_ancestor(:block, :numblock).any?
67
+ invalid_syntax = true
68
+ next
69
+ end
70
+
66
71
  register_offense(block_pass_node, node)
67
72
  end
73
+
74
+ register_offense(last_argument, node) unless invalid_syntax
68
75
  end
76
+ # rubocop:enable Metrics/CyclomaticComplexity
69
77
  alias on_defs on_def
70
78
 
71
79
  private
@@ -61,8 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def allowed_method_call_on_rhs?(node)
64
- node&.send_type? &&
65
- (node.receiver.nil? || !literal_receiver?(node))
64
+ node&.send_type? && (node.receiver.nil? || !literal_receiver?(node))
66
65
  end
67
66
 
68
67
  # @!method literal_receiver?(node)
@@ -240,12 +240,12 @@ module RuboCop
240
240
  self
241
241
  end
242
242
 
243
- def select(&block)
244
- cops.select(&block)
243
+ def select(...)
244
+ cops.select(...)
245
245
  end
246
246
 
247
- def each(&block)
248
- cops.each(&block)
247
+ def each(...)
248
+ cops.each(...)
249
249
  end
250
250
 
251
251
  # @param [String] cop_name
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # # bad
24
24
  # open(something)
25
25
  # open("| #{something}")
26
+ # open("| foo")
26
27
  # URI.open(something)
27
28
  #
28
29
  # # good
@@ -32,7 +33,6 @@ module RuboCop
32
33
  #
33
34
  # # good (literal strings)
34
35
  # open("foo.text")
35
- # open("| foo")
36
36
  # URI.open("http://example.com")
37
37
  class Open < Base
38
38
  MSG = 'The use of `%<receiver>sopen` is a serious security risk.'
@@ -40,7 +40,7 @@ module RuboCop
40
40
 
41
41
  # @!method open?(node)
42
42
  def_node_matcher :open?, <<~PATTERN
43
- (send ${nil? (const {nil? cbase} :URI)} :open $!str ...)
43
+ (send ${nil? (const {nil? cbase} :URI)} :open $_ ...)
44
44
  PATTERN
45
45
 
46
46
  def on_send(node)