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
@@ -59,7 +59,37 @@ module RuboCop
59
59
  private
60
60
 
61
61
  def offense?(node)
62
- node.ternary? && node.multiline? && node.source != replacement(node)
62
+ return false unless node.ternary? && node.multiline?
63
+ return false if heredoc_body_outside_branch?(node) || outer_heredoc_body_inside?(node)
64
+
65
+ node.source != replacement(node)
66
+ end
67
+
68
+ def heredoc_body_outside_branch?(node)
69
+ single_line = enforce_single_line_ternary_operator?(node)
70
+
71
+ [node.condition, node.if_branch, node.else_branch].any? do |branch|
72
+ branch.each_node(:any_str).any? do |str|
73
+ next false unless str.heredoc?
74
+ next false if single_line && heredoc_ends_after?(str, node)
75
+
76
+ heredoc_ends_after?(str, branch)
77
+ end
78
+ end
79
+ end
80
+
81
+ def outer_heredoc_body_inside?(node)
82
+ ternary_range = node.source_range
83
+
84
+ processed_source.ast.each_node(:any_str).any? do |str|
85
+ next false if !str.heredoc? || ternary_range.contains?(str.source_range)
86
+
87
+ ternary_range.overlaps?(str.loc.heredoc_body)
88
+ end
89
+ end
90
+
91
+ def heredoc_ends_after?(heredoc, node)
92
+ heredoc.loc.heredoc_end.end_pos > node.source_range.end_pos
63
93
  end
64
94
 
65
95
  def autocorrect(corrector, node)
@@ -376,7 +376,7 @@ module RuboCop
376
376
  (send (const {nil? cbase} :ENV) :[] _)
377
377
  (or (send (const {nil? cbase} :ENV) :[] _) _)
378
378
  (send _ {:count :length :size} ...)
379
- (block (send _ {:count :length :size} ...) ...)
379
+ (any_block (send _ {:count :length :size} ...) ...)
380
380
  }
381
381
  PATTERN
382
382
 
@@ -107,12 +107,20 @@ module RuboCop
107
107
 
108
108
  def swap_branches(corrector, node)
109
109
  if node.if_branch.nil?
110
- corrector.remove(range_by_whole_lines(node.loc.else, include_final_newline: true))
110
+ corrector.remove(else_removal_range(node.loc.else))
111
111
  else
112
112
  corrector.swap(if_range(node), else_range(node))
113
113
  end
114
114
  end
115
115
 
116
+ def else_removal_range(keyword)
117
+ line = keyword.source_line
118
+ return range_by_whole_lines(keyword, include_final_newline: true) if line.strip == 'else'
119
+
120
+ side = keyword.last_column == line.rstrip.length ? :both : :right
121
+ range_with_surrounding_space(keyword, side: side, newlines: false)
122
+ end
123
+
116
124
  # Collect the entire if branch, including whitespace and comments
117
125
  def if_range(node)
118
126
  if node.ternary?
@@ -28,10 +28,6 @@ module RuboCop
28
28
 
29
29
  MSG = 'Add parentheses to nested method call `%<source>s`.'
30
30
 
31
- def self.autocorrect_incompatible_with
32
- [Style::MethodCallWithArgsParentheses]
33
- end
34
-
35
31
  def on_send(node)
36
32
  return unless node.parenthesized?
37
33
 
@@ -48,6 +48,8 @@ module RuboCop
48
48
 
49
49
  style_check?(node) do
50
50
  add_offense(node.loc.selector) do |corrector|
51
+ next if heredoc_after_receiver?(node)
52
+
51
53
  if prefer_comparison?
52
54
  autocorrect_to_comparison(corrector, node)
53
55
  else
@@ -65,6 +67,14 @@ module RuboCop
65
67
  processed_source.contains_comment?(node.receiver.source_range.end.join(node.loc.dot))
66
68
  end
67
69
 
70
+ def heredoc_after_receiver?(node)
71
+ receiver = node.receiver
72
+ return false if receiver.last_line == node.last_line
73
+
74
+ range = receiver.source_range.end.join(node.source_range.end)
75
+ processed_source.tokens_within(range).any? { |token| token.type == :tSTRING_END }
76
+ end
77
+
68
78
  def autocorrect_to_comparison(corrector, node)
69
79
  range = node.receiver.source_range.end.join(node.loc.selector.end)
70
80
  corrector.replace(range, ' == nil')
@@ -6,10 +6,8 @@ module RuboCop
6
6
  # Checks for lambdas and procs that always return nil,
7
7
  # which can be replaced with an empty lambda or proc instead.
8
8
  #
9
- # NOTE: A `proc` that returns nil via an explicit `return` is allowed,
10
- # because in a `proc` `return` exits the enclosing method, so removing it
11
- # would change behavior. A lambda is still reported, since there `return`
12
- # only exits the lambda itself.
9
+ # In a non-lambda proc, `return` and `break` are non-local exits.
10
+ # They cannot be removed even when their argument is `nil`.
13
11
  #
14
12
  # @example
15
13
  # # bad
@@ -21,10 +19,6 @@ module RuboCop
21
19
  #
22
20
  # proc { nil }
23
21
  #
24
- # Proc.new do
25
- # break nil
26
- # end
27
- #
28
22
  # # good
29
23
  # -> {}
30
24
  #
@@ -36,6 +30,7 @@ module RuboCop
36
30
  # proc {}
37
31
  #
38
32
  # Proc.new { nil if x }
33
+ # proc { break nil }
39
34
  #
40
35
  class NilLambda < Base
41
36
  extend AutoCorrector
@@ -51,9 +46,7 @@ module RuboCop
51
46
  def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler -- a lambda whose body is `nil` uses no parameters
52
47
  return unless node.lambda_or_proc?
53
48
  return unless nil_return?(node.body)
54
- # `return` inside a non-lambda proc returns from the enclosing method,
55
- # so dropping it changes behavior; only a lambda can omit it safely.
56
- return if node.body.return_type? && !node.lambda?
49
+ return if node.body.type?(:return, :break) && !node.lambda?
57
50
 
58
51
  message = format(MSG, type: node.lambda? ? 'lambda' : 'proc')
59
52
  add_offense(node, message: message) do |corrector|
@@ -64,13 +57,25 @@ module RuboCop
64
57
  private
65
58
 
66
59
  def autocorrect(corrector, node)
67
- range = if node.single_line?
68
- range_with_surrounding_space(node.body.source_range)
69
- else
70
- range_by_whole_lines(node.body.source_range, include_final_newline: true)
71
- end
60
+ body_range = node.body.source_range
61
+
62
+ if node.single_line?
63
+ corrector.replace(range_with_surrounding_space(body_range), node.braces? ? '' : ' ')
64
+ elsif own_lines?(body_range)
65
+ corrector.remove(range_by_whole_lines(body_range, include_final_newline: true))
66
+ else
67
+ corrector.remove(body_range_on_shared_line(body_range))
68
+ end
69
+ end
70
+
71
+ def own_lines?(range)
72
+ range_by_whole_lines(range).source.strip == range.source
73
+ end
74
+
75
+ def body_range_on_shared_line(body_range)
76
+ side = begins_its_line?(body_range) ? :right : :left
72
77
 
73
- corrector.remove(range)
78
+ range_with_surrounding_space(body_range, side: side, newlines: false)
74
79
  end
75
80
  end
76
81
  end
@@ -91,6 +91,9 @@ module RuboCop
91
91
  end
92
92
 
93
93
  def autocorrect(corrector, node)
94
+ receiver = node.method?(:!) ? node.receiver.receiver : node.receiver
95
+ return if receiver && correction_removes_heredoc?(node, receiver)
96
+
94
97
  case node.method_name
95
98
  when :!=
96
99
  autocorrect_comparison(corrector, node)
@@ -101,6 +104,16 @@ module RuboCop
101
104
  end
102
105
  end
103
106
 
107
+ def correction_removes_heredoc?(node, receiver)
108
+ return false if node.single_line?
109
+
110
+ # Only the receiver is kept, so a heredoc terminator anywhere else in the
111
+ # node is lost, whether the heredoc was opened in the receiver or before it.
112
+ processed_source.tokens_within(node).any? do |token|
113
+ token.type == :tSTRING_END && !receiver.source_range.contains?(token.pos)
114
+ end
115
+ end
116
+
104
117
  def unless_and_nil_check?(send_node)
105
118
  parent = send_node.parent
106
119
 
@@ -154,7 +167,7 @@ module RuboCop
154
167
 
155
168
  def autocorrect_unless_nil(corrector, node, receiver)
156
169
  corrector.replace(node.parent.loc.keyword, 'if')
157
- corrector.replace(node, receiver.source)
170
+ corrector.replace(node, receiver ? receiver.source : 'self')
158
171
  end
159
172
 
160
173
  def nil_comparison_style
@@ -5,7 +5,10 @@ module RuboCop
5
5
  module Style
6
6
  # Checks for redundant dot before operator method call.
7
7
  # The target operator methods are `|`, `^`, `&`, ``<=>``, `==`, `===`, `=~`, `>`, `>=`, `<`,
8
- # ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `~`, `!`, `!=`, and `!~`.
8
+ # ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `!=`, and `!~`.
9
+ #
10
+ # `!` and `~` are not targeted because they have no binary operator form,
11
+ # so `foo.! bar` cannot be written as `foo ! bar`, which means `foo(!bar)`.
9
12
  #
10
13
  # @example
11
14
  #
@@ -21,22 +24,26 @@ module RuboCop
21
24
  extend AutoCorrector
22
25
 
23
26
  MSG = 'Redundant dot detected.'
24
- RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ ! != !~].freeze
27
+ RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** != !~].freeze
25
28
  INVALID_SYNTAX_ARG_TYPES = %i[
26
29
  splat kwsplat forwarded_args forwarded_restarg forwarded_kwrestarg block_pass
27
30
  ].freeze
28
31
 
32
+ def self.autocorrect_incompatible_with
33
+ [Style::TrailingCommaInArguments]
34
+ end
35
+
29
36
  # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
30
37
  def on_send(node)
31
38
  return unless (dot = node.loc.dot)
32
- return if unary_method_no_operator?(node)
33
- return if node.receiver.const_type? || !node.arguments.one?
39
+ return if node.receiver.const_type? || !node.arguments.one? || node.block_literal?
34
40
 
35
41
  return unless (rhs = node.first_argument)
36
42
  return if method_call_with_parenthesized_arg?(rhs)
37
43
  return if invalid_syntax_argument?(rhs)
38
44
 
39
45
  add_offense(dot) do |corrector|
46
+ remove_trailing_comma(corrector, node)
40
47
  wrap_in_parentheses_if_chained(corrector, node)
41
48
  corrector.replace(dot, ' ')
42
49
 
@@ -47,14 +54,6 @@ module RuboCop
47
54
 
48
55
  private
49
56
 
50
- # `foo.~@` and `foo.!@` call the method `~` and `!` respectively. While those
51
- # are operator methods, we don't want to actually consider them as such.
52
- def unary_method_no_operator?(node)
53
- return false unless node.nonmutating_unary_operator_method?
54
-
55
- node.method_name.to_s != node.selector.source
56
- end
57
-
58
57
  # Checks for an acceptable case of `foo.+(bar).baz`.
59
58
  def method_call_with_parenthesized_arg?(argument)
60
59
  return false unless argument.parent.parent&.send_type?
@@ -68,14 +67,22 @@ module RuboCop
68
67
  INVALID_SYNTAX_ARG_TYPES.include?(type)
69
68
  end
70
69
 
71
- def wrap_in_parentheses_if_chained(corrector, node)
72
- return unless node.parent&.call_type?
73
- return if node.parent.first_argument == node
70
+ def remove_trailing_comma(corrector, node)
71
+ return unless node.parenthesized?
74
72
 
75
- operator = node.loc.selector
73
+ comma = processed_source.tokens_within(node).find do |token|
74
+ token.begin_pos >= node.last_argument.source_range.end_pos && !token.comment?
75
+ end
76
+ corrector.remove(comma.pos) if comma&.comma?
77
+ end
78
+
79
+ def wrap_in_parentheses_if_chained(corrector, node)
80
+ return unless chained?(node)
76
81
 
77
- ParenthesesCorrector.correct(corrector, node)
78
- corrector.insert_after(operator, ' ')
82
+ if node.parenthesized?
83
+ ParenthesesCorrector.correct(corrector, node)
84
+ corrector.insert_after(node.loc.selector, ' ')
85
+ end
79
86
  corrector.wrap(node, '(', ')')
80
87
  end
81
88
 
@@ -84,14 +91,20 @@ module RuboCop
84
91
  selector = node.loc.selector
85
92
 
86
93
  return true if selector.end_pos == rhs.source_range.begin_pos
87
- return false if node.parent&.call_type? # if chained, a space is already added
94
+ return false if chained?(node) # if chained, a space is already added
95
+
96
+ # Without a space, `foo +(bar)` is parsed as `foo(+(bar))` and `foo /(bar)` starts
97
+ # a regexp literal.
98
+ node.parenthesized?
99
+ end
100
+
101
+ def chained?(node)
102
+ parent = node.parent
103
+ return false unless parent&.call_type?
88
104
 
89
- # For `/` operations, if the RHS starts with a `(` without space,
90
- # add one to avoid a syntax error.
91
- range = selector.end.join(rhs.source_range.begin)
92
- return true if node.method?(:/) && range.source == '('
105
+ return true unless parent.first_argument.equal?(node)
93
106
 
94
- false
107
+ parent.binary_operation? && !parent.method?(:[])
95
108
  end
96
109
  end
97
110
  end
@@ -56,7 +56,6 @@ module RuboCop
56
56
  class ParenthesesAroundCondition < Base
57
57
  include SafeAssignment
58
58
  include Parentheses
59
- include RangeHelp
60
59
  extend AutoCorrector
61
60
 
62
61
  def on_if(node)
@@ -82,7 +81,8 @@ module RuboCop
82
81
 
83
82
  control_op_condition(cond) do |first_child, rest_children|
84
83
  return if require_parentheses?(node, first_child)
85
- return if semicolon_separated_expressions?(first_child, rest_children)
84
+ # `(foo; bar)` and `(foo\n bar)` would leave only `foo` as the condition.
85
+ return if rest_children.any?
86
86
  return if modifier_op?(first_child) || first_child.masgn_type?
87
87
  return if command_call_assignment?(first_child)
88
88
  return if parens_allowed?(cond)
@@ -101,14 +101,6 @@ module RuboCop
101
101
  condition_body.send_node.block_literal? && condition_body.keywords?
102
102
  end
103
103
 
104
- def semicolon_separated_expressions?(first_exp, rest_exps)
105
- return false unless (second_exp = rest_exps.first)
106
-
107
- range = range_between(first_exp.source_range.end_pos, second_exp.source_range.begin_pos)
108
-
109
- range.source.include?(';')
110
- end
111
-
112
104
  def modifier_op?(node)
113
105
  return false if node.if_type? && node.ternary?
114
106
  return true if node.rescue_type?
@@ -220,8 +220,7 @@ module RuboCop
220
220
  replacement = "#{select_var}, #{reject_var} = #{partition_call}"
221
221
 
222
222
  corrector.replace(sibling_container, replacement)
223
- range = range_by_whole_lines(container.source_range, include_final_newline: true)
224
- corrector.remove(range)
223
+ corrector.remove(range_with_surrounding_space(container.source_range, side: :left))
225
224
  end
226
225
 
227
226
  def complementary_variable_order(sibling, container, sibling_container)
@@ -109,6 +109,9 @@ module RuboCop
109
109
  # @private
110
110
  # @param [RuboCop::AST::Node] node
111
111
  def on_back_ref_or_gvar_or_nth_ref(node)
112
+ # `alias $MATCH $&` names the variables, it does not read them.
113
+ return if node.parent&.alias_type?
114
+
112
115
  preferred_expression = preferred_expression_to_node_with_constant_prefix(node)
113
116
  return unless preferred_expression
114
117
 
@@ -56,6 +56,10 @@ module RuboCop
56
56
 
57
57
  RESTRICT_ON_SEND = %i[raise fail].freeze
58
58
 
59
+ def self.autocorrect_incompatible_with
60
+ [Style::TrailingCommaInArguments]
61
+ end
62
+
59
63
  def on_send(node)
60
64
  return unless node.command?(:raise) || node.command?(:fail)
61
65
 
@@ -171,10 +175,21 @@ module RuboCop
171
175
 
172
176
  def correct_heredoc_compact_to_exploded(corrector, node)
173
177
  new_call = node.first_argument
178
+ remove_trailing_comma(corrector, new_call)
174
179
  corrector.replace(range_between(new_call.receiver, new_call.first_argument), ', ')
175
180
  corrector.remove(new_call.loc.end) if new_call.loc.end
176
181
  end
177
182
 
183
+ def remove_trailing_comma(corrector, new_call)
184
+ return unless new_call.loc.end
185
+
186
+ argument_end = new_call.first_argument.source_range.end
187
+ token = processed_source.first_token_of(argument_end)
188
+ return unless token.comma?
189
+
190
+ corrector.remove(argument_end.join(token.pos))
191
+ end
192
+
178
193
  def correct_raise(node)
179
194
  opposite_style_detected
180
195
  return if inside_corrected_raise?(node)
@@ -29,6 +29,10 @@ module RuboCop
29
29
 
30
30
  RESTRICT_ON_SEND = %i[new [] Array].freeze
31
31
 
32
+ def self.autocorrect_incompatible_with
33
+ [Style::TrailingCommaInArguments]
34
+ end
35
+
32
36
  # @!method redundant_array_constructor(node)
33
37
  def_node_matcher :redundant_array_constructor, <<~PATTERN
34
38
  {
@@ -67,8 +71,21 @@ module RuboCop
67
71
  private
68
72
 
69
73
  def register_offense(range, node, replacement)
74
+ replacement = replacement.source_range if replacement.is_a?(AST::Node)
75
+
70
76
  add_offense(range) do |corrector|
71
- corrector.replace(node, replacement.source)
77
+ next if removes_comment_or_heredoc?(node, replacement)
78
+
79
+ corrector.remove(node.source_range.begin.join(replacement.begin))
80
+ corrector.remove(replacement.end.join(node.source_range.end))
81
+ end
82
+ end
83
+
84
+ def removes_comment_or_heredoc?(node, literal)
85
+ return false if node.single_line?
86
+
87
+ processed_source.tokens_within(node).any? do |token|
88
+ (token.comment? || token.type == :tSTRING_END) && !literal.contains?(token.pos)
72
89
  end
73
90
  end
74
91
  end
@@ -62,15 +62,23 @@ module RuboCop
62
62
  # baz
63
63
  # end
64
64
  # end
65
- class RedundantBegin < Base
65
+ class RedundantBegin < Base # rubocop:disable Metrics/ClassLength
66
66
  include RangeHelp
67
67
  extend AutoCorrector
68
68
 
69
69
  MSG = 'Redundant `begin` block detected.'
70
70
  SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
71
+ VALUE_PARENT_TYPES = %i[return next break optarg kwoptarg].freeze
72
+ STATEMENT_CONTAINER_TYPES = %i[
73
+ begin kwbegin any_block any_def class module sclass rescue resbody ensure
74
+ ].freeze
71
75
 
72
76
  def self.autocorrect_incompatible_with
73
- [Style::BlockDelimiters]
77
+ [
78
+ Style::TrailingCommaInArguments,
79
+ Style::TrailingCommaInArrayLiteral,
80
+ Style::TrailingCommaInHashLiteral
81
+ ]
74
82
  end
75
83
 
76
84
  # @!method offensive_kwbegins(node)
@@ -103,7 +111,7 @@ module RuboCop
103
111
  body = node.body
104
112
 
105
113
  return unless body&.kwbegin_type?
106
- return if body.rescue_node || body.ensure_node
114
+ return if body.ensure_node || body.rescue_node
107
115
 
108
116
  register_offense(body)
109
117
  end
@@ -138,27 +146,67 @@ module RuboCop
138
146
  end
139
147
 
140
148
  def register_offense(node)
149
+ return if end_cannot_join_statement_line?(node)
150
+
141
151
  offense_range = node.loc.begin
142
152
 
143
153
  add_offense(offense_range) do |corrector|
144
- if node.parent&.assignment? || node.parent&.type?(:return, :next, :break)
154
+ if node.parent&.assignment? || node.parent&.type?(*VALUE_PARENT_TYPES)
145
155
  replace_begin_with_statement(corrector, offense_range, node)
146
156
  else
147
157
  remove_begin(corrector, offense_range, node)
148
158
  end
149
159
 
150
- if use_modifier_form_after_multiline_begin_block?(node)
151
- correct_modifier_form_after_multiline_begin_block(corrector, node)
152
- end
153
- corrector.remove(node.loc.end)
160
+ correct_end(corrector, node)
154
161
  end
155
162
  end
156
163
 
164
+ def end_cannot_join_statement_line?(node)
165
+ return false unless code_after_end_on_its_own_line?(node)
166
+
167
+ range_with_end_line_break(node).begin_pos != statement_end_pos(node)
168
+ end
169
+
170
+ def use_modifier_form_after_multiline_begin_block?(node)
171
+ return false unless (parent = node.parent)
172
+ return false unless node.multiline? && parent.if_type? && parent.modifier_form?
173
+
174
+ !parent.condition.equal?(node)
175
+ end
176
+
177
+ def code_after_end_on_its_own_line?(node)
178
+ return false unless begins_its_line?(node.loc.end)
179
+
180
+ rest_of_line = rest_of_line(closing_range(node)).lstrip
181
+
182
+ !rest_of_line.empty? && !rest_of_line.start_with?('#')
183
+ end
184
+
185
+ def closing_range(node)
186
+ return node.loc.end unless use_modifier_form_after_multiline_begin_block?(node)
187
+
188
+ node.loc.end.join(condition_range(node.parent))
189
+ end
190
+
191
+ def rest_of_line(range)
192
+ range.source_buffer.source_line(range.last_line)[range.last_column..]
193
+ end
194
+
195
+ def range_with_end_line_break(node)
196
+ range_with_surrounding_space(node.loc.end, side: :left, newlines: true)
197
+ end
198
+
199
+ def statement_end_pos(node)
200
+ return node.loc.begin.end_pos if node.children.empty?
201
+
202
+ node.children.last.source_range.end_pos
203
+ end
204
+
157
205
  def replace_begin_with_statement(corrector, offense_range, node)
158
206
  first_child = node.children.first
159
207
 
160
208
  source = first_child.source
161
- source = "(#{source})" if low_precedence_statement?(first_child)
209
+ source = "(#{source})" if parentheses_required?(first_child, node.parent)
162
210
 
163
211
  corrector.replace(offense_range, source)
164
212
  corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
@@ -166,6 +214,15 @@ module RuboCop
166
214
  restore_removed_comments(corrector, offense_range, node, first_child)
167
215
  end
168
216
 
217
+ def parentheses_required?(statement, parent)
218
+ return true if low_precedence_statement?(statement)
219
+ return false unless command_call?(statement)
220
+ return parent.type?(:optarg, :kwoptarg) unless parent.assignment?
221
+
222
+ parent = parent.parent while parent.parent&.assignment?
223
+ !statement_position?(parent)
224
+ end
225
+
169
226
  def low_precedence_statement?(node)
170
227
  return node.modifier_form? if node.basic_conditional?
171
228
  return node.prefix_not? if node.send_type?
@@ -173,6 +230,22 @@ module RuboCop
173
230
  node.operator_keyword? && node.semantic_operator?
174
231
  end
175
232
 
233
+ def command_call?(node)
234
+ node = node.send_node if node.any_block_type?
235
+ return false unless node.type?(:call, :super, :yield)
236
+
237
+ node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
238
+ end
239
+
240
+ def statement_position?(node)
241
+ parent = node.parent
242
+ return true if parent.nil? || parent.type?(*STATEMENT_CONTAINER_TYPES)
243
+ return parent.body.equal?(node) if parent.type?(:when, :in_pattern, :for)
244
+ return false if parent.if_type? && parent.ternary?
245
+
246
+ parent.type?(:if, :while, :until, :case, :case_match) && !parent.condition.equal?(node)
247
+ end
248
+
176
249
  def remove_begin(corrector, offense_range, node)
177
250
  if node.parent.respond_to?(:endless?) && node.parent.endless?
178
251
  offense_range = range_with_surrounding_space(offense_range, newlines: true)
@@ -190,18 +263,34 @@ module RuboCop
190
263
  corrector.insert_before(node.parent, comments) unless comments.blank?
191
264
  end
192
265
 
193
- def use_modifier_form_after_multiline_begin_block?(node)
194
- return false unless (parent = node.parent)
195
-
196
- node.multiline? && parent.if_type? && parent.modifier_form? &&
197
- !parent.condition.equal?(node)
266
+ def correct_end(corrector, node)
267
+ if use_modifier_form_after_multiline_begin_block?(node)
268
+ correct_modifier_form_after_multiline_begin_block(corrector, node)
269
+ elsif code_after_end_on_its_own_line?(node)
270
+ corrector.remove(range_with_end_line_break(node))
271
+ else
272
+ corrector.remove(node.loc.end)
273
+ end
198
274
  end
199
275
 
200
276
  def correct_modifier_form_after_multiline_begin_block(corrector, node)
201
277
  condition_range = condition_range(node.parent)
202
278
 
203
279
  corrector.insert_after(node.children.first, " #{condition_range.source}")
204
- corrector.remove(range_by_whole_lines(condition_range, include_final_newline: true))
280
+ corrector.remove(modifier_form_closing_removal_range(node))
281
+ end
282
+
283
+ def modifier_form_closing_removal_range(node)
284
+ closing_range = closing_range(node)
285
+
286
+ if range_with_end_line_break(node).begin_pos == statement_end_pos(node)
287
+ range_with_surrounding_space(closing_range, side: :left, newlines: true)
288
+ elsif rest_of_line(closing_range).strip.empty?
289
+ range_by_whole_lines(closing_range, include_final_newline: true)
290
+ else
291
+ # Only a comment can follow here, and it stays on its own line.
292
+ range_with_surrounding_space(closing_range, side: :right, newlines: false)
293
+ end
205
294
  end
206
295
 
207
296
  def condition_range(node)
@@ -238,7 +327,9 @@ module RuboCop
238
327
  end
239
328
 
240
329
  def valid_begin_assignment?(node)
241
- node.parent&.assignment? && (!node.children.one? || too_long_when_joined?(node))
330
+ return false unless node.parent&.assignment?
331
+
332
+ !node.children.one? || too_long_when_joined?(node) || multiline_memoization?(node)
242
333
  end
243
334
 
244
335
  def too_long_when_joined?(node)
@@ -264,10 +355,19 @@ module RuboCop
264
355
  SPACE_INSIDE_PARENS_STYLES.include?(style)
265
356
  end
266
357
 
358
+ def multiline_memoization?(node)
359
+ if !node.parent.or_asgn_type? || !config.cop_enabled?('Style/MultilineMemoization')
360
+ return false
361
+ end
362
+
363
+ statement = node.children.first
364
+ statement.multiline? && low_precedence_statement?(statement)
365
+ end
366
+
267
367
  def inspect_branches(node)
268
368
  node.branches.each do |branch|
269
369
  next unless branch&.kwbegin_type?
270
- next if branch.rescue_node || branch.ensure_node
370
+ next if branch.ensure_node || branch.rescue_node
271
371
  next if ternary_operand_requiring_begin?(branch)
272
372
 
273
373
  register_offense(branch)