rubocop 1.92.2 → 1.92.3

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 (136) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -0
  6. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  7. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  8. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  9. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  10. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  11. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  12. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  13. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  14. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  15. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +14 -6
  16. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  17. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  18. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  19. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  20. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  21. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  22. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  23. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  24. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  26. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  27. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  28. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  29. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  30. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  31. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  32. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  33. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  34. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  35. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  36. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  37. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  38. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  39. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  40. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  41. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  42. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  43. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  44. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  45. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  46. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  47. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  48. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  49. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  50. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  51. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  52. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  53. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  54. data/lib/rubocop/cop/lint/void.rb +20 -9
  55. data/lib/rubocop/cop/message_annotator.rb +6 -8
  56. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  57. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  58. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  59. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  60. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  61. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  62. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  63. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  64. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  65. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  66. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  67. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  68. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  69. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  70. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  71. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  72. data/lib/rubocop/cop/style/and_or.rb +8 -1
  73. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  74. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  75. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  76. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  77. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  78. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  79. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  80. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  81. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  82. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  83. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  84. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  85. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  86. data/lib/rubocop/cop/style/for.rb +59 -4
  87. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  88. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  89. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  90. data/lib/rubocop/cop/style/hash_lookup_method.rb +11 -2
  91. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  92. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  93. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  94. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  95. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  96. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  97. data/lib/rubocop/cop/style/lambda.rb +13 -5
  98. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  99. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  100. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  103. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  104. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  105. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  106. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  107. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  108. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  109. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  110. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  111. data/lib/rubocop/cop/style/redundant_begin.rb +96 -16
  112. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  113. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  114. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  115. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  116. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  117. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  118. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  119. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  120. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  121. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  122. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  123. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  124. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  125. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  126. data/lib/rubocop/cop/style/word_array.rb +1 -3
  127. data/lib/rubocop/cop/util.rb +2 -0
  128. data/lib/rubocop/cop/variable_force.rb +14 -1
  129. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  130. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  131. data/lib/rubocop/result_cache.rb +9 -3
  132. data/lib/rubocop/runner.rb +5 -0
  133. data/lib/rubocop/server/cache.rb +24 -5
  134. data/lib/rubocop/todo_audit.rb +61 -15
  135. data/lib/rubocop/version.rb +1 -1
  136. metadata +4 -3
@@ -126,15 +126,24 @@ module RuboCop
126
126
  end
127
127
  end
128
128
 
129
- # Whatever sits between the last element and the `]` of an array that isn't the
130
- # last argument would end up before the following comma (a newline before it is a
131
- # syntax error), so it's removed along with the `]`.
129
+ # Whatever sits between the last element and the `]` of an array followed by
130
+ # a comma would end up before that comma (a newline before it is a syntax error),
131
+ # so it's removed along with the `]`.
132
132
  def closing_bracket_range(node, argument)
133
- return argument.loc.end if argument.equal?(node.last_argument)
133
+ return argument.loc.end if argument.equal?(node.last_argument) && !trailing_comma?(node)
134
134
 
135
135
  range_between(argument.children.last.source_range.end_pos, argument.loc.end.end_pos)
136
136
  end
137
137
 
138
+ def trailing_comma?(node)
139
+ return false unless node.parenthesized?
140
+
141
+ tokens = processed_source.tokens_within(node)
142
+ closing_bracket_index = tokens.index(processed_source.last_token_of(node.last_argument))
143
+
144
+ tokens[closing_bracket_index + 1].comma?
145
+ end
146
+
138
147
  def replace_selector(corrector, node)
139
148
  if node.parenthesized? || node.single_line?
140
149
  corrector.replace(node.loc.selector, 'push')
@@ -44,6 +44,10 @@ module RuboCop
44
44
 
45
45
  minimum_target_ruby_version 3.2
46
46
 
47
+ def self.autocorrect_incompatible_with
48
+ [Style::MethodCallWithArgsParentheses]
49
+ end
50
+
47
51
  def on_class(node)
48
52
  return unless data_define?(node.parent_class)
49
53
  return if defines_constants?(node.body)
@@ -43,6 +43,7 @@ module RuboCop
43
43
  extend AutoCorrector
44
44
 
45
45
  MSG = "Omit the parentheses in defs when the method doesn't accept any arguments."
46
+ SPACES_BEFORE_ASSIGNMENT = /\A(?:[ \t]|\\\n)+\z/.freeze
46
47
 
47
48
  def on_def(node)
48
49
  return unless !node.arguments? && (arguments_range = node.arguments.source_range)
@@ -64,9 +65,15 @@ module RuboCop
64
65
  # signature from the body, so the parentheses can be removed (`def foo; end`).
65
66
  return false if token_after_argument == ';'
66
67
 
67
- return true if node.single_line? && !node.endless?
68
+ return node.single_line? unless node.endless?
68
69
 
69
- token_after_argument == '='
70
+ !spaces_before_assignment?(node, end_pos)
71
+ end
72
+
73
+ def spaces_before_assignment?(node, end_pos)
74
+ source = range_between(end_pos, node.loc.assignment.begin_pos).source
75
+
76
+ SPACES_BEFORE_ASSIGNMENT.match?(source)
70
77
  end
71
78
  end
72
79
  end
@@ -107,11 +107,10 @@ module RuboCop
107
107
  corrector.replace(node, "class #{class_name} < #{parent_class_name}\n#{indent}end")
108
108
  end
109
109
 
110
- # Preserve any namespace on the assigned constant (e.g. `Foo::Bar`),
110
+ # Preserve any namespace on the assigned constant (e.g. `Foo::Bar` or `::Bar`),
111
111
  # which `node.name` drops.
112
112
  def constant_name(node)
113
- namespace = node.namespace
114
- namespace ? "#{namespace.source}::#{node.name}" : node.name
113
+ node.loc.name.with(begin_pos: node.source_range.begin_pos).source
115
114
  end
116
115
 
117
116
  def autocorrect_class_definition(corrector, node)
@@ -34,6 +34,12 @@ module RuboCop
34
34
 
35
35
  RESTRICT_ON_SEND = %i[new [] Array Hash].freeze
36
36
 
37
+ # A hash literal as the first argument needs the parentheses of the call,
38
+ # which `Style/MethodCallWithArgsParentheses` removes in the same pass.
39
+ def self.autocorrect_incompatible_with
40
+ [Style::MethodCallWithArgsParentheses]
41
+ end
42
+
37
43
  # @!method array_node(node)
38
44
  def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new (array)?)'
39
45
 
@@ -98,6 +104,7 @@ module RuboCop
98
104
  def first_argument_unparenthesized?(node)
99
105
  parent = node.parent
100
106
  return false unless parent && %i[send super zsuper].include?(parent.type)
107
+ return false if parent.operator_method? && !parent.loc.dot
101
108
 
102
109
  node.equal?(parent.first_argument) && !parentheses?(node.parent)
103
110
  end
@@ -81,17 +81,26 @@ module RuboCop
81
81
  end
82
82
 
83
83
  def corrected(node)
84
- scope = node.receiver ? "#{node.receiver.source}." : ''
85
84
  arguments = if node.arguments?
86
85
  args = node.arguments.map(&:source).join(', ')
87
86
 
88
87
  parentheses?(node.arguments) ? "(#{args})" : " #{args}"
89
88
  end
90
- signature = [scope, node.method_name, arguments].join
89
+ signature = [scope(node), node.method_name, arguments].join
91
90
 
92
91
  ["def #{signature}", 'end'].join(joint(node))
93
92
  end
94
93
 
94
+ def scope(node)
95
+ return '' unless (receiver = node.receiver)
96
+
97
+ parenthesized_receiver?(node) ? "(#{receiver.source})." : "#{receiver.source}."
98
+ end
99
+
100
+ def parenthesized_receiver?(node)
101
+ node.loc.keyword.end.join(node.receiver.source_range.begin).source.include?('(')
102
+ end
103
+
95
104
  def correction_exceeds_line_length?(correction)
96
105
  compact_style? && max_line_length && correction.size > max_line_length
97
106
  end
@@ -102,10 +111,13 @@ module RuboCop
102
111
  compact_style? ? '; ' : "\n#{indent}"
103
112
  end
104
113
 
105
- # A method with an argument spanning multiple lines (e.g. a hash default
114
+ # A method with a receiver or an argument spanning multiple lines (e.g. a hash default
106
115
  # value) can't be put on a single line, so it's as compact as it gets.
107
116
  def compact?(node)
108
- node.single_line? || node.arguments.any?(&:multiline?)
117
+ return true if node.single_line?
118
+ return true if node.receiver&.multiline?
119
+
120
+ node.arguments.any?(&:multiline?)
109
121
  end
110
122
 
111
123
  def expanded?(node)
@@ -128,14 +128,12 @@ module RuboCop
128
128
  end
129
129
 
130
130
  def ternary_style_autocorrect(node, outcome, keyword)
131
+ return if outcome.nil? || outcome.multiline?
132
+
131
133
  add_offense(node, message: MSG_TERNARY) do |corrector|
132
134
  next if empty_string_conditional_in_condition?(node)
133
135
 
134
- head = node.source_range.begin.join(outcome.source_range.begin)
135
- tail = outcome.source_range.end.join(node.source_range.end)
136
-
137
- corrector.remove(head)
138
- corrector.replace(tail, " #{keyword} #{node.condition.source}")
136
+ autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
139
137
  end
140
138
  end
141
139
 
@@ -146,6 +144,14 @@ module RuboCop
146
144
  empty_if_outcome?(node) || empty_else_outcome?(node)
147
145
  end
148
146
  end
147
+
148
+ def autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
149
+ head = node.source_range.begin.join(outcome.source_range.begin)
150
+ tail = outcome.source_range.end.join(node.source_range.end)
151
+
152
+ corrector.remove(head)
153
+ corrector.replace(tail, " #{keyword} #{node.condition.source}")
154
+ end
149
155
  end
150
156
  end
151
157
  end
@@ -147,15 +147,20 @@ module RuboCop
147
147
 
148
148
  # `Style/MethodDefParentheses` adds the same parentheses a parameter list needs in
149
149
  # an endless definition, so letting both correct in the same pass would double them.
150
- # `Style/GuardClause` turns a single `if` body into several statements,
151
- # which an endless definition cannot hold.
150
+ # `Style/GuardClause` and `Style/IdenticalConditionalBranches` turn a body that is
151
+ # a single conditional into several statements, which an endless definition cannot hold.
152
152
  # `Style/RaiseArgs` would drop the parentheses of a `raise` that becomes
153
153
  # the body in the same pass, which a method passed as an argument needs.
154
154
  # `Style/IfUnlessModifier` and `Style/WhileUntilModifier` would turn the body into
155
155
  # a modifier form, which then applies to the whole endless definition.
156
+ # `Lint/LiteralAsCondition` and `Lint/UselessTimes` replace the body with
157
+ # the statements inside it, which an endless definition may not hold.
156
158
  def self.autocorrect_incompatible_with
157
159
  [
160
+ Lint::LiteralAsCondition,
161
+ Lint::UselessTimes,
158
162
  Style::GuardClause,
163
+ Style::IdenticalConditionalBranches,
159
164
  Style::IfUnlessModifier,
160
165
  Style::MethodCallWithArgsParentheses,
161
166
  Style::MethodDefParentheses,
@@ -187,7 +192,7 @@ module RuboCop
187
192
  return if node.single_line? || style == :allow_always
188
193
 
189
194
  add_offense(node, message: MSG_MULTI_LINE) do |corrector|
190
- next if endless_parent?(node)
195
+ next if nested_in_multiline_correction?(node)
191
196
 
192
197
  correct_to_multiline(corrector, node)
193
198
  end
@@ -196,10 +201,14 @@ module RuboCop
196
201
  def handle_require_single_line_style(node)
197
202
  if node.endless? && node.multiline?
198
203
  add_offense(node, message: MSG_MULTI_LINE) do |corrector|
204
+ next if nested_in_multiline_correction?(node)
205
+
199
206
  correct_to_multiline(corrector, node)
200
207
  end
201
208
  elsif convertible_to_endless?(node) && single_line_when_made_endless?(node)
202
209
  add_offense(node, message: MSG_REQUIRE_SINGLE) do |corrector|
210
+ next if nested_in_multiline_correction?(node)
211
+
203
212
  correct_to_endless(corrector, node)
204
213
  end
205
214
  end
@@ -222,14 +231,29 @@ module RuboCop
222
231
  return unless node.endless?
223
232
 
224
233
  add_offense(node) do |corrector|
225
- next if endless_parent?(node)
234
+ next if nested_in_multiline_correction?(node)
226
235
 
227
236
  correct_to_multiline(corrector, node)
228
237
  end
229
238
  end
230
239
 
231
- def endless_parent?(node)
232
- node.parent&.any_def_type? && node.parent.endless?
240
+ def nested_in_multiline_correction?(node)
241
+ node.each_ancestor(:any_def).any? do |ancestor|
242
+ corrected_to_multiline?(ancestor)
243
+ end
244
+ end
245
+
246
+ def corrected_to_multiline?(node)
247
+ return false if !node.endless? || use_heredoc?(node)
248
+
249
+ case style
250
+ when :disallow
251
+ true
252
+ when :allow_single_line, :require_single_line
253
+ node.multiline?
254
+ else
255
+ false
256
+ end
233
257
  end
234
258
 
235
259
  def use_heredoc?(node)
@@ -274,10 +298,6 @@ module RuboCop
274
298
  RUBY
275
299
  end
276
300
 
277
- def receiver(node)
278
- node.receiver ? "#{node.receiver.source}#{node.loc.operator.source}" : ''
279
- end
280
-
281
301
  def arguments(node, missing = '')
282
302
  return missing unless node.arguments.any?
283
303
  return parenthesized_arguments(node) if unparenthesized_arguments?(node)
@@ -50,11 +50,15 @@ module RuboCop
50
50
  (block $_ (args $...) (yield $...))
51
51
  PATTERN
52
52
 
53
- # `Style/LambdaCall` rewrites the `call` that the block argument is appended to, and
54
- # `Style/TrailingCommaInArguments` changes the trailing comma where it is appended,
55
- # in the same pass.
53
+ # `Style/LambdaCall` rewrites the `call` that the block argument is appended to,
54
+ # `Style/MethodCallWithArgsParentheses` and `Style/NestedParenthesizedCalls` add the same
55
+ # parentheses around a last argument without them, and `Style/TrailingCommaInArguments`
56
+ # changes the trailing comma where it is appended, in the same pass.
56
57
  def self.autocorrect_incompatible_with
57
- [Lint::UnusedMethodArgument, Style::LambdaCall, Style::TrailingCommaInArguments]
58
+ [
59
+ Lint::UnusedMethodArgument, Style::LambdaCall, Style::MethodCallWithArgsParentheses,
60
+ Style::NestedParenthesizedCalls, Style::TrailingCommaInArguments
61
+ ]
58
62
  end
59
63
 
60
64
  def initialize(config = nil, options = nil)
@@ -166,12 +170,23 @@ module RuboCop
166
170
 
167
171
  def insert_argument(node, corrector, block_name)
168
172
  last_arg = node.last_argument
173
+ parenthesize_arguments(last_arg, corrector) if command_call?(last_arg)
169
174
  arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
170
175
  replacement = " &#{block_name}"
171
176
  replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
172
177
  corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
173
178
  end
174
179
 
180
+ def command_call?(node)
181
+ node.call_type? && node.arguments? && !node.parenthesized? &&
182
+ !node.operator_method? && !node.setter_method?
183
+ end
184
+
185
+ def parenthesize_arguments(node, corrector)
186
+ corrector.replace(node.loc.selector.end.join(node.first_argument.source_range.begin), '(')
187
+ corrector.insert_after(node.last_argument, ')')
188
+ end
189
+
175
190
  def correct_call_node(node, corrector, block_name)
176
191
  new_arguments = if node.zsuper_type?
177
192
  args = build_new_arguments_for_zsuper(node) << "&#{block_name}"
@@ -53,13 +53,16 @@ module RuboCop
53
53
  PREFER_EACH = 'Prefer `each` over `for`.'
54
54
  PREFER_FOR = 'Prefer `for` over `each`.'
55
55
  IMPLICIT_PARAMETERS = %i[_1 _2 _3 _4 _5 _6 _7 _8 _9 it].to_set.freeze
56
+ SCOPE_TYPES = %i[any_def class module sclass].freeze
56
57
 
58
+ # `Style/BlockDelimiters` can turn a brace block in the receiver into `do...end`
59
+ # in the same pass, which is a syntax error in the `for` collection.
57
60
  # `Style/RedundantBegin` removes the `begin` of a block body in the same pass,
58
61
  # leaving a `rescue` or `ensure` clause directly in the `for` body.
59
62
  # `Style/ItBlockParameter` replaces block parameter references in the body
60
63
  # with `it` in the same pass, while they become `for` loop variables.
61
64
  def self.autocorrect_incompatible_with
62
- [Style::RedundantBegin, Style::ItBlockParameter]
65
+ [Style::BlockDelimiters, Style::RedundantBegin, Style::ItBlockParameter]
63
66
  end
64
67
 
65
68
  def on_for(node)
@@ -80,10 +83,9 @@ module RuboCop
80
83
 
81
84
  if style == :for
82
85
  return unless node.receiver
83
- return unless convertible_to_for?(node)
84
- return if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
86
+ return unless correctable_to_for?(node)
85
87
 
86
- add_offense(node, message: PREFER_FOR) do |corrector|
88
+ add_offense(offense_range(node), message: PREFER_FOR) do |corrector|
87
89
  EachToForCorrector.new(node).call(corrector)
88
90
  opposite_style_detected
89
91
  end
@@ -126,6 +128,13 @@ module RuboCop
126
128
  node.multiline? && node.method?(:each) && !node.send_node.arguments?
127
129
  end
128
130
 
131
+ def correctable_to_for?(node)
132
+ return false unless convertible_to_for?(node)
133
+ return false if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
134
+
135
+ !leaked_variable_called_later?(node)
136
+ end
137
+
129
138
  def convertible_to_for?(node)
130
139
  return false if node.type?(:numblock, :itblock)
131
140
  return true unless node.arguments?
@@ -143,6 +152,52 @@ module RuboCop
143
152
  def do_end_block_in_collection?(node)
144
153
  node.receiver.each_node(:any_block).any?(&:keywords?)
145
154
  end
155
+
156
+ def leaked_variable_called_later?(node)
157
+ names = leaked_variable_names(node)
158
+ return false if names.empty?
159
+
160
+ calls = innermost_scope(node, :any_block, *SCOPE_TYPES).each_descendant(:send)
161
+ scope = innermost_scope(node, *SCOPE_TYPES)
162
+
163
+ calls.any? { |call| leaked_name_called?(call, node, names, scope) }
164
+ end
165
+
166
+ def leaked_name_called?(call, node, names, scope)
167
+ return false unless names.include?(call.method_name) && variable_like_call?(call)
168
+ return false if call.source_range.begin_pos < node.source_range.end_pos
169
+
170
+ innermost_scope(call, *SCOPE_TYPES).equal?(scope)
171
+ end
172
+
173
+ def leaked_variable_names(node)
174
+ names = node.arguments.each_descendant(:arg, :restarg).filter_map(&:name)
175
+ return names unless node.body
176
+
177
+ assigned_names = node.body.each_node(:lvasgn).filter_map do |lvasgn|
178
+ lvasgn.name if innermost_scope(lvasgn, :any_block, *SCOPE_TYPES).equal?(node)
179
+ end
180
+
181
+ names + assigned_names
182
+ end
183
+
184
+ def innermost_scope(node, *types)
185
+ node.each_ancestor(*types).first || processed_source.ast
186
+ end
187
+
188
+ def variable_like_call?(send_node)
189
+ !send_node.receiver && !send_node.parenthesized?
190
+ end
191
+
192
+ def offense_range(node)
193
+ range = node.source_range
194
+ selector = node.send_node.loc.selector
195
+ return range if same_line?(range, selector)
196
+
197
+ line = selector.source_buffer.line_range(selector.line)
198
+
199
+ range.with(begin_pos: line.begin_pos + line.source.index(/\S/))
200
+ end
146
201
  end
147
202
  end
148
203
  end
@@ -100,14 +100,18 @@ module RuboCop
100
100
  # end
101
101
  # end
102
102
  #
103
- class GuardClause < Base
103
+ class GuardClause < Base # rubocop:disable Metrics/ClassLength
104
104
  extend AutoCorrector
105
105
  include RangeHelp
106
106
  include MinBodyLength
107
107
  include StatementModifier
108
+ include RescueNode
108
109
 
109
110
  MSG = 'Use a guard clause (`%<example>s`) instead of wrapping the ' \
110
111
  'code inside a conditional expression.'
112
+ STATEMENT_PARENT_TYPES = %i[
113
+ begin kwbegin block numblock itblock class module sclass ensure
114
+ ].freeze
111
115
 
112
116
  def self.autocorrect_incompatible_with
113
117
  [Style::MissingElse]
@@ -191,6 +195,7 @@ module RuboCop
191
195
 
192
196
  add_offense(node.loc.keyword, message: format(MSG, example: example)) do |corrector|
193
197
  next if node.else? && guard.nil?
198
+ next if code_after_end_needs_conditional?(node, guard)
194
199
 
195
200
  autocorrect(corrector, node, example, replacement, guard)
196
201
  end
@@ -224,9 +229,7 @@ module RuboCop
224
229
  end
225
230
 
226
231
  def find_heredoc_argument(node)
227
- return unless node
228
-
229
- node = node.children.first while node.begin_type?
232
+ return unless (node = unwrap_begin(node))
230
233
  return node if heredoc?(node)
231
234
  return unless node.call_type?
232
235
 
@@ -238,12 +241,17 @@ module RuboCop
238
241
  find_heredoc_argument(node.receiver)
239
242
  end
240
243
 
244
+ def unwrap_begin(node)
245
+ node = node.children.first while node&.begin_type?
246
+ node
247
+ end
248
+
241
249
  def close_multiline_guard_clause(corrector, range, heredoc, guard)
242
250
  corrector.insert_after(guard && heredoc ? heredoc.loc.heredoc_end : range, "\nend")
243
251
  end
244
252
 
245
253
  def autocorrect_heredoc_argument(corrector, node, heredoc_node, leave_branch, guard)
246
- remove_whole_lines(corrector, node.loc.end)
254
+ remove_end(corrector, node.loc.end)
247
255
  return unless node.else?
248
256
 
249
257
  if leave_branch
@@ -255,6 +263,14 @@ module RuboCop
255
263
  remove_whole_lines(corrector, range_of_branch_to_remove(node, guard))
256
264
  end
257
265
 
266
+ def remove_end(corrector, end_range)
267
+ if range_by_whole_lines(end_range).source.strip == end_range.source
268
+ remove_whole_lines(corrector, end_range)
269
+ else
270
+ corrector.remove(end_range)
271
+ end
272
+ end
273
+
258
274
  def range_of_branch_to_remove(node, guard)
259
275
  branch = case guard
260
276
  when :if then node.if_branch
@@ -283,15 +299,35 @@ module RuboCop
283
299
  end
284
300
 
285
301
  def accepted_form?(node, ending: false)
286
- accepted_if?(node, ending) || node.condition.multiline? ||
287
- node.parent&.assignment? || endless_method_body?(node)
302
+ return true if accepted_if?(node, ending) || node.condition.multiline?
303
+
304
+ !statement?(node)
288
305
  end
289
306
 
290
- # The body of an endless method is a single expression, so there is no
291
- # room for a guard clause followed by the rest of the code.
292
- def endless_method_body?(node)
307
+ # A guard clause is followed by the rest of the code as separate statements,
308
+ # so there is no room for them where a single expression is expected,
309
+ # such as an assignment value, a hash value, or the body of an endless method.
310
+ def statement?(node)
293
311
  parent = node.parent
294
- parent&.any_def_type? && parent.endless?
312
+ return true if parent.nil? || parent.type?(*STATEMENT_PARENT_TYPES)
313
+ return !parent.endless? if parent.any_def_type?
314
+ return !rescue_modifier?(parent) if parent.resbody_type?
315
+ return !rescue_modifier?(parent.resbody_branches.first) if parent.rescue_type?
316
+
317
+ statement_branch?(node, parent)
318
+ end
319
+
320
+ def statement_branch?(node, parent)
321
+ case parent.type
322
+ when :if
323
+ !parent.ternary? && !node.equal?(parent.condition)
324
+ when :when, :in_pattern, :while, :until, :for
325
+ node.equal?(parent.body)
326
+ when :case, :case_match
327
+ node.equal?(parent.else_branch)
328
+ else
329
+ false
330
+ end
295
331
  end
296
332
 
297
333
  def trivial?(node)
@@ -300,6 +336,15 @@ module RuboCop
300
336
  node.branches.one? && !node.if_branch.if_type? && !node.if_branch.begin_type?
301
337
  end
302
338
 
339
+ def code_after_end_needs_conditional?(node, guard)
340
+ guard_branch = guard == :else ? node.else_branch : node.if_branch
341
+ return false unless find_heredoc_argument(guard_branch)
342
+
343
+ end_keyword = node.loc.end
344
+
345
+ !end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:\z|[#;)\]}])/)
346
+ end
347
+
303
348
  def accepted_if?(node, ending)
304
349
  return true if node.modifier_form? || node.ternary? || node.elsif_conditional? ||
305
350
  assigned_lvar_used_in_if_branch?(node)
@@ -100,7 +100,9 @@ module RuboCop
100
100
 
101
101
  def register_offense_for_hash(node, hash_argument)
102
102
  add_offense(node, message: MSG_LITERAL_HASH_ARG) do |corrector|
103
- corrector.replace(node, "{#{hash_argument.source}}")
103
+ replacement = hash_argument.source
104
+ replacement = "{#{replacement}}" unless hash_argument.braces?
105
+ corrector.replace(node, replacement)
104
106
  add_parentheses_to_parent_call(node, corrector)
105
107
  end
106
108
  end
@@ -59,7 +59,7 @@ module RuboCop
59
59
  return unless last_replaceable_node
60
60
  return unless arguments.size > 1
61
61
 
62
- range = last_replaceable_node.selector.join(node.loc.end)
62
+ range = last_replaceable_node.selector.join(node.source_range.end)
63
63
  replacement = replacement(arguments)
64
64
  message = format(MSG, replacement: replacement)
65
65
 
@@ -44,6 +44,10 @@ module RuboCop
44
44
  # # good
45
45
  # hash.fetch(key)
46
46
  #
47
+ # # good - `Array[]` and `Hash[]` are constructors
48
+ # Array[element]
49
+ # Hash[pairs]
50
+ #
47
51
  # @example AllowedReceivers: ['Rails.cache']
48
52
  # # good
49
53
  # Rails.cache.fetch(name, options) { block }
@@ -61,6 +65,9 @@ module RuboCop
61
65
  # @!method env_const?(node)
62
66
  def_node_matcher :env_const?, '(const {nil? cbase} :ENV)'
63
67
 
68
+ # @!method array_or_hash_const?(node)
69
+ def_node_matcher :array_or_hash_const?, '(const {nil? cbase} {:Array :Hash})'
70
+
64
71
  def on_send(node)
65
72
  return if (receiver = node.receiver) && allowed_receiver?(receiver)
66
73
  return if part_of_ignored_node?(node)
@@ -112,8 +119,10 @@ module RuboCop
112
119
  end
113
120
 
114
121
  def offense_for_fetch?(node)
115
- style == :fetch && node.method?(:[]) && node.arguments.one? &&
116
- !compound_assignment_target?(node)
122
+ return false if style != :fetch || !node.method?(:[]) || !node.arguments.one?
123
+ return false if array_or_hash_const?(node.receiver)
124
+
125
+ !compound_assignment_target?(node)
117
126
  end
118
127
 
119
128
  def compound_assignment_target?(node)
@@ -144,9 +144,10 @@ module RuboCop
144
144
 
145
145
  # Omitting the last hash value of a call without parentheses adds the parentheses,
146
146
  # which `Style/MethodCallWithArgsParentheses` and `Style/SuperWithArgsParentheses`
147
- # would add again in the same pass.
147
+ # would add again in the same pass. Likewise, `Style/RedundantReturn` would add again
148
+ # the braces wrapped around a braceless hash returned with `return`.
148
149
  def self.autocorrect_incompatible_with
149
- [Style::MethodCallWithArgsParentheses, Style::SuperWithArgsParentheses]
150
+ [Style::MethodCallWithArgsParentheses, Style::RedundantReturn, Style::SuperWithArgsParentheses]
150
151
  end
151
152
 
152
153
  def on_hash(node)