rubocop 1.92.2 → 1.93.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 (191) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/#directive_comment.rb# +224 -0
  5. data/lib/rubocop/cache_config.rb +3 -0
  6. data/lib/rubocop/cli.rb +5 -4
  7. data/lib/rubocop/config.rb +22 -0
  8. data/lib/rubocop/config_store.rb +1 -0
  9. data/lib/rubocop/config_validator.rb +45 -1
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
  13. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  14. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  16. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  17. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  21. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  22. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  23. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  25. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
  27. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  28. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  29. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
  31. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  33. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  34. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  36. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  39. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  41. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  42. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  43. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  44. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
  47. data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
  48. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  49. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  50. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  51. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  52. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  53. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
  54. data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
  55. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  58. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  59. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  60. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  61. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  62. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  63. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  64. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  65. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  66. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  67. data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
  68. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  69. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  70. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  71. data/lib/rubocop/cop/lint/useless_times.rb +52 -12
  72. data/lib/rubocop/cop/lint/void.rb +20 -9
  73. data/lib/rubocop/cop/message_annotator.rb +6 -8
  74. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  75. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
  76. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  77. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  78. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  79. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  80. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  81. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  82. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  83. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  84. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  85. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  86. data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
  87. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  89. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  90. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  91. data/lib/rubocop/cop/style/and_or.rb +8 -1
  92. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  93. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  94. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  95. data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
  96. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  97. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  98. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  99. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  100. data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
  101. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  102. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  103. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  104. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
  105. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  106. data/lib/rubocop/cop/style/empty_method.rb +27 -4
  107. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  108. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  109. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  110. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  111. data/lib/rubocop/cop/style/file_read.rb +5 -1
  112. data/lib/rubocop/cop/style/file_write.rb +89 -20
  113. data/lib/rubocop/cop/style/for.rb +59 -4
  114. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  115. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  116. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  117. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
  118. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  119. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  121. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  122. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  123. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  124. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  125. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  126. data/lib/rubocop/cop/style/lambda.rb +13 -5
  127. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  128. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  129. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  130. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  132. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  134. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  135. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  136. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  137. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  138. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  139. data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
  140. data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
  141. data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
  142. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  143. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  144. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  145. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  146. data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
  148. data/lib/rubocop/cop/style/redundant_each.rb +26 -1
  149. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  151. data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
  152. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  153. data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
  154. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  155. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  156. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  157. data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
  158. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  159. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  160. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  162. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  163. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  164. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  165. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  167. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  168. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  169. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  170. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  171. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  172. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  173. data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
  174. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  175. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  176. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  177. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  178. data/lib/rubocop/cop/style/word_array.rb +1 -3
  179. data/lib/rubocop/cop/util.rb +2 -0
  180. data/lib/rubocop/cop/variable_force.rb +14 -1
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  183. data/lib/rubocop/result_cache.rb +9 -3
  184. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  185. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  186. data/lib/rubocop/runner.rb +21 -7
  187. data/lib/rubocop/server/cache.rb +24 -5
  188. data/lib/rubocop/target_ruby.rb +6 -8
  189. data/lib/rubocop/todo_audit.rb +61 -15
  190. data/lib/rubocop/version.rb +18 -8
  191. metadata +9 -5
@@ -82,6 +82,13 @@ module RuboCop
82
82
  # 1
83
83
  # end
84
84
  #
85
+ # # good - `undef`, `undef_method`, and `remove_method` remove the earlier definition
86
+ # attr_reader :foo
87
+ # undef foo
88
+ # def foo
89
+ # 1
90
+ # end
91
+ #
85
92
  # # bad
86
93
  # class MyClass
87
94
  # extend Forwardable
@@ -205,15 +212,15 @@ module RuboCop
205
212
  end
206
213
 
207
214
  def on_def(node)
208
- # if a method definition is inside an if, it is very likely
215
+ # if a method definition is inside a conditional, it is very likely
209
216
  # that a different definition is used depending on platform, etc.
210
- return if node.each_ancestor.any?(&:if_type?)
217
+ return if inside_condition?(node)
211
218
 
212
219
  found_instance_method(node, node.method_name)
213
220
  end
214
221
 
215
222
  def on_defs(node)
216
- return if node.each_ancestor.any?(&:if_type?)
223
+ return if inside_condition?(node)
217
224
 
218
225
  if node.receiver.const_type?
219
226
  _, const_name = *node.receiver
@@ -236,16 +243,28 @@ module RuboCop
236
243
  track_intentional_redefinition(node, name)
237
244
  return
238
245
  end
239
- return if node.ancestors.any?(&:if_type?)
246
+ return if inside_condition?(node)
240
247
 
241
248
  found_instance_method(node, name)
242
249
  end
243
250
 
251
+ def on_undef(node)
252
+ node.children.each do |name_node|
253
+ name = sym_name(name_node)
254
+ removed_instance_method(node, name) if name
255
+ end
256
+ end
257
+
244
258
  # @!method alias_method?(node)
245
259
  def_node_matcher :alias_method?, <<~PATTERN
246
260
  (send nil? :alias_method (sym $_name) (sym $_original_name))
247
261
  PATTERN
248
262
 
263
+ # @!method removed_method_names(node)
264
+ def_node_matcher :removed_method_names, <<~PATTERN
265
+ (send nil? {:remove_method :undef_method} ({sym str} $_)+)
266
+ PATTERN
267
+
249
268
  # Matches the argument shape of an Active Support `delegate` call
250
269
  # (`delegate :a, :b, to: :target`), regardless of the method name; the
251
270
  # name is checked separately against `DelegatingMethods`.
@@ -302,6 +321,9 @@ module RuboCop
302
321
  PATTERN
303
322
 
304
323
  def on_send(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
324
+ # Everything that is looked for here is called without a receiver.
325
+ return if node.receiver
326
+
305
327
  name, original_name = alias_method?(node)
306
328
 
307
329
  if name && original_name
@@ -326,6 +348,8 @@ module RuboCop
326
348
  return if inside_condition?(node)
327
349
 
328
350
  names.each { |name| found_instance_method(node, name) }
351
+ elsif (names = removed_method_names(node))
352
+ names.each { |name| removed_instance_method(node, name) }
329
353
  elsif (name = active_support_redefinition_marker(node))
330
354
  # `redefine_method` takes a block; scope resolution must start from the
331
355
  # block node, since `parent_module_name` cannot see through a block
@@ -355,7 +379,7 @@ module RuboCop
355
379
  end
356
380
 
357
381
  def inside_condition?(node)
358
- node.ancestors.any?(&:if_type?)
382
+ node.each_ancestor(:if, :case, :case_match).any?
359
383
  end
360
384
 
361
385
  def message_for_dup(node, method_name, key)
@@ -399,20 +423,30 @@ module RuboCop
399
423
  end
400
424
 
401
425
  def hash_value(node, key)
402
- node.pairs.find { |pair| pair.key.value == key }&.value
426
+ node.pairs.find { |pair| pair.key.sym_type? && pair.key.value == key }&.value
403
427
  end
404
428
 
405
429
  def found_instance_method(node, name)
430
+ method_name, scope_id = instance_method_name(node, name)
431
+ found_method(node, method_name, scope_id: scope_id) if method_name
432
+ end
433
+
434
+ # `undef`, `undef_method`, and `remove_method` remove the method defined so far,
435
+ # so a method defined after them with the same name is not a duplicate.
436
+ def removed_instance_method(node, name)
437
+ method_name, scope_id = instance_method_name(node, name)
438
+ @definitions.delete(definition_key(node, method_name, scope_id)) if method_name
439
+ end
440
+
441
+ def instance_method_name(node, name)
406
442
  if (scope = node.parent_module_name)
407
- found_method(node, "#{humanize_scope(scope)}#{name}")
443
+ ["#{humanize_scope(scope)}#{name}"]
408
444
  elsif (anon_block = anonymous_class_block(node))
409
445
  base = qualified_name(anon_block.parent_module_name, nil, 'Object')
410
446
  scope = node.each_ancestor(:sclass).any? ? "#<Class:#{base}>" : base
411
- found_method(
412
- node, "#{humanize_scope(scope)}#{name}", scope_id: anon_block_scope_id(anon_block)
413
- )
447
+ ["#{humanize_scope(scope)}#{name}", anon_block_scope_id(anon_block)]
414
448
  else
415
- found_sclass_method(node, name)
449
+ sclass_method_name(node, name)
416
450
  end
417
451
  end
418
452
 
@@ -463,19 +497,18 @@ module RuboCop
463
497
  receiver unless class_or_module_new_block?(receiver)
464
498
  end
465
499
 
466
- def found_sclass_method(node, name)
500
+ def sclass_method_name(node, name)
467
501
  singleton_ancestor = node.each_ancestor.find(&:sclass_type?)
468
502
  return unless singleton_ancestor
469
503
 
470
504
  singleton_receiver_node = singleton_ancestor.children[0]
471
505
  return unless singleton_receiver_node.send_type?
472
506
 
473
- found_method(node, "#{singleton_receiver_node.method_name}.#{name}")
507
+ ["#{singleton_receiver_node.method_name}.#{name}"]
474
508
  end
475
509
 
476
510
  def found_method(node, method_name, scope_id: nil)
477
- key = method_key(node, method_name)
478
- key = "#{key}@#{scope_id}" if scope_id
511
+ key = definition_key(node, method_name, scope_id)
479
512
  scope = node.each_ancestor(:rescue, :ensure).first&.type
480
513
 
481
514
  if @definitions.key?(key)
@@ -525,6 +558,11 @@ module RuboCop
525
558
  @intentionally_redefined << "#{humanize_scope(scope)}#{name}"
526
559
  end
527
560
 
561
+ def definition_key(node, method_name, scope_id)
562
+ key = method_key(node, method_name)
563
+ scope_id ? "#{key}@#{scope_id}" : key
564
+ end
565
+
528
566
  def method_key(node, method_name)
529
567
  if (ancestor_def = node.each_ancestor(:any_def).first)
530
568
  "#{ancestor_def.method_name}.#{method_name}"
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  # @!method require_call?(node)
35
35
  def_node_matcher :require_call?, <<~PATTERN
36
- (send {nil? (const _ :Kernel)} %REQUIRE_METHODS _)
36
+ (send {nil? (const {nil? cbase} :Kernel)} %REQUIRE_METHODS _)
37
37
  PATTERN
38
38
 
39
39
  def on_new_investigation
@@ -44,12 +44,19 @@ module RuboCop
44
44
 
45
45
  def on_send(node)
46
46
  return unless require_call?(node)
47
+ return unless statement_sequence?(node.parent)
47
48
  return if @required[node.parent].add?("#{node.method_name}#{node.first_argument}")
48
49
 
49
50
  add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
50
- corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
51
+ corrector.remove(range_for_statement_removal(node.source_range))
51
52
  end
52
53
  end
54
+
55
+ private
56
+
57
+ def statement_sequence?(node)
58
+ node&.type?(:begin, :kwbegin)
59
+ end
53
60
  end
54
61
  end
55
62
  end
@@ -32,6 +32,12 @@ module RuboCop
32
32
  MSG = 'Use a hash literal instead of keyword arguments.'
33
33
  RESTRICT_ON_SEND = %i[new].freeze
34
34
 
35
+ # The braced hash literal needs the parentheses of the call,
36
+ # which `Style/MethodCallWithArgsParentheses` removes in the same pass.
37
+ def self.autocorrect_incompatible_with
38
+ [Style::MethodCallWithArgsParentheses]
39
+ end
40
+
35
41
  # @!method hash_new(node)
36
42
  def_node_matcher :hash_new, <<~PATTERN
37
43
  (send (const {nil? (cbase)} :Hash) :new $[hash !braces?])
@@ -46,7 +52,20 @@ module RuboCop
46
52
  end
47
53
 
48
54
  add_offense(first_argument) do |corrector|
55
+ autocorrect(corrector, node, first_argument)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def autocorrect(corrector, node, first_argument)
62
+ if node.parenthesized?
49
63
  corrector.wrap(first_argument, '{', '}')
64
+ else
65
+ gap = node.loc.selector.end.join(first_argument.source_range.begin)
66
+
67
+ corrector.replace(gap, '({')
68
+ corrector.insert_after(first_argument, '})')
50
69
  end
51
70
  end
52
71
  end
@@ -65,7 +65,9 @@ module RuboCop
65
65
 
66
66
  def valid_syntax?(node)
67
67
  processed_source = parse(interpolated_source(node))
68
- processed_source.valid_syntax? && processed_source.ast.dstr_type?
68
+ return false unless processed_source.valid_syntax? && processed_source.ast.dstr_type?
69
+
70
+ !yield_outside_method?(processed_source.ast, node)
69
71
  end
70
72
 
71
73
  def interpolated_source(node)
@@ -80,6 +82,10 @@ module RuboCop
80
82
  def continued_string_part?(node)
81
83
  node.parent&.dstr_type? && node.left_sibling
82
84
  end
85
+
86
+ def yield_outside_method?(interpolated_node, node)
87
+ interpolated_node.each_node(:yield).any? && node.each_ancestor(:any_def).none?
88
+ end
83
89
  end
84
90
  end
85
91
  end
@@ -37,12 +37,25 @@ module RuboCop
37
37
  PATTERN
38
38
 
39
39
  def on_send(node)
40
- if node.parent&.block_type? || !node.first_argument || lambda_with_symbol_proc?(node)
41
- return
42
- end
40
+ argument = node.first_argument
41
+ return if node.parent&.block_type? || !argument || lambda_with_symbol_proc?(node)
43
42
 
44
43
  add_offense(node) do |corrector|
45
- corrector.replace(node, node.first_argument.source.delete_prefix('&'))
44
+ next if unsafe_to_unwrap?(node, argument)
45
+
46
+ corrector.replace(node, argument.source.delete_prefix('&'))
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def unsafe_to_unwrap?(node, argument)
53
+ return false if node.single_line?
54
+
55
+ processed_source.tokens_within(node).any? do |token|
56
+ next if argument.source_range.contains?(token.pos)
57
+
58
+ token.comment? || token.type == :tSTRING_END
46
59
  end
47
60
  end
48
61
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  # while true
36
36
  # break if condition
37
37
  # end
38
- class LiteralAsCondition < Base
38
+ class LiteralAsCondition < Base # rubocop:disable Metrics/ClassLength
39
39
  include RangeHelp
40
40
  extend AutoCorrector
41
41
 
@@ -50,7 +50,7 @@ module RuboCop
50
50
  # the leftmost node can lead to a void value expression syntax error.
51
51
  next if void_value_expression?(node.rhs)
52
52
 
53
- corrector.replace(node, node.rhs.source)
53
+ replace_node(corrector, node, node.rhs.source)
54
54
  end
55
55
  end
56
56
 
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # the leftmost node can lead to a void value expression syntax error.
63
63
  next if void_value_expression?(node.rhs)
64
64
 
65
- corrector.replace(node, node.rhs.source)
65
+ replace_node(corrector, node, node.rhs.source)
66
66
  end
67
67
  end
68
68
 
@@ -79,11 +79,11 @@ module RuboCop
79
79
 
80
80
  if node.condition.truthy_literal?
81
81
  add_offense(node.condition) do |corrector|
82
- corrector.replace(node.condition, 'true')
82
+ replace_node(corrector, node.condition, 'true')
83
83
  end
84
84
  elsif node.condition.falsey_literal?
85
85
  add_offense(node.condition) do |corrector|
86
- corrector.remove(node)
86
+ replace_node(corrector, node, removable?(node) ? '' : 'nil')
87
87
  end
88
88
  end
89
89
  end
@@ -94,13 +94,13 @@ module RuboCop
94
94
 
95
95
  if node.condition.truthy_literal?
96
96
  add_offense(node.condition) do |corrector|
97
- corrector.replace(node, node.source.sub(node.condition.source, 'true'))
97
+ replace_node(corrector, node, node.source.sub(node.condition.source, 'true'))
98
98
  end
99
99
  elsif node.condition.falsey_literal?
100
100
  add_offense(node.condition) do |corrector|
101
101
  next if orphans_loop_control_keyword?(node)
102
102
 
103
- corrector.replace(node, node.body.child_nodes.map(&:source).join("\n"))
103
+ replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
104
104
  end
105
105
  end
106
106
  end
@@ -110,11 +110,11 @@ module RuboCop
110
110
 
111
111
  if node.condition.falsey_literal?
112
112
  add_offense(node.condition) do |corrector|
113
- corrector.replace(node.condition, 'false')
113
+ replace_node(corrector, node.condition, 'false')
114
114
  end
115
115
  elsif node.condition.truthy_literal?
116
116
  add_offense(node.condition) do |corrector|
117
- corrector.remove(node)
117
+ replace_node(corrector, node, removable?(node) ? '' : 'nil')
118
118
  end
119
119
  end
120
120
  end
@@ -125,13 +125,13 @@ module RuboCop
125
125
 
126
126
  if node.condition.falsey_literal?
127
127
  add_offense(node.condition) do |corrector|
128
- corrector.replace(node, node.source.sub(node.condition.source, 'false'))
128
+ replace_node(corrector, node, node.source.sub(node.condition.source, 'false'))
129
129
  end
130
130
  elsif node.condition.truthy_literal?
131
131
  add_offense(node.condition) do |corrector|
132
132
  next if orphans_loop_control_keyword?(node)
133
133
 
134
- corrector.replace(node, node.body.child_nodes.map(&:source).join("\n"))
134
+ replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
135
135
  end
136
136
  end
137
137
  end
@@ -286,19 +286,30 @@ module RuboCop
286
286
  "#{node.else_branch.source.sub('elsif', 'if')}\nend"
287
287
  elsif node.else? || node.ternary?
288
288
  node.else_branch.source
289
- elsif node.value_used?
290
- 'nil'
291
- else
289
+ elsif removable?(node)
292
290
  '' # Equivalent to removing the node
291
+ else
292
+ 'nil'
293
293
  end
294
294
 
295
295
  add_offense(cond) do |corrector|
296
- next if part_of_ignored_node?(node)
296
+ replace_node(corrector, node, new_node)
297
+ end
298
+ end
297
299
 
298
- corrector.replace(node, new_node)
300
+ def removable?(node)
301
+ return false if node.value_used?
302
+ return true unless (parent = node.parent)
303
+ return true if parent.type?(:begin, :kwbegin, :when)
299
304
 
300
- ignore_node(node)
301
- end
305
+ parent.type?(:if, :while, :until) && parent.loc?(:end)
306
+ end
307
+
308
+ def replace_node(corrector, node, replacement)
309
+ return if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
310
+
311
+ corrector.replace(node, replacement)
312
+ ignore_node(node)
302
313
  end
303
314
  end
304
315
  end
@@ -24,6 +24,10 @@ module RuboCop
24
24
  MSG = 'Literal interpolation detected.'
25
25
  COMPOSITE = %i[array hash pair irange erange].freeze
26
26
 
27
+ def self.autocorrect_incompatible_with
28
+ [Style::PercentLiteralDelimiters, Style::RegexpLiteral]
29
+ end
30
+
27
31
  def on_interpolation(begin_node)
28
32
  final_node = begin_node.children.last
29
33
  return unless offending?(final_node)
@@ -71,6 +75,7 @@ module RuboCop
71
75
  # interpolation should not be removed if the expanded value
72
76
  # contains a space character.
73
77
  return if in_array_percent_literal?(begin_node) && /\s|\A\z/.match?(value)
78
+ return if breaks_delimiters?(begin_node, value)
74
79
 
75
80
  value
76
81
  end
@@ -151,9 +156,9 @@ module RuboCop
151
156
  end
152
157
 
153
158
  def autocorrected_value_for_array(node)
154
- return node.source.gsub('"', '\"') unless node.percent_literal?
159
+ return escape_string_content(node.source) unless node.percent_literal?
155
160
 
156
- contents_range(node).source.split.to_s.gsub('"', '\"')
161
+ escape_string_content(node.values.map { |value| value.value.to_s }.to_s)
157
162
  end
158
163
 
159
164
  def autocorrected_value_for_hash(node)
@@ -188,8 +193,15 @@ module RuboCop
188
193
 
189
194
  # Does node print its own source when converted to a string?
190
195
  def prints_as_self?(node)
191
- node.basic_literal? ||
192
- (COMPOSITE.include?(node.type) && node.children.all? { |child| prints_as_self?(child) })
196
+ return true if node.basic_literal?
197
+ return false unless COMPOSITE.include?(node.type)
198
+ return numeric_open_range?(node) if node.children.any?(&:nil?)
199
+
200
+ node.children.all? { |child| prints_as_self?(child) }
201
+ end
202
+
203
+ def numeric_open_range?(node)
204
+ node.children.compact.all? { |child| child.type?(:int, :float) }
193
205
  end
194
206
 
195
207
  def space_literal?(node)
@@ -215,6 +227,40 @@ module RuboCop
215
227
  grandparent&.array_type? && grandparent.percent_literal?
216
228
  end
217
229
 
230
+ def breaks_delimiters?(begin_node, value)
231
+ return false unless (literal = delimited_literal(begin_node))
232
+
233
+ opening = literal.loc.begin.source[-1]
234
+ closing = literal.loc.end.source[0]
235
+ return !balanced?(value, opening, closing) if opening != closing
236
+
237
+ !escaped_delimiter?(literal, closing) && value.include?(closing)
238
+ end
239
+
240
+ def delimited_literal(begin_node)
241
+ parent = begin_node.parent
242
+ literal = in_array_percent_literal?(begin_node) ? parent.parent : parent
243
+
244
+ literal if literal.loc?(:begin) && literal.loc?(:end)
245
+ end
246
+
247
+ def balanced?(value, opening, closing)
248
+ depth = 0
249
+ value.each_char do |char|
250
+ depth += 1 if char == opening
251
+ depth -= 1 if char == closing
252
+ return false if depth.negative?
253
+ end
254
+
255
+ depth.zero?
256
+ end
257
+
258
+ def escaped_delimiter?(literal, delimiter)
259
+ return true if delimiter == '"'
260
+
261
+ delimiter == '/' && literal.regexp_type? && literal.slash_literal?
262
+ end
263
+
218
264
  def escape_trailing_hash_sign(begin_node, value)
219
265
  following = processed_source.buffer.source[begin_node.source_range.end_pos]
220
266
  return value unless %w[{ @ $].include?(following) && unescaped_hash_sign?(value)
@@ -42,6 +42,7 @@ module RuboCop
42
42
  # FileUtils.rm_f(path)
43
43
  #
44
44
  class NonAtomicFileOperation < Base
45
+ include RangeHelp
45
46
  extend AutoCorrector
46
47
 
47
48
  MSG_REMOVE_FILE_EXIST_CHECK = 'Remove unnecessary existence check ' \
@@ -77,6 +78,10 @@ module RuboCop
77
78
  (pair (sym :force) (:false))
78
79
  PATTERN
79
80
 
81
+ def self.autocorrect_incompatible_with
82
+ [Style::MethodCallWithArgsParentheses]
83
+ end
84
+
80
85
  def on_send(node)
81
86
  return unless node.receiver&.const_type?
82
87
  return unless if_node_child?(node)
@@ -106,7 +111,7 @@ module RuboCop
106
111
  range = parent.loc.keyword.begin.join(parent.condition.source_range.end)
107
112
 
108
113
  add_offense(range, message: message_remove_file_exist_check(exist_node)) do |corrector|
109
- autocorrect(corrector, node, range) unless parent.elsif?
114
+ autocorrect(corrector, node, range) if autocorrectable?(node)
110
115
  end
111
116
  end
112
117
 
@@ -120,17 +125,60 @@ module RuboCop
120
125
  format(MSG_REMOVE_FILE_EXIST_CHECK, receiver: receiver, method_name: method_name)
121
126
  end
122
127
 
128
+ def autocorrectable?(node)
129
+ parent = node.parent
130
+ return false if parent.elsif?
131
+ return true if parent.modifier_form?
132
+ return true unless code_after_end?(parent)
133
+ return false if !node.parenthesized? || last_heredoc_end_pos(node)
134
+
135
+ !processed_source.contains_comment?(body_to_end_range(node))
136
+ end
137
+
138
+ def code_after_end?(if_node)
139
+ end_keyword = if_node.loc.end
140
+
141
+ !end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:#|\z)/)
142
+ end
143
+
144
+ def body_to_end_range(node)
145
+ begin_pos = [node.source_range.end_pos, last_heredoc_end_pos(node)].compact.max
146
+
147
+ range_between(begin_pos, node.parent.loc.end.end_pos)
148
+ end
149
+
150
+ def last_heredoc_end_pos(node)
151
+ node.each_descendant(:any_str).select(&:heredoc?).map do |heredoc|
152
+ heredoc.loc.heredoc_end.end_pos
153
+ end.max
154
+ end
155
+
123
156
  def autocorrect(corrector, node, range)
124
- corrector.remove(range)
157
+ if_node = node.parent
158
+
159
+ corrector.remove(condition_range(if_node, range))
125
160
  autocorrect_replace_method(corrector, node)
126
161
 
127
- if node.parent.modifier_form?
128
- corrector.remove(node.source_range.end.join(node.parent.loc.keyword.begin))
162
+ if if_node.modifier_form?
163
+ corrector.remove(node.source_range.end.join(if_node.loc.keyword.begin))
129
164
  else
130
- corrector.remove(node.parent.loc.end)
165
+ corrector.remove(end_keyword_range(node))
131
166
  end
132
167
  end
133
168
 
169
+ def condition_range(if_node, range)
170
+ return range unless (separator = if_node.loc.begin)
171
+
172
+ range.join(separator)
173
+ end
174
+
175
+ def end_keyword_range(node)
176
+ range = body_to_end_range(node)
177
+ return node.parent.loc.end if processed_source.contains_comment?(range)
178
+
179
+ range
180
+ end
181
+
134
182
  def autocorrect_replace_method(corrector, node)
135
183
  return if force_method?(node)
136
184
 
@@ -88,6 +88,10 @@ module RuboCop
88
88
  CONVERSION_METHODS = %i[Integer Float Complex Rational to_i to_f to_c to_r].freeze
89
89
  METHODS = CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
90
90
 
91
+ def self.autocorrect_incompatible_with
92
+ [Style::MethodCallWithArgsParentheses, Style::NestedParenthesizedCalls]
93
+ end
94
+
91
95
  # @!method to_method(node)
92
96
  def_node_matcher :to_method, <<~PATTERN
93
97
  (call $_ ${#{METHODS}})
@@ -122,6 +126,7 @@ module RuboCop
122
126
  )
123
127
  add_offense(node, message: message) do |corrector|
124
128
  next if safe_navigation?(node) || part_of_ignored_node?(node)
129
+ next if conversion_removes_content?(node, receiver)
125
130
 
126
131
  corrector.replace(node, correct_method(node, node.receiver))
127
132
 
@@ -130,6 +135,15 @@ module RuboCop
130
135
  end
131
136
  end
132
137
 
138
+ def conversion_removes_content?(node, receiver)
139
+ return false if receiver.last_line == node.last_line
140
+
141
+ range = receiver.source_range.end.join(node.source_range.end)
142
+ processed_source.tokens_within(range).any? do |token|
143
+ token.comment? || token.type == :tSTRING_END
144
+ end
145
+ end
146
+
133
147
  def handle_as_symbol(node)
134
148
  to_method_symbol(node) do |receiver, sym_node, to_method|
135
149
  next if receiver.nil? || !node.arguments.one? || node.operator_method?