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
@@ -100,14 +100,18 @@ module RuboCop
100
100
  # end
101
101
  # end
102
102
  #
103
- class GuardClause < Base
103
+ class GuardClause < Base # rubocop:disable Metrics/ClassLength
104
104
  extend AutoCorrector
105
105
  include RangeHelp
106
106
  include MinBodyLength
107
107
  include StatementModifier
108
+ include RescueNode
108
109
 
109
110
  MSG = 'Use a guard clause (`%<example>s`) instead of wrapping the ' \
110
111
  'code inside a conditional expression.'
112
+ STATEMENT_PARENT_TYPES = %i[
113
+ begin kwbegin block numblock itblock class module sclass ensure
114
+ ].freeze
111
115
 
112
116
  def self.autocorrect_incompatible_with
113
117
  [Style::MissingElse]
@@ -191,6 +195,7 @@ module RuboCop
191
195
 
192
196
  add_offense(node.loc.keyword, message: format(MSG, example: example)) do |corrector|
193
197
  next if node.else? && guard.nil?
198
+ next if code_after_end_needs_conditional?(node, guard)
194
199
 
195
200
  autocorrect(corrector, node, example, replacement, guard)
196
201
  end
@@ -224,9 +229,7 @@ module RuboCop
224
229
  end
225
230
 
226
231
  def find_heredoc_argument(node)
227
- return unless node
228
-
229
- node = node.children.first while node.begin_type?
232
+ return unless (node = unwrap_begin(node))
230
233
  return node if heredoc?(node)
231
234
  return unless node.call_type?
232
235
 
@@ -238,12 +241,17 @@ module RuboCop
238
241
  find_heredoc_argument(node.receiver)
239
242
  end
240
243
 
244
+ def unwrap_begin(node)
245
+ node = node.children.first while node&.begin_type?
246
+ node
247
+ end
248
+
241
249
  def close_multiline_guard_clause(corrector, range, heredoc, guard)
242
250
  corrector.insert_after(guard && heredoc ? heredoc.loc.heredoc_end : range, "\nend")
243
251
  end
244
252
 
245
253
  def autocorrect_heredoc_argument(corrector, node, heredoc_node, leave_branch, guard)
246
- remove_whole_lines(corrector, node.loc.end)
254
+ remove_end(corrector, node.loc.end)
247
255
  return unless node.else?
248
256
 
249
257
  if leave_branch
@@ -255,6 +263,14 @@ module RuboCop
255
263
  remove_whole_lines(corrector, range_of_branch_to_remove(node, guard))
256
264
  end
257
265
 
266
+ def remove_end(corrector, end_range)
267
+ if range_by_whole_lines(end_range).source.strip == end_range.source
268
+ remove_whole_lines(corrector, end_range)
269
+ else
270
+ corrector.remove(end_range)
271
+ end
272
+ end
273
+
258
274
  def range_of_branch_to_remove(node, guard)
259
275
  branch = case guard
260
276
  when :if then node.if_branch
@@ -283,15 +299,35 @@ module RuboCop
283
299
  end
284
300
 
285
301
  def accepted_form?(node, ending: false)
286
- accepted_if?(node, ending) || node.condition.multiline? ||
287
- node.parent&.assignment? || endless_method_body?(node)
302
+ return true if accepted_if?(node, ending) || node.condition.multiline?
303
+
304
+ !statement?(node)
288
305
  end
289
306
 
290
- # The body of an endless method is a single expression, so there is no
291
- # room for a guard clause followed by the rest of the code.
292
- def endless_method_body?(node)
307
+ # A guard clause is followed by the rest of the code as separate statements,
308
+ # so there is no room for them where a single expression is expected,
309
+ # such as an assignment value, a hash value, or the body of an endless method.
310
+ def statement?(node)
293
311
  parent = node.parent
294
- parent&.any_def_type? && parent.endless?
312
+ return true if parent.nil? || parent.type?(*STATEMENT_PARENT_TYPES)
313
+ return !parent.endless? if parent.any_def_type?
314
+ return !rescue_modifier?(parent) if parent.resbody_type?
315
+ return !rescue_modifier?(parent.resbody_branches.first) if parent.rescue_type?
316
+
317
+ statement_branch?(node, parent)
318
+ end
319
+
320
+ def statement_branch?(node, parent)
321
+ case parent.type
322
+ when :if
323
+ !parent.ternary? && !node.equal?(parent.condition)
324
+ when :when, :in_pattern, :while, :until, :for
325
+ node.equal?(parent.body)
326
+ when :case, :case_match
327
+ node.equal?(parent.else_branch)
328
+ else
329
+ false
330
+ end
295
331
  end
296
332
 
297
333
  def trivial?(node)
@@ -300,6 +336,15 @@ module RuboCop
300
336
  node.branches.one? && !node.if_branch.if_type? && !node.if_branch.begin_type?
301
337
  end
302
338
 
339
+ def code_after_end_needs_conditional?(node, guard)
340
+ guard_branch = guard == :else ? node.else_branch : node.if_branch
341
+ return false unless find_heredoc_argument(guard_branch)
342
+
343
+ end_keyword = node.loc.end
344
+
345
+ !end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:\z|[#;)\]}])/)
346
+ end
347
+
303
348
  def accepted_if?(node, ending)
304
349
  return true if node.modifier_form? || node.ternary? || node.elsif_conditional? ||
305
350
  assigned_lvar_used_in_if_branch?(node)
@@ -100,7 +100,9 @@ module RuboCop
100
100
 
101
101
  def register_offense_for_hash(node, hash_argument)
102
102
  add_offense(node, message: MSG_LITERAL_HASH_ARG) do |corrector|
103
- corrector.replace(node, "{#{hash_argument.source}}")
103
+ replacement = hash_argument.source
104
+ replacement = "{#{replacement}}" unless hash_argument.braces?
105
+ corrector.replace(node, replacement)
104
106
  add_parentheses_to_parent_call(node, corrector)
105
107
  end
106
108
  end
@@ -59,7 +59,7 @@ module RuboCop
59
59
  return unless last_replaceable_node
60
60
  return unless arguments.size > 1
61
61
 
62
- range = last_replaceable_node.selector.join(node.loc.end)
62
+ range = last_replaceable_node.selector.join(node.source_range.end)
63
63
  replacement = replacement(arguments)
64
64
  message = format(MSG, replacement: replacement)
65
65
 
@@ -44,6 +44,10 @@ module RuboCop
44
44
  # # good
45
45
  # hash.fetch(key)
46
46
  #
47
+ # # good - `Array[]` and `Hash[]` are constructors
48
+ # Array[element]
49
+ # Hash[pairs]
50
+ #
47
51
  # @example AllowedReceivers: ['Rails.cache']
48
52
  # # good
49
53
  # Rails.cache.fetch(name, options) { block }
@@ -58,8 +62,8 @@ module RuboCop
58
62
 
59
63
  RESTRICT_ON_SEND = %i[[] fetch].freeze
60
64
 
61
- # @!method env_const?(node)
62
- def_node_matcher :env_const?, '(const {nil? cbase} :ENV)'
65
+ # @!method array_or_hash_const?(node)
66
+ def_node_matcher :array_or_hash_const?, '(const {nil? cbase} {:Array :Hash})'
63
67
 
64
68
  def on_send(node)
65
69
  return if (receiver = node.receiver) && allowed_receiver?(receiver)
@@ -108,12 +112,15 @@ module RuboCop
108
112
  end
109
113
 
110
114
  def env_fetch_preferred?(receiver)
111
- env_const?(receiver) && processed_source.registry.enabled?(Style::FetchEnvVar, config)
115
+ receiver.global_const?(:ENV) &&
116
+ processed_source.registry.enabled?(Style::FetchEnvVar, config)
112
117
  end
113
118
 
114
119
  def offense_for_fetch?(node)
115
- style == :fetch && node.method?(:[]) && node.arguments.one? &&
116
- !compound_assignment_target?(node)
120
+ return false if style != :fetch || !node.method?(:[]) || !node.arguments.one?
121
+ return false if array_or_hash_const?(node.receiver)
122
+
123
+ !compound_assignment_target?(node)
117
124
  end
118
125
 
119
126
  def compound_assignment_target?(node)
@@ -144,9 +144,10 @@ module RuboCop
144
144
 
145
145
  # Omitting the last hash value of a call without parentheses adds the parentheses,
146
146
  # which `Style/MethodCallWithArgsParentheses` and `Style/SuperWithArgsParentheses`
147
- # would add again in the same pass.
147
+ # would add again in the same pass. Likewise, `Style/RedundantReturn` would add again
148
+ # the braces wrapped around a braceless hash returned with `return`.
148
149
  def self.autocorrect_incompatible_with
149
- [Style::MethodCallWithArgsParentheses, Style::SuperWithArgsParentheses]
150
+ [Style::MethodCallWithArgsParentheses, Style::RedundantReturn, Style::SuperWithArgsParentheses]
150
151
  end
151
152
 
152
153
  def on_hash(node)
@@ -57,7 +57,7 @@ module RuboCop
57
57
  # @!method on_bad_hash_brackets_map(node)
58
58
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
59
59
  (send
60
- (const _ :Hash)
60
+ (const {nil? cbase} :Hash)
61
61
  :[]
62
62
  (block
63
63
  (call #hash_receiver? {:map :collect})
@@ -55,7 +55,7 @@ module RuboCop
55
55
  # @!method on_bad_hash_brackets_map(node)
56
56
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
57
57
  (send
58
- (const _ :Hash)
58
+ (const {nil? cbase} :Hash)
59
59
  :[]
60
60
  (block
61
61
  (call #hash_receiver? {:map :collect})
@@ -109,10 +109,14 @@ module RuboCop
109
109
  # end
110
110
  class IdenticalConditionalBranches < Base
111
111
  include RangeHelp
112
+ include RescueNode
112
113
  extend AutoCorrector
113
114
 
114
115
  MSG = 'Move `%<source>s` out of the conditional.'
115
116
 
117
+ STATEMENT_BODY_TYPES = %i[kwbegin block numblock itblock ensure].freeze
118
+ VOID_VALUE_TYPES = %i[return break next redo retry].freeze
119
+
116
120
  def on_if(node)
117
121
  return if node.elsif?
118
122
 
@@ -193,10 +197,12 @@ module RuboCop
193
197
  return if expressions.any?(&:nil?)
194
198
 
195
199
  inserted_expression = false
200
+ hoistable = hoistable?(node)
196
201
 
197
202
  expressions.each do |expression|
198
203
  add_offense(expression) do |corrector|
199
- next if node.if_type? && (node.ternary? || node.then?)
204
+ next unless hoistable
205
+ next if void_value_position?(node, expression, insert_position)
200
206
 
201
207
  corrector.remove(removal_range(expression))
202
208
  next if inserted_expression
@@ -212,6 +218,77 @@ module RuboCop
212
218
  end
213
219
  end
214
220
 
221
+ def hoistable?(node)
222
+ return false if node.if_type? && (node.ternary? || node.then?)
223
+
224
+ statement?(node.parent&.assignment? ? node.parent : node)
225
+ end
226
+
227
+ def statement?(node)
228
+ parent = node.parent
229
+ return true if parent.nil? || parent.type?(*STATEMENT_BODY_TYPES)
230
+ return false if inline_form?(parent)
231
+
232
+ statement_branch?(node, parent)
233
+ end
234
+
235
+ def inline_form?(node)
236
+ case node.type
237
+ when :def, :defs
238
+ node.endless?
239
+ when :if, :while, :until
240
+ node.modifier_form?
241
+ when :rescue
242
+ rescue_modifier?(node.resbody_branches.first)
243
+ when :resbody
244
+ rescue_modifier?(node)
245
+ else
246
+ false
247
+ end
248
+ end
249
+
250
+ def statement_branch?(node, parent)
251
+ case parent.type
252
+ when :begin, :def, :defs, :rescue, :resbody
253
+ true
254
+ when :if
255
+ !parent.ternary? && !node.equal?(parent.condition)
256
+ when :class, :module, :sclass, :when, :in_pattern, :for, :while, :until
257
+ node.equal?(parent.body)
258
+ when :case, :case_match
259
+ node.equal?(parent.else_branch)
260
+ else
261
+ false
262
+ end
263
+ end
264
+
265
+ def void_value_position?(node, expression, insert_position)
266
+ return false if insert_position != :after_condition
267
+ return false unless value_position?(node)
268
+
269
+ jump?(expression)
270
+ end
271
+
272
+ def value_position?(node)
273
+ parent = node.parent
274
+ return false unless parent
275
+ return true if parent.assignment?
276
+ return false unless explicit_begin?(parent)
277
+
278
+ node.value_used?
279
+ end
280
+
281
+ def explicit_begin?(node)
282
+ node.kwbegin_type? || (node.begin_type? && !node.loc.begin.nil?)
283
+ end
284
+
285
+ def jump?(node)
286
+ return true if node.type?(*VOID_VALUE_TYPES)
287
+ return false if node.type?(:any_block, :any_def)
288
+
289
+ node.each_child_node.any? { |child| jump?(child) }
290
+ end
291
+
215
292
  def removal_range(expression)
216
293
  range = expression.source_range
217
294
  whole_lines = range_by_whole_lines(range, include_final_newline: true)
@@ -88,7 +88,7 @@ module RuboCop
88
88
  MSG_USE_NORMAL = 'Modifier form of `%<keyword>s` makes the line too long.'
89
89
 
90
90
  def self.autocorrect_incompatible_with
91
- [Style::Next, Style::SoleNestedConditional]
91
+ [Style::MethodCallWithArgsParentheses, Style::Next, Style::SoleNestedConditional]
92
92
  end
93
93
 
94
94
  # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -29,7 +29,7 @@ module RuboCop
29
29
  MSG = 'Avoid modifier `%<keyword>s` after another conditional.'
30
30
 
31
31
  def self.autocorrect_incompatible_with
32
- [Style::SoleNestedConditional]
32
+ [Style::MethodCallWithArgsParentheses, Style::SoleNestedConditional]
33
33
  end
34
34
 
35
35
  # rubocop:disable-next Metrics/AbcSize
@@ -126,8 +126,9 @@ module RuboCop
126
126
  def lower_precedence_than_ternary?(node)
127
127
  return true if node.type?(*LOWER_PRECEDENCE_THAN_TERNARY_TYPES)
128
128
  return true if node.send_type? && (node.prefix_not? || prefix_bang_on_command?(node))
129
+ return true if command_assignment?(node)
129
130
 
130
- node.type?(:yield, :super, :defined?) && node.arguments.any? && !node.parenthesized?
131
+ keyword_command?(node)
131
132
  end
132
133
 
133
134
  def operand_of_parent?(node)
@@ -174,8 +175,37 @@ module RuboCop
174
175
  require_argument_parentheses?(receiver) || lower_precedence_than_ternary?(receiver)
175
176
  end
176
177
 
178
+ def command_assignment?(node)
179
+ return false unless node.assignment?
180
+
181
+ value = node.call_type? ? node.last_argument : node.expression
182
+ return true if value.array_type? && !value.bracketed?
183
+
184
+ value = value.body if value.rescue_type?
185
+
186
+ command_value?(value)
187
+ end
188
+
189
+ def command_value?(node)
190
+ node = node.send_node if node.any_block_type?
191
+ return true if command_assignment?(node)
192
+ return false if node.defined_type?
193
+ return true if keyword_command?(node)
194
+ return false if binary_operator_call?(node)
195
+
196
+ require_argument_parentheses?(node)
197
+ end
198
+
199
+ def keyword_command?(node)
200
+ node.type?(:yield, :super, :defined?) && node.arguments.any? && !node.parenthesized?
201
+ end
202
+
203
+ def binary_operator_call?(node)
204
+ node.call_type? && node.binary_operation? && !node.loc.dot
205
+ end
206
+
177
207
  def require_argument_parentheses?(node)
178
- return false if !node.call_type? || node.arithmetic_operation?
208
+ return false if !node.call_type? || node.arithmetic_operation? || node.setter_method?
179
209
 
180
210
  !node.parenthesized? && node.arguments.any? && !node.method?(:[]) && !node.method?(:[]=)
181
211
  end
@@ -86,7 +86,7 @@ module RuboCop
86
86
  find_block_variables(node, block_argument_name).each do |variable|
87
87
  add_offense(variable, message: MSG_USE_IT_PARAMETER) do |corrector|
88
88
  corrector.remove(node.arguments)
89
- corrector.replace(variable, 'it')
89
+ replace_with_it(corrector, variable)
90
90
  end
91
91
  end
92
92
  end
@@ -131,6 +131,16 @@ module RuboCop
131
131
  end
132
132
  end
133
133
 
134
+ def replace_with_it(corrector, variable)
135
+ parent = variable.parent
136
+
137
+ if parent.pair_type? && parent.value_omission?
138
+ corrector.replace(parent.loc.operator, ': it')
139
+ else
140
+ corrector.replace(variable, 'it')
141
+ end
142
+ end
143
+
134
144
  def it_variable_in_scope?(node)
135
145
  scope = nearest_scope(node)
136
146
  return true if it_argument_in_scope?(node, scope)
@@ -76,11 +76,9 @@ module RuboCop
76
76
  return if uncorrectable_lambda_literal?(node)
77
77
 
78
78
  add_offense(node.send_node, message: message(node, selector)) do |corrector|
79
- if node.send_node.lambda_literal?
80
- LambdaLiteralToMethodCorrector.new(node).call(corrector)
81
- else
82
- autocorrect_method_to_literal(corrector, node)
83
- end
79
+ next if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
80
+
81
+ autocorrect(corrector, node)
84
82
  end
85
83
  end
86
84
  alias on_numblock on_block
@@ -124,6 +122,16 @@ module RuboCop
124
122
  end
125
123
  end
126
124
 
125
+ def autocorrect(corrector, node)
126
+ if node.send_node.lambda_literal?
127
+ LambdaLiteralToMethodCorrector.new(node).call(corrector)
128
+ else
129
+ autocorrect_method_to_literal(corrector, node)
130
+ end
131
+
132
+ ignore_node(node.arguments) if node.arguments?
133
+ end
134
+
127
135
  def autocorrect_method_to_literal(corrector, node)
128
136
  corrector.replace(node.send_node, '->')
129
137
 
@@ -63,6 +63,10 @@ module RuboCop
63
63
 
64
64
  MSG = 'Use `%<new_method_name>s` instead of `each` to map elements into an array.'
65
65
 
66
+ def self.autocorrect_incompatible_with
67
+ [Style::TrailingCommaInArguments]
68
+ end
69
+
66
70
  # @!method suitable_argument_node?(node)
67
71
  def_node_matcher :suitable_argument_node?, <<-PATTERN
68
72
  !{splat forwarded-restarg forwarded-args (hash (forwarded-kwrestarg)) block-pass}
@@ -50,10 +50,6 @@ module RuboCop
50
50
  (args $(mlhs (arg _)+))
51
51
  PATTERN
52
52
 
53
- def self.autocorrect_incompatible_with
54
- [Layout::SingleLineBlockChain]
55
- end
56
-
57
53
  def on_send(node)
58
54
  return unless (to_h_node, map_node = map_to_h(node))
59
55
  return if to_h_node.block_literal?
@@ -298,9 +298,8 @@ module RuboCop
298
298
  parent.assignment? && (grandparent.conditional? || grandparent.when_type?)
299
299
  end
300
300
 
301
- # rubocop:disable-next Metrics/PerceivedComplexity
302
301
  def command_assignment_in_expression?(node)
303
- value = node.parent&.type?(:any_block, :rescue) ? node.parent : node
302
+ value = assigned_value(node)
304
303
  assignment = outermost_assignment(value)
305
304
  return false if assignment.equal?(value)
306
305
 
@@ -308,9 +307,18 @@ module RuboCop
308
307
  call_in_logical_operators?(assignment) ||
309
308
  call_in_optional_arguments?(assignment) ||
310
309
  call_as_argument_or_chain?(assignment) ||
310
+ call_in_match_pattern?(assignment) ||
311
311
  assignment.parent&.type?(:masgn, :return, :next, :break, :defined?)
312
312
  end
313
313
 
314
+ def assigned_value(node)
315
+ parent = node.parent
316
+ return parent if parent&.rescue_type?
317
+ return parent if parent&.any_block_type? && parent.send_node.equal?(node)
318
+
319
+ node
320
+ end
321
+
314
322
  def outermost_assignment(value)
315
323
  parent = value.parent
316
324
  return value if !parent&.assignment? || parent.masgn_type?
@@ -29,9 +29,47 @@ module RuboCop
29
29
  end
30
30
 
31
31
  def eligible_for_parentheses_omission?(node)
32
+ return true if do_end_block_in_command_arguments?(node)
33
+
32
34
  node.operator_method? || node.setter_method? || ignored_macro?(node)
33
35
  end
34
36
 
37
+ def do_end_block_in_command_arguments?(node)
38
+ return false unless node.block_node&.keywords?
39
+
40
+ child = node.block_node
41
+ child.each_ancestor do |ancestor|
42
+ return !ancestor.parenthesized? if command_argument?(ancestor, child)
43
+ return false if command_arguments_boundary?(ancestor, child)
44
+
45
+ child = ancestor
46
+ end
47
+
48
+ false
49
+ end
50
+
51
+ def command_argument?(node, child)
52
+ return false unless node.type?(:call, :super, :yield)
53
+ return false if node.operator_method? || node.setter_method?
54
+
55
+ node.arguments.any? { |argument| argument.equal?(child) }
56
+ end
57
+
58
+ def command_arguments_boundary?(node, child)
59
+ return true if node.type?(:any_block, :any_def, :class, :module, :sclass, :kwbegin)
60
+ return true if node.type?(:break, :next, :return) || index_argument?(node, child)
61
+
62
+ node.type?(:begin, :array, :hash) && node.loc?(:begin)
63
+ end
64
+
65
+ def index_argument?(node, child)
66
+ return false unless node.call_type?
67
+ return node.arguments.any? { |argument| argument.equal?(child) } if node.method?(:[])
68
+ return false unless node.method?(:[]=)
69
+
70
+ node.arguments[0...-1].any? { |argument| argument.equal?(child) }
71
+ end
72
+
35
73
  def included_macros_list
36
74
  @included_macros_list ||= cop_config.fetch('IncludedMacros', []).map(&:to_sym).freeze
37
75
  end
@@ -245,6 +245,7 @@ module RuboCop
245
245
  [
246
246
  Layout::HeredocArgumentClosingParenthesis,
247
247
  Style::NestedParenthesizedCalls,
248
+ Style::ParenthesesAroundCondition,
248
249
  Style::RedundantParentheses,
249
250
  Style::RescueModifier,
250
251
  Style::TernaryParentheses,
@@ -278,7 +279,8 @@ module RuboCop
278
279
  first_node = node.first_argument
279
280
  return false unless first_node.begin_type? && first_node.parenthesized_call?
280
281
 
281
- first_node.children.one? && mergeable_parentheses?(first_node.children.first)
282
+ first_node.children.one? && mergeable_parentheses?(first_node.children.first) &&
283
+ !semicolon_around_grouped_expression?(first_node)
282
284
  end
283
285
 
284
286
  def mergeable_parentheses?(node)
@@ -295,6 +297,14 @@ module RuboCop
295
297
  true
296
298
  end
297
299
  end
300
+
301
+ def semicolon_around_grouped_expression?(node)
302
+ expression = node.children.first.source_range
303
+
304
+ processed_source.tokens_within(node).any? do |token|
305
+ token.semicolon? && !expression.contains?(token.pos)
306
+ end
307
+ end
298
308
  end
299
309
  end
300
310
  end
@@ -102,10 +102,6 @@ module RuboCop
102
102
  MSG_PRESENT = 'Use def without parentheses.'
103
103
  MSG_MISSING = 'Use def with parentheses when there are parameters.'
104
104
 
105
- def self.autocorrect_incompatible_with
106
- [Style::ArgumentsForwarding]
107
- end
108
-
109
105
  def on_def(node)
110
106
  args = node.arguments
111
107
 
@@ -43,12 +43,9 @@ module RuboCop
43
43
  def autocorrect(corrector, node, begin_of_arguments)
44
44
  arguments = node.arguments
45
45
  joined_arguments = arguments.map(&:source).join(', ')
46
- last_line_source_of_arguments = last_line_source_of_arguments(arguments)
47
46
 
48
- if last_line_source_of_arguments.start_with?(')')
49
- joined_arguments = "#{joined_arguments}#{last_line_source_of_arguments}"
50
-
51
- corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
47
+ if last_line_source_of_arguments(arguments).start_with?(')')
48
+ remove_line_break_before_closing_parenthesis(corrector, arguments)
52
49
  end
53
50
 
54
51
  arguments_range = range_with_surrounding_space(arguments_range(node), side: :left)
@@ -68,6 +65,19 @@ module RuboCop
68
65
  processed_source[arguments.last_line - 1].strip
69
66
  end
70
67
 
68
+ def remove_line_break_before_closing_parenthesis(corrector, arguments)
69
+ closing_parenthesis = arguments.loc.end
70
+ closing_line = processed_source.buffer.line_range(closing_parenthesis.line)
71
+ end_of_last_argument = arguments.last.source_range.end
72
+
73
+ corrector.remove(end_of_last_argument.join(closing_parenthesis.begin))
74
+ corrector.insert_after(closing_line.end, text_between(end_of_last_argument, closing_line))
75
+ end
76
+
77
+ def text_between(range, line_range)
78
+ range.join(line_range.begin).source.delete_suffix("\n")
79
+ end
80
+
71
81
  def opening_line(node)
72
82
  node.first_line
73
83
  end