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
@@ -85,7 +85,7 @@ module RuboCop
85
85
  def range(node)
86
86
  return node.selector unless node.method?(:each)
87
87
 
88
- if node.parent&.call_type?
88
+ if receiver_of_dot_call?(node)
89
89
  node.selector.join(node.parent.loc.dot)
90
90
  else
91
91
  node.loc.dot.join(node.selector)
@@ -104,6 +104,9 @@ module RuboCop
104
104
  end
105
105
 
106
106
  def remove_redundant_each(corrector, range, redundant_node)
107
+ return unless removable_each?(range)
108
+ return unless (range = with_preceding_line_break(range, redundant_node))
109
+
107
110
  corrector.remove(range)
108
111
 
109
112
  if redundant_node.method?(:each_with_index)
@@ -112,6 +115,28 @@ module RuboCop
112
115
  corrector.replace(redundant_node.loc.selector, 'each.with_object')
113
116
  end
114
117
  end
118
+
119
+ def removable_each?(range)
120
+ between = processed_source.tokens_within(range)[1...-1]
121
+ between.empty? || between.map(&:type) == %i[tLPAREN2 tRPAREN]
122
+ end
123
+
124
+ # The block of an `each` that starts its line would be left without a call, so the
125
+ # line break goes too, unless there's a comment before it.
126
+ def with_preceding_line_break(range, redundant_node)
127
+ receiver_end = redundant_node.source_range.end
128
+ return range if receiver_end.end_pos >= range.begin_pos
129
+
130
+ gap = receiver_end.join(range.begin)
131
+ range.join(gap) if gap.source.strip.empty?
132
+ end
133
+
134
+ def receiver_of_dot_call?(node)
135
+ parent = node.parent
136
+ return false unless parent&.call_type?
137
+
138
+ parent.receiver.equal?(node) && parent.loc?(:dot)
139
+ end
115
140
  end
116
141
  end
117
142
  end
@@ -43,7 +43,10 @@ module RuboCop
43
43
  next if message.nil_type?
44
44
 
45
45
  add_offense(node, message: MSG_1) do |corrector|
46
- corrector.remove(exception.source_range.join(message.source_range.begin))
46
+ prefix = exception.source_range.join(message.source_range.begin)
47
+ next if comment_or_heredoc_in?(prefix)
48
+
49
+ corrector.remove(prefix)
47
50
  corrector.replace(message, message_source(message))
48
51
  end
49
52
  end
@@ -58,17 +61,24 @@ module RuboCop
58
61
  next if message.nil_type?
59
62
 
60
63
  add_offense(node, message: MSG_2) do |corrector|
61
- # Replacing the call would drop a heredoc body that sits inside its parentheses.
62
- next if heredoc_body_inside?(new_call, message)
64
+ next if unsafe_to_unwrap?(new_call, message)
63
65
 
64
66
  corrector.replace(new_call, message_source(message))
65
67
  end
66
68
  end
67
69
  end
68
70
 
69
- def heredoc_body_inside?(new_call, message)
70
- message.each_node(:any_str).any? do |str|
71
- str.heredoc? && str.loc.heredoc_end.end_pos < new_call.source_range.end_pos
71
+ def unsafe_to_unwrap?(new_call, message)
72
+ prefix = new_call.source_range.begin.join(message.source_range.begin)
73
+ suffix = message.source_range.end.join(new_call.source_range.end)
74
+ comment_or_heredoc_in?(prefix) || comment_or_heredoc_in?(suffix)
75
+ end
76
+
77
+ def comment_or_heredoc_in?(range)
78
+ return false if range.single_line?
79
+
80
+ processed_source.tokens_within(range).any? do |token|
81
+ (token.comment? || token.type == :tSTRING_END) && range.contains?(token.pos)
72
82
  end
73
83
  end
74
84
 
@@ -110,12 +110,21 @@ module RuboCop
110
110
 
111
111
  MSG = 'Remove unnecessary `initialize` method.'
112
112
  MSG_EMPTY = 'Remove unnecessary empty `initialize` method.'
113
+ VALUE_DISCARDING_BODY_TYPES = %i[
114
+ class module sclass block numblock itblock def defs while until for
115
+ ].freeze
116
+ VALUE_PASSING_BODY_TYPES = %i[when in_pattern resbody].freeze
113
117
 
114
118
  # @!method initialize_forwards?(node)
115
119
  def_node_matcher :initialize_forwards?, <<~PATTERN
116
120
  (def _ (args $arg*) $({super zsuper} ...))
117
121
  PATTERN
118
122
 
123
+ # @!method private_definition?(node)
124
+ def_node_matcher :private_definition?, <<~PATTERN
125
+ (send nil? :private def)
126
+ PATTERN
127
+
119
128
  def on_def(node)
120
129
  return if acceptable?(node)
121
130
 
@@ -136,12 +145,28 @@ module RuboCop
136
145
 
137
146
  def register_offense(node, message)
138
147
  add_offense(node, message: message) do |corrector|
139
- corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
148
+ corrector.remove(removal_range(node))
140
149
  end
141
150
  end
142
151
 
152
+ def removal_range(node)
153
+ statement = statement_of(node)
154
+ return shared_line_range(statement) unless own_lines?(statement)
155
+
156
+ range = statement.source_range
157
+ return range_with_surrounding_space(range, side: :right) if trailing_comment(statement)
158
+
159
+ range_by_whole_lines(range, include_final_newline: true)
160
+ end
161
+
162
+ def statement_of(node)
163
+ private_definition?(node.parent) ? node.parent : node
164
+ end
165
+
143
166
  def acceptable?(node)
144
- !node.method?(:initialize) || forwards?(node) || allow_comments?(node)
167
+ return true unless node.method?(:initialize)
168
+
169
+ forwards?(node) || allow_comments?(node) || !removable?(node)
145
170
  end
146
171
 
147
172
  def forwards?(node)
@@ -154,6 +179,94 @@ module RuboCop
154
179
  contains_comments?(node) && !comments_contain_disables?(node, name)
155
180
  end
156
181
 
182
+ def removable?(node)
183
+ statement = statement_of(node)
184
+ return false unless statement?(statement)
185
+
186
+ own_lines?(statement) || !shared_line_range(statement).nil?
187
+ end
188
+
189
+ def own_lines?(node)
190
+ code_on_lines_of(node) == node.source
191
+ end
192
+
193
+ def shared_line_range(node)
194
+ return if heredoc_body_after?(node)
195
+ return unless node.parent&.type?(:begin, :kwbegin)
196
+
197
+ range = sibling_separator_range(node)
198
+ range unless range.nil? || foreign_heredoc_body_in?(range, node)
199
+ end
200
+
201
+ def sibling_separator_range(node)
202
+ range = node.source_range
203
+ if (following = node.right_sibling) && following.first_line == node.last_line
204
+ range.join(following.source_range.begin)
205
+ elsif (preceding = node.left_sibling) && preceding.last_line == node.first_line
206
+ preceding.source_range.end.join(range)
207
+ end
208
+ end
209
+
210
+ def heredoc_body_after?(node)
211
+ node.each_descendant(:any_str).any? do |str|
212
+ str.heredoc? && str.loc.heredoc_end.end_pos > node.source_range.end_pos
213
+ end
214
+ end
215
+
216
+ # The body of a heredoc opened by another statement would be removed with the range.
217
+ def foreign_heredoc_body_in?(range, node)
218
+ processed_source.ast.each_node(:any_str).any? do |str|
219
+ next false if !str.heredoc? || node.source_range.contains?(str.source_range)
220
+
221
+ range.overlaps?(str.loc.heredoc_body)
222
+ end
223
+ end
224
+
225
+ def code_on_lines_of(node)
226
+ lines = range_by_whole_lines(node.source_range)
227
+ comment = trailing_comment(node)
228
+ return lines.source.strip.delete_suffix(';').rstrip unless comment
229
+
230
+ lines.begin.join(comment.source_range.begin).source.strip
231
+ end
232
+
233
+ def trailing_comment(node)
234
+ processed_source.comment_at_line(node.last_line)
235
+ end
236
+
237
+ def statement?(node)
238
+ parent = node.parent
239
+ return true if parent.nil? || discards_value_of?(parent, node)
240
+
241
+ passes_on_value_of?(parent, node) && statement?(parent)
242
+ end
243
+
244
+ def discards_value_of?(parent, node)
245
+ case parent.type
246
+ when :begin, :kwbegin
247
+ !parent.children.last.equal?(node)
248
+ when :ensure
249
+ parent.branch.equal?(node)
250
+ when *VALUE_DISCARDING_BODY_TYPES
251
+ parent.body.equal?(node)
252
+ else
253
+ false
254
+ end
255
+ end
256
+
257
+ def passes_on_value_of?(parent, node)
258
+ case parent.type
259
+ when :begin, :kwbegin, :rescue, :ensure
260
+ true
261
+ when :if, :case, :case_match
262
+ !parent.condition.equal?(node)
263
+ when *VALUE_PASSING_BODY_TYPES
264
+ parent.body.equal?(node)
265
+ else
266
+ false
267
+ end
268
+ end
269
+
157
270
  def same_args?(super_node, args)
158
271
  return true if super_node.zsuper_type?
159
272
 
@@ -43,14 +43,14 @@ module RuboCop
43
43
  MSG = 'Prefer `to_s` over string interpolation.'
44
44
 
45
45
  def self.autocorrect_incompatible_with
46
- [Style::LineEndConcatenation]
46
+ [Style::MethodCallWithArgsParentheses]
47
47
  end
48
48
 
49
49
  def on_dstr(node)
50
50
  return unless single_interpolation?(node)
51
51
 
52
52
  embedded_node = node.children.first
53
- return if use_match_pattern?(embedded_node)
53
+ return if void_value?(embedded_node)
54
54
 
55
55
  add_offense(node) do |corrector|
56
56
  if variable_interpolation?(embedded_node)
@@ -72,18 +72,36 @@ module RuboCop
72
72
  !embedded_in_percent_array?(node)
73
73
  end
74
74
 
75
- def use_match_pattern?(node)
76
- return false if target_ruby_version <= 2.7
75
+ def void_value?(node)
76
+ return false unless node
77
+ return true if node.type?(:return, :break, :next, :redo, :retry)
78
+ return target_ruby_version >= 3.0 if node.match_pattern_type?
77
79
 
78
- node.children.any? do |child|
79
- child.respond_to?(:match_pattern_type?) && child.match_pattern_type?
80
+ void_compound_statement?(node)
81
+ end
82
+
83
+ def void_compound_statement?(node)
84
+ case node.type
85
+ when :begin, :kwbegin then void_value?(node.children.last)
86
+ when :if then void_value?(node.if_branch) && void_value?(node.else_branch)
87
+ when :rescue then rescue_void_value?(node)
88
+ when :ensure then void_value?(node.children.first) || void_value?(node.branch)
89
+ else false
80
90
  end
81
91
  end
82
92
 
93
+ def rescue_void_value?(node)
94
+ return false unless void_value?(node.body)
95
+ return void_value?(node.else_branch) if node.else?
96
+
97
+ node.resbody_branches.all? { |resbody| void_value?(resbody.body) }
98
+ end
99
+
83
100
  def single_variable_interpolation?(node)
84
101
  return false unless node.children.one?
85
102
 
86
103
  first_child = node.children.first
104
+ return false unless first_child.last_line == node.last_line
87
105
 
88
106
  variable_interpolation?(first_child) ||
89
107
  (first_child.send_type? && !first_child.operator_method?)
@@ -111,21 +129,24 @@ module RuboCop
111
129
  corrector.replace(node, replacement)
112
130
  end
113
131
 
132
+ # rubocop:disable-next Metrics/AbcSize
114
133
  def autocorrect_single_variable_interpolation(corrector, embedded_node, node)
115
134
  embedded_var = embedded_node.children.first
135
+ suffix = '.to_s'
136
+
137
+ if require_parentheses?(embedded_var)
138
+ corrector.replace(arguments_begin_range(embedded_var), '(')
139
+ suffix = ').to_s'
140
+ end
116
141
 
117
- source = if require_parentheses?(embedded_var)
118
- receiver = range_between(
119
- embedded_var.source_range.begin_pos, embedded_var.loc.selector.end_pos
120
- )
121
- arguments = embedded_var.arguments.map(&:source).join(', ')
142
+ corrector.remove(node.source_range.begin.join(embedded_var.source_range.begin))
143
+ corrector.replace(embedded_var.source_range.end.join(node.source_range.end), suffix)
144
+ end
122
145
 
123
- "#{receiver.source}(#{arguments})"
124
- else
125
- embedded_var.source
126
- end
146
+ def arguments_begin_range(send_node)
147
+ first_argument_range = send_node.first_argument.source_range
127
148
 
128
- corrector.replace(node, "#{source}.to_s")
149
+ send_node.loc.selector.end.join(first_argument_range.begin)
129
150
  end
130
151
 
131
152
  def autocorrect_other(corrector, embedded_node, node)
@@ -31,14 +31,14 @@ module RuboCop
31
31
  minimum_target_ruby_version 3.0
32
32
 
33
33
  def self.autocorrect_incompatible_with
34
- [Layout::MultilineMethodCallBraceLayout]
34
+ [Layout::MultilineMethodCallBraceLayout, Style::TrailingCommaInArguments]
35
35
  end
36
36
 
37
37
  # @!method redundant_unfreeze?(node)
38
38
  def_node_matcher :redundant_unfreeze?, <<~PATTERN
39
39
  {
40
40
  (send dstr_type? {:+@ :dup})
41
- (send (const nil? :String) :new dstr_type?)
41
+ (send (const {nil? cbase} :String) :new dstr_type?)
42
42
  }
43
43
  PATTERN
44
44
 
@@ -30,6 +30,12 @@ module RuboCop
30
30
  PERCENT_CAPITAL_Q = '%Q'
31
31
  STRING_INTERPOLATION_REGEXP = /#\{.+\}/.freeze
32
32
  ESCAPED_NON_BACKSLASH = /\\[^\\]/.freeze
33
+ ESCAPED_QUOTE = /(?<!\\)(?:\\\\)*\\"/.freeze
34
+ UNESCAPED_QUOTE = /(?<!\\)(?:\\\\)*"/.freeze
35
+
36
+ def self.autocorrect_incompatible_with
37
+ [Style::RedundantStringEscape]
38
+ end
33
39
 
34
40
  def on_dstr(node)
35
41
  return unless string_literal?(node)
@@ -53,7 +59,7 @@ module RuboCop
53
59
  return if interpolated_quotes?(node) || allowed_percent_q?(node)
54
60
 
55
61
  add_offense(node) do |corrector|
56
- delimiter = /\A%Q[^"]+\z|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE
62
+ delimiter = double_quotes_delimiter?(node.source) ? QUOTE : SINGLE_QUOTE
57
63
 
58
64
  corrector.replace(node.loc.begin, delimiter)
59
65
  corrector.replace(node.loc.end, delimiter)
@@ -69,6 +75,12 @@ module RuboCop
69
75
  (node.source.start_with?(PERCENT_CAPITAL_Q) && acceptable_capital_q?(node))
70
76
  end
71
77
 
78
+ def double_quotes_delimiter?(src)
79
+ return true if /\A%Q[^"]+\z|'/.match?(src)
80
+
81
+ src.start_with?(PERCENT_CAPITAL_Q) && ESCAPED_QUOTE.match?(src)
82
+ end
83
+
72
84
  def message(node)
73
85
  src = node.source
74
86
  extra = if src.start_with?(PERCENT_CAPITAL_Q)
@@ -102,7 +114,8 @@ module RuboCop
102
114
  src = node.source
103
115
  src.include?(QUOTE) &&
104
116
  (STRING_INTERPOLATION_REGEXP.match?(src) ||
105
- (node.str_type? && double_quotes_required?(src)))
117
+ (node.str_type? && double_quotes_required?(src)) ||
118
+ (ESCAPED_QUOTE.match?(src) && UNESCAPED_QUOTE.match?(src)))
106
119
  end
107
120
  end
108
121
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  return unless (regexp = redundant_regexp_constructor(node))
35
35
 
36
36
  add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
37
- next if ambiguous_slash_regexp?(node, regexp)
37
+ next if ambiguous_slash_regexp?(node, regexp) || drops_heredoc_or_comment?(node, regexp)
38
38
 
39
39
  # Reuse the inner literal's own source so its delimiters are preserved.
40
40
  # Forcing `/.../` would break patterns like `%r{foo/bar}` that contain
@@ -57,6 +57,16 @@ module RuboCop
57
57
  end
58
58
  end
59
59
 
60
+ def drops_heredoc_or_comment?(node, regexp)
61
+ return false if node.single_line?
62
+
63
+ processed_source.tokens_within(node).any? do |token|
64
+ next false if regexp.source_range.contains?(token.pos)
65
+
66
+ token.comment? || token.type == :tSTRING_END
67
+ end
68
+ end
69
+
60
70
  def command?(call)
61
71
  call.arguments? && !call.parenthesized? && !call.operator_method? && !call.setter_method?
62
72
  end
@@ -54,7 +54,7 @@ module RuboCop
54
54
  yield __FILE__ __LINE__ __ENCODING__].freeze
55
55
 
56
56
  def self.autocorrect_incompatible_with
57
- [ColonMethodCall, Layout::DotPosition]
57
+ [Layout::DotPosition]
58
58
  end
59
59
 
60
60
  def initialize(config = nil, options = nil)
@@ -25,7 +25,6 @@ module RuboCop
25
25
  # foo.concat(ary)
26
26
  #
27
27
  class RedundantSelfAssignment < Base
28
- include RangeHelp
29
28
  extend AutoCorrector
30
29
 
31
30
  MSG = 'Redundant self assignment detected. ' \
@@ -55,7 +54,6 @@ module RuboCop
55
54
  ...))
56
55
  PATTERN
57
56
 
58
- # rubocop:disable-next Metrics/AbcSize
59
57
  def on_lvasgn(node)
60
58
  return unless (rhs = node.rhs)
61
59
  return unless rhs.type?(:any_block, :call) && method_returning_self?(rhs.method_name)
@@ -64,10 +62,7 @@ module RuboCop
64
62
  receiver_type = ASSIGNMENT_TYPE_TO_RECEIVER_TYPE[node.type]
65
63
  return unless receiver.type == receiver_type && receiver.children.first == node.lhs
66
64
 
67
- message = format(MSG, method_name: rhs.method_name)
68
- add_offense(node.loc.operator, message: message) do |corrector|
69
- corrector.replace(node, rhs.source)
70
- end
65
+ register_offense(node, rhs)
71
66
  end
72
67
  alias on_ivasgn on_lvasgn
73
68
  alias on_cvasgn on_lvasgn
@@ -77,10 +72,7 @@ module RuboCop
77
72
  return unless node.assignment_method?
78
73
  return unless redundant_assignment?(node)
79
74
 
80
- message = format(MSG, method_name: node.first_argument.method_name)
81
- add_offense(node.loc.operator, message: message) do |corrector|
82
- corrector.remove(correction_range(node))
83
- end
75
+ register_offense(node, node.first_argument)
84
76
  end
85
77
  alias on_csend on_send
86
78
 
@@ -96,8 +88,24 @@ module RuboCop
96
88
  redundant_self_assignment?(node, node.receiver, receiver_name)
97
89
  end
98
90
 
99
- def correction_range(node)
100
- range_between(node.source_range.begin_pos, node.first_argument.source_range.begin_pos)
91
+ def register_offense(node, rhs)
92
+ message = format(MSG, method_name: rhs.method_name)
93
+ add_offense(node.loc.operator, message: message) do |corrector|
94
+ prefix = node.source_range.begin.join(rhs.source_range.begin)
95
+ corrector.remove(prefix) unless unsafe_prefix?(prefix)
96
+ end
97
+ end
98
+
99
+ def unsafe_prefix?(range)
100
+ return false if range.single_line? && !range.source.include?('<<')
101
+
102
+ processed_source.tokens_within(range).any? do |token|
103
+ case token.type
104
+ when :tSTRING_BEG, :tXSTRING_BEG then token.text.start_with?('<<')
105
+ when :tSTRING_END then true
106
+ else token.comment?
107
+ end
108
+ end
101
109
  end
102
110
  end
103
111
  end
@@ -91,7 +91,7 @@ module RuboCop
91
91
  (call $(call _ $:sort) ${:[] :at :slice} {(int 0) (int -1)})
92
92
 
93
93
  (call $(call _ $:sort_by _) ${:last :first})
94
- (send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
94
+ (call $(call _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
95
95
 
96
96
  (call (any_block $(call _ ${:sort_by :sort}) ...) ${:last :first})
97
97
  (call
@@ -101,6 +101,10 @@ module RuboCop
101
101
  }
102
102
  MATCHER
103
103
 
104
+ def self.autocorrect_incompatible_with
105
+ [Style::TrailingCommaInArguments, Style::TrailingCommaInArrayLiteral, Style::TrailingCommaInHashLiteral]
106
+ end
107
+
104
108
  def on_send(node)
105
109
  ancestor, sort_node, sorter, accessor =
106
110
  find_redundant_sort(node.parent, node.parent&.parent)
@@ -139,7 +143,7 @@ module RuboCop
139
143
 
140
144
  rest = code_after_accessor(node)
141
145
 
142
- !rest.empty? && !rest.match?(/\A(#|&?\.|[)\]},])/)
146
+ !rest.empty? && !rest.match?(/\A(#|&?\.|[)\]}])/)
143
147
  end
144
148
 
145
149
  def code_after_accessor(node)
@@ -45,10 +45,6 @@ module RuboCop
45
45
 
46
46
  MSG = 'Avoid using `rescue` in its modifier form.'
47
47
 
48
- def self.autocorrect_incompatible_with
49
- [Style::MethodCallWithArgsParentheses]
50
- end
51
-
52
48
  def on_resbody(node)
53
49
  return unless rescue_modifier?(node)
54
50
 
@@ -63,11 +63,6 @@ module RuboCop
63
63
  }
64
64
  PATTERN
65
65
 
66
- # @!method env_const?(node)
67
- def_node_matcher :env_const?, <<~PATTERN
68
- (const {nil? cbase} :ENV)
69
- PATTERN
70
-
71
66
  # @!method calls_lvar?(node, name)
72
67
  def_node_matcher :calls_lvar?, <<~PATTERN
73
68
  (send (lvar %1) %CLASS_CHECK_METHODS _)
@@ -96,7 +91,7 @@ module RuboCop
96
91
  def receiver_allowed?(node)
97
92
  return false unless node
98
93
 
99
- node.hash_type? || creates_hash?(node) || env_const?(node)
94
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
100
95
  end
101
96
 
102
97
  def replacement(class_check_send_node, node)
@@ -84,11 +84,6 @@ module RuboCop
84
84
  }
85
85
  PATTERN
86
86
 
87
- # @!method env_const?(node)
88
- def_node_matcher :env_const?, <<~PATTERN
89
- (const {nil? cbase} :ENV)
90
- PATTERN
91
-
92
87
  # @!method between_call?(node, name)
93
88
  def_node_matcher :between_call?, <<~PATTERN
94
89
  (send (lvar %1) :between? _ _)
@@ -117,7 +112,7 @@ module RuboCop
117
112
  def receiver_allowed?(node)
118
113
  return false unless node
119
114
 
120
- node.hash_type? || creates_hash?(node) || env_const?(node)
115
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
121
116
  end
122
117
 
123
118
  def replacement(range_check_send_node, node)
@@ -77,11 +77,6 @@ module RuboCop
77
77
  }
78
78
  PATTERN
79
79
 
80
- # @!method env_const?(node)
81
- def_node_matcher :env_const?, <<~PATTERN
82
- (const {nil? cbase} :ENV)
83
- PATTERN
84
-
85
80
  # @!method calls_lvar?(node, name)
86
81
  def_node_matcher :calls_lvar?, <<~PATTERN
87
82
  {
@@ -120,7 +115,7 @@ module RuboCop
120
115
  def receiver_allowed?(node)
121
116
  return false unless node
122
117
 
123
- node.hash_type? || creates_hash?(node) || env_const?(node)
118
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
124
119
  end
125
120
 
126
121
  def replacement(regexp_method_send_node, node)
@@ -18,10 +18,6 @@ module RuboCop
18
18
  MSG = 'Use self-assignment shorthand `%<method>s=`.'
19
19
  OPS = %i[+ - * ** / % ^ << >> | &].freeze
20
20
 
21
- def self.autocorrect_incompatible_with
22
- [Layout::SpaceAroundOperators]
23
- end
24
-
25
21
  def on_lvasgn(node)
26
22
  check(node, :lvar)
27
23
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  MSG = 'Do not use semicolons to terminate expressions.'
36
36
 
37
37
  def self.autocorrect_incompatible_with
38
- [Style::SingleLineMethods]
38
+ [Style::MethodCallWithArgsParentheses, Style::SingleLineMethods, Style::SuperWithArgsParentheses]
39
39
  end
40
40
 
41
41
  def on_new_investigation
@@ -68,7 +68,7 @@ module RuboCop
68
68
  [Layout::MultilineMethodArgumentLineBreaks]
69
69
  end
70
70
 
71
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
71
+ # rubocop:disable Metrics/AbcSize
72
72
  def on_send(node)
73
73
  return if allow_send? && !node.method?(:public_send)
74
74
  return unless (first_argument = node.first_argument)
@@ -76,7 +76,7 @@ module RuboCop
76
76
 
77
77
  offense_range = offense_range(node)
78
78
  method_name = first_argument.value
79
- return if !METHOD_NAME_PATTERN.match?(method_name) || RESERVED_WORDS.include?(method_name)
79
+ return unless valid_method_name?(method_name)
80
80
 
81
81
  add_offense(offense_range, message: format(MSG, method_name: method_name)) do |corrector|
82
82
  if node.arguments.one?
@@ -88,7 +88,7 @@ module RuboCop
88
88
  end
89
89
  end
90
90
  alias on_csend on_send
91
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
91
+ # rubocop:enable Metrics/AbcSize
92
92
 
93
93
  private
94
94
 
@@ -100,6 +100,11 @@ module RuboCop
100
100
  node.loc.selector.join(node.source_range.end)
101
101
  end
102
102
 
103
+ def valid_method_name?(method_name)
104
+ method_name.to_s.valid_encoding? && METHOD_NAME_PATTERN.match?(method_name) &&
105
+ !RESERVED_WORDS.include?(method_name)
106
+ end
107
+
103
108
  def removal_argument_range(first_argument, second_argument)
104
109
  first_argument.source_range.begin.join(second_argument.source_range.begin)
105
110
  end