rubocop 1.84.2 → 1.86.2

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 (210) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +99 -16
  3. data/config/obsoletion.yml +5 -0
  4. data/lib/rubocop/cache_config.rb +1 -1
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +28 -2
  6. data/lib/rubocop/cli/command/list_enabled_cops_for.rb +40 -0
  7. data/lib/rubocop/cli/command/mcp.rb +19 -0
  8. data/lib/rubocop/cli/command/show_cops.rb +2 -2
  9. data/lib/rubocop/cli/command/show_docs_url.rb +4 -8
  10. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  11. data/lib/rubocop/cli.rb +7 -7
  12. data/lib/rubocop/comment_config.rb +12 -15
  13. data/lib/rubocop/config.rb +14 -10
  14. data/lib/rubocop/config_finder.rb +1 -1
  15. data/lib/rubocop/config_loader_resolver.rb +2 -1
  16. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  17. data/lib/rubocop/config_store.rb +1 -1
  18. data/lib/rubocop/config_validator.rb +1 -1
  19. data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
  20. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  22. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  23. data/lib/rubocop/cop/correctors.rb +28 -0
  24. data/lib/rubocop/cop/documentation.rb +2 -3
  25. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  26. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -4
  27. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  28. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  30. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  31. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  32. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -2
  34. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  35. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  36. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  37. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  38. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  39. data/lib/rubocop/cop/layout/end_alignment.rb +6 -3
  40. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  42. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  43. data/lib/rubocop/cop/layout/line_length.rb +5 -3
  44. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  45. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  46. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +53 -3
  47. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  48. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  54. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  55. data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
  56. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  57. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  58. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  59. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  61. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  62. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
  63. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  64. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  65. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
  66. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  67. data/lib/rubocop/cop/lint/require_relative_self_path.rb +2 -0
  68. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  69. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  70. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  71. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  72. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  73. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  74. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  75. data/lib/rubocop/cop/lint/useless_assignment.rb +4 -9
  76. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  77. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  78. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +35 -9
  79. data/lib/rubocop/cop/lint/void.rb +32 -12
  80. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  81. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  82. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  83. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  84. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  85. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  86. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  87. data/lib/rubocop/cop/mixin.rb +85 -0
  88. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  89. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  90. data/lib/rubocop/cop/offense.rb +8 -0
  91. data/lib/rubocop/cop/registry.rb +39 -37
  92. data/lib/rubocop/cop/security/eval.rb +15 -2
  93. data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
  94. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  95. data/lib/rubocop/cop/style/alias.rb +4 -1
  96. data/lib/rubocop/cop/style/and_or.rb +1 -0
  97. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  98. data/lib/rubocop/cop/style/array_join.rb +4 -2
  99. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  100. data/lib/rubocop/cop/style/attr.rb +5 -2
  101. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  102. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  103. data/lib/rubocop/cop/style/block_delimiters.rb +25 -33
  104. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  105. data/lib/rubocop/cop/style/class_and_module_children.rb +10 -2
  106. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  107. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  108. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  109. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
  110. data/lib/rubocop/cop/style/copyright.rb +22 -11
  111. data/lib/rubocop/cop/style/date_time.rb +2 -2
  112. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  113. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  114. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  115. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  116. data/lib/rubocop/cop/style/empty_class_definition.rb +43 -20
  117. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  118. data/lib/rubocop/cop/style/encoding.rb +7 -1
  119. data/lib/rubocop/cop/style/end_block.rb +3 -1
  120. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  121. data/lib/rubocop/cop/style/file_open.rb +84 -0
  122. data/lib/rubocop/cop/style/for.rb +3 -0
  123. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  124. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  125. data/lib/rubocop/cop/style/guard_clause.rb +9 -6
  126. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  127. data/lib/rubocop/cop/style/hash_lookup_method.rb +19 -7
  128. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  129. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  130. data/lib/rubocop/cop/style/if_inside_else.rb +16 -7
  131. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -3
  132. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  133. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  134. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  135. data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
  136. data/lib/rubocop/cop/style/map_join.rb +123 -0
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
  138. data/lib/rubocop/cop/style/module_member_existence_check.rb +7 -14
  139. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  140. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  141. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  142. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  143. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  144. data/lib/rubocop/cop/style/not.rb +2 -0
  145. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  146. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  147. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  148. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  149. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  150. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  151. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  152. data/lib/rubocop/cop/style/proc.rb +3 -2
  153. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  154. data/lib/rubocop/cop/style/reduce_to_hash.rb +200 -0
  155. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  156. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  157. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  159. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  160. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  161. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -22
  162. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
  163. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  164. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  165. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  166. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  167. data/lib/rubocop/cop/style/regexp_literal.rb +29 -0
  168. data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
  169. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  170. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  171. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  172. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  173. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  174. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  175. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  176. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  177. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  178. data/lib/rubocop/cop/style/symbol_proc.rb +7 -6
  179. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  180. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  181. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  182. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  183. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  184. data/lib/rubocop/cop/team.rb +86 -35
  185. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  186. data/lib/rubocop/directive_comment.rb +2 -1
  187. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -2
  188. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  189. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  190. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  191. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  192. data/lib/rubocop/formatter.rb +22 -21
  193. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  194. data/lib/rubocop/lsp/routes.rb +10 -3
  195. data/lib/rubocop/lsp/runtime.rb +1 -2
  196. data/lib/rubocop/mcp/server.rb +200 -0
  197. data/lib/rubocop/options.rb +17 -4
  198. data/lib/rubocop/path_util.rb +14 -2
  199. data/lib/rubocop/plugin/loader.rb +1 -1
  200. data/lib/rubocop/result_cache.rb +22 -10
  201. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  202. data/lib/rubocop/rspec/shared_contexts.rb +32 -2
  203. data/lib/rubocop/runner.rb +78 -51
  204. data/lib/rubocop/server/cache.rb +5 -7
  205. data/lib/rubocop/server/core.rb +2 -0
  206. data/lib/rubocop/target_finder.rb +14 -7
  207. data/lib/rubocop/target_ruby.rb +18 -12
  208. data/lib/rubocop/version.rb +2 -2
  209. data/lib/rubocop.rb +21 -96
  210. metadata +25 -5
@@ -38,6 +38,8 @@ module RuboCop
38
38
  private
39
39
 
40
40
  def same_file?(file_path, required_feature)
41
+ return false unless File.extname(file_path) == '.rb'
42
+
41
43
  file_path == required_feature || remove_ext(file_path) == required_feature
42
44
  end
43
45
 
@@ -37,20 +37,25 @@ module RuboCop
37
37
  }
38
38
  PATTERN
39
39
 
40
+ # rubocop:disable Metrics/AbcSize
40
41
  def on_send(node)
41
42
  return unless require_safe_navigation?(node)
42
43
 
43
44
  bad_method?(node) do |safe_nav, method|
44
45
  return if nil_methods.include?(method) || PLUS_MINUS_METHODS.include?(node.method_name)
46
+ return if ternary_safe_navigation?(node, safe_nav)
45
47
 
46
48
  begin_range = node.loc.dot || safe_nav.source_range.end
47
49
  location = begin_range.join(node.source_range.end)
48
50
 
49
51
  add_offense(location) do |corrector|
52
+ next if ternary_else_branch?(node, safe_nav)
53
+
50
54
  autocorrect(corrector, offense_range: location, send_node: node)
51
55
  end
52
56
  end
53
57
  end
58
+ # rubocop:enable Metrics/AbcSize
54
59
 
55
60
  private
56
61
 
@@ -61,6 +66,18 @@ module RuboCop
61
66
  parent.rhs != node || parent.lhs.receiver != parent.rhs.receiver
62
67
  end
63
68
 
69
+ def ternary_safe_navigation?(node, safe_nav)
70
+ return false unless (parent = node.parent)
71
+
72
+ parent.if_type? && node.equal?(parent.if_branch) && parent.condition == safe_nav
73
+ end
74
+
75
+ def ternary_else_branch?(node, safe_nav)
76
+ return false unless (parent = node.parent)
77
+
78
+ parent.if_type? && node.equal?(parent.else_branch) && parent.condition == safe_nav
79
+ end
80
+
64
81
  # @param [Parser::Source::Range] offense_range
65
82
  # @param [RuboCop::AST::SendNode] send_node
66
83
  # @return [String]
@@ -3,10 +3,16 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # Check to make sure that if safe navigation is used in an `&&` or `||` condition,
6
+ # Checks that if safe navigation is used in an `&&` or `||` condition,
7
7
  # consistent and appropriate safe navigation, without excess or deficiency,
8
8
  # is used for all method calls on the same object.
9
9
  #
10
+ # @safety
11
+ # Autocorrection is unsafe because if the receiver is not a local variable
12
+ # but a method call, it may not be idempotent. For example, replacing
13
+ # `foo&.bar` with `foo.bar` could raise `NoMethodError` if `foo` returns
14
+ # `nil` on a subsequent call.
15
+ #
10
16
  # @example
11
17
  # # bad
12
18
  # foo&.bar && foo&.baz
@@ -26,7 +26,31 @@ module RuboCop
26
26
  "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
27
27
  'configure using `TargetRubyVersion` parameter, under `AllCops`)'
28
28
  end
29
- add_offense(diagnostic.location, message: message, severity: diagnostic.level)
29
+ location = diagnostic_location(diagnostic.location)
30
+ add_offense(location, message: message, severity: diagnostic.level)
31
+ end
32
+
33
+ # Expand zero-length diagnostic ranges so that editors and formatters
34
+ # can display them. This typically occurs when the parser reports
35
+ # `unexpected token $end` at EOF.
36
+ def diagnostic_location(location)
37
+ return location if location.size.positive?
38
+
39
+ source_buffer = location.source_buffer
40
+ if location.end_pos < source_buffer.source.size
41
+ location.resize(1)
42
+ elsif location.begin_pos.positive?
43
+ location.adjust(begin_pos: -1)
44
+ else
45
+ location
46
+ end
47
+ end
48
+
49
+ # Override to skip multiline_ranges check which requires AST.
50
+ # Syntax errors mean the AST is nil, so we go directly to
51
+ # the EOL comment insertion path.
52
+ def disable_offense(offense_range)
53
+ disable_offense_with_eol_or_surround_comment(offense_range)
30
54
  end
31
55
 
32
56
  def add_offense_from_error(error)
@@ -32,6 +32,7 @@ module RuboCop
32
32
  include RangeHelp
33
33
 
34
34
  MSG = 'Avoid leaving a trailing comma in attribute declarations.'
35
+ RESTRICT_ON_SEND = %i[attr_reader attr_writer attr_accessor attr].freeze
35
36
 
36
37
  def on_send(node)
37
38
  return unless node.attribute_accessor? && node.last_argument.def_type?
@@ -120,6 +120,7 @@ module RuboCop
120
120
  check_return_values(node)
121
121
  end
122
122
  alias on_numblock on_block
123
+ alias on_itblock on_block
123
124
 
124
125
  private
125
126
 
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for unreachable `in` pattern branches in `case...in` statements.
7
+ #
8
+ # An `in` branch is unreachable when a previous branch uses an unguarded
9
+ # catch-all pattern that matches any value unconditionally. Any `in` branches
10
+ # (and `else`) that follow such a catch-all are dead code.
11
+ #
12
+ # A catch-all pattern is one of:
13
+ #
14
+ # * A bare variable capture (`in x`)
15
+ # * An underscore (`in _`)
16
+ # * A pattern alias where the left side is a catch-all (`in _ => y`)
17
+ # * An alternation pattern where at least one alternative is a catch-all
18
+ # (`in _ | Integer`)
19
+ #
20
+ # NOTE: A catch-all pattern with a guard clause (e.g., `in _ if condition`)
21
+ # does NOT make subsequent branches unreachable because the guard might
22
+ # not be satisfied.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # case value
28
+ # in Integer
29
+ # handle_integer
30
+ # in x
31
+ # handle_other
32
+ # in String
33
+ # handle_string
34
+ # else
35
+ # handle_else
36
+ # end
37
+ #
38
+ # # good
39
+ # case value
40
+ # in Integer
41
+ # handle_integer
42
+ # in String
43
+ # handle_string
44
+ # in x
45
+ # handle_other
46
+ # end
47
+ #
48
+ # # bad - else is unreachable after catch-all
49
+ # case value
50
+ # in Integer
51
+ # handle_integer
52
+ # in _
53
+ # handle_other
54
+ # else
55
+ # handle_else
56
+ # end
57
+ #
58
+ # # good - guard clause means catch-all might not match
59
+ # case value
60
+ # in x if x.positive?
61
+ # handle_positive
62
+ # in Integer
63
+ # handle_integer
64
+ # else
65
+ # handle_other
66
+ # end
67
+ #
68
+ class UnreachablePatternBranch < Base
69
+ extend TargetRubyVersion
70
+
71
+ MSG = 'Unreachable `in` pattern branch detected.'
72
+ MSG_ELSE = 'Unreachable `else` branch detected.'
73
+
74
+ minimum_target_ruby_version 2.7
75
+
76
+ def on_case_match(case_node)
77
+ catch_all_found = false
78
+
79
+ case_node.in_pattern_branches.each do |in_pattern_node|
80
+ if catch_all_found
81
+ add_offense(in_pattern_node)
82
+ next
83
+ end
84
+
85
+ pattern = in_pattern_node.pattern
86
+ guard = in_pattern_node.children[1]
87
+
88
+ catch_all_found = true if catch_all_pattern?(pattern) && guard.nil?
89
+ end
90
+
91
+ return unless catch_all_found && case_node.else?
92
+
93
+ add_offense(case_node.loc.else, message: MSG_ELSE)
94
+ end
95
+
96
+ private
97
+
98
+ def catch_all_pattern?(pattern)
99
+ case pattern.type
100
+ when :match_var
101
+ true
102
+ when :match_as, :begin
103
+ catch_all_pattern?(pattern.children[0])
104
+ when :match_alt
105
+ pattern.children.any? { |child| catch_all_pattern?(child) }
106
+ else
107
+ false
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -95,10 +95,20 @@ module RuboCop
95
95
  return unless variable.method_argument?
96
96
  return if variable.keyword_argument? && cop_config['AllowUnusedKeywordArguments']
97
97
  return if ignored_method?(variable.scope.node.body)
98
+ return if block_argument_with_yield?(variable)
98
99
 
99
100
  super
100
101
  end
101
102
 
103
+ def block_argument_with_yield?(variable)
104
+ return false unless variable.declaration_node.blockarg_type?
105
+
106
+ method_body = variable.scope.node.body
107
+ return false unless method_body
108
+
109
+ method_body.yield_type? || method_body.each_descendant(:yield).any?
110
+ end
111
+
102
112
  def ignored_method?(body)
103
113
  (cop_config['IgnoreEmptyMethods'] && body.nil?) ||
104
114
  (cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
@@ -40,8 +40,6 @@ module RuboCop
40
40
  class UselessAssignment < Base
41
41
  extend AutoCorrector
42
42
 
43
- include RangeHelp
44
-
45
43
  MSG = 'Useless assignment to variable - `%<variable>s`.'
46
44
 
47
45
  def self.joining_forces
@@ -189,12 +187,9 @@ module RuboCop
189
187
  # rubocop:enable Metrics/AbcSize
190
188
 
191
189
  def remove_exception_assignment_part(corrector, node)
192
- corrector.remove(
193
- range_between(
194
- (node.parent.children.first&.source_range || node.parent.location.keyword).end_pos,
195
- node.source_range.end_pos
196
- )
197
- )
190
+ range = node.parent.children.first&.source_range || node.parent.location.keyword
191
+
192
+ corrector.remove(range.end.join(node.source_range.end))
198
193
  end
199
194
 
200
195
  def rename_variable_with_underscore(corrector, node)
@@ -213,7 +208,7 @@ module RuboCop
213
208
  end
214
209
 
215
210
  def remove_local_variable_assignment_part(corrector, node)
216
- corrector.replace(node, node.expression.source)
211
+ corrector.remove(node.loc.name.begin.join(node.expression.source_range.begin))
217
212
  end
218
213
 
219
214
  def variable_in_loop_condition?(assignment_node, variable)
@@ -48,12 +48,12 @@ module RuboCop
48
48
 
49
49
  def after_private_modifier?(left_siblings)
50
50
  access_modifier_candidates = left_siblings.compact.select do |left_sibling|
51
- left_sibling.respond_to?(:send_type?) && left_sibling.send_type?
51
+ left_sibling.respond_to?(:bare_access_modifier?) && left_sibling.bare_access_modifier?
52
52
  end
53
53
 
54
- access_modifier_candidates.any? do |candidate|
55
- candidate.command?(:private) && candidate.arguments.none?
56
- end
54
+ return false if access_modifier_candidates.empty?
55
+
56
+ access_modifier_candidates.last.command?(:private)
57
57
  end
58
58
 
59
59
  def private_constantize?(right_siblings, const_value)
@@ -67,6 +67,8 @@ module RuboCop
67
67
  PATTERN
68
68
 
69
69
  def on_send(node)
70
+ return unless node.receiver
71
+
70
72
  unless (prev_arg_node, default_value_node = default_value_argument_and_block(node.parent))
71
73
  return
72
74
  end
@@ -60,9 +60,7 @@ module RuboCop
60
60
  return true if _cant_be_nil?(node.expression, receiver)
61
61
  end
62
62
 
63
- # Due to how `if/else` are implemented (`elsif` is a child of `if` or another `elsif`),
64
- # using left_siblings will not work correctly for them.
65
- if !else_branch?(node) || (node.if_type? && !node.elsif?)
63
+ if sequentially_reached?(node)
66
64
  node.left_siblings.reverse_each do |sibling|
67
65
  next unless sibling.is_a?(AST::Node)
68
66
 
@@ -82,28 +80,48 @@ module RuboCop
82
80
  !NIL_METHODS.include?(method_name) && !@additional_nil_methods.include?(method_name)
83
81
  end
84
82
 
85
- # rubocop:disable Metrics/PerceivedComplexity
83
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
86
84
  def sole_condition_of_parent_if?(node)
85
+ child = node
87
86
  parent = node.parent
88
87
 
89
88
  while parent
90
89
  if parent.if_type?
91
- if parent.condition == node
92
- return true
93
- elsif parent.elsif?
94
- parent = find_top_if(parent)
90
+ unless parent.unless?
91
+ condition = parent.condition
92
+ return true if !child.equal?(condition) && non_nil_condition?(condition, node)
95
93
  end
94
+
95
+ parent = find_top_if(parent) if parent.elsif?
96
96
  elsif else_branch?(parent)
97
97
  # Find the top `if` for `else`.
98
98
  parent = parent.parent
99
99
  end
100
100
 
101
+ child = parent
101
102
  parent = parent&.parent
102
103
  end
103
104
 
104
105
  false
105
106
  end
106
- # rubocop:enable Metrics/PerceivedComplexity
107
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
108
+
109
+ def non_nil_condition?(condition, node)
110
+ return true if condition == node
111
+
112
+ condition.csend_type? && csend_root_receiver(condition) == node
113
+ end
114
+
115
+ # Whether control reaches `node` by falling through its left siblings rather than by
116
+ # a non-sequential entry. A `resbody` is entered via an exception, the `ensure` branch
117
+ # runs even after a partway raise, and the `else` arm of an `if/elsif` chain is reached by
118
+ # branching (its `if/case` siblings are walked via parent recursion instead).
119
+ def sequentially_reached?(node)
120
+ return false if node.resbody_type?
121
+ return false if node.parent&.ensure_type? && node.parent.branch.equal?(node)
122
+
123
+ !else_branch?(node) || (node.if_type? && !node.elsif?)
124
+ end
107
125
 
108
126
  def else_branch?(node)
109
127
  node.parent&.if_type? && node.parent.else_branch == node
@@ -114,6 +132,14 @@ module RuboCop
114
132
 
115
133
  node
116
134
  end
135
+
136
+ def csend_root_receiver(node)
137
+ return unless (receiver = node.receiver)
138
+
139
+ receiver = receiver.receiver while receiver.call_type? && receiver.receiver
140
+
141
+ receiver
142
+ end
117
143
  end
118
144
  end
119
145
  end
@@ -87,8 +87,9 @@ module RuboCop
87
87
  def on_block(node)
88
88
  return unless node.body && !node.body.begin_type?
89
89
  return unless in_void_context?(node.body)
90
+ return if node.method?(:each)
90
91
 
91
- check_void_op(node.body) { node.method?(:each) }
92
+ check_void_op(node.body)
92
93
  check_expression(node.body)
93
94
  end
94
95
  alias on_numblock on_block
@@ -107,22 +108,23 @@ module RuboCop
107
108
 
108
109
  def check_begin(node)
109
110
  expressions = *node
110
- expressions.pop unless in_void_context?(node)
111
+ inside_each_block = node.each_ancestor(:any_block).first&.method?(:each)
112
+ expressions.pop if !in_void_context?(node) || inside_each_block
111
113
  expressions.each do |expr|
112
- check_void_op(expr) do
113
- block_node = node.each_ancestor(:any_block).first
114
-
115
- block_node&.method?(:each)
116
- end
117
-
114
+ check_void_op(expr) { inside_each_block }
118
115
  check_expression(expr)
119
116
  end
120
117
  end
121
118
 
122
119
  def check_expression(expr)
123
- expr = expr.body if expr.if_type?
124
- return unless expr
120
+ return check_if_expression(expr) if expr.if_type?
121
+ return check_case_expression(expr) if expr.case_type?
122
+ return check_case_match_expression(expr) if expr.case_match_type?
123
+
124
+ check_void_expression_nodes(expr)
125
+ end
125
126
 
127
+ def check_void_expression_nodes(expr)
126
128
  check_literal(expr)
127
129
  check_var(expr)
128
130
  check_self(expr)
@@ -132,6 +134,22 @@ module RuboCop
132
134
  check_nonmutating(expr)
133
135
  end
134
136
 
137
+ def check_if_expression(if_node)
138
+ check_void_expression_nodes(if_node.body) if if_node.body
139
+ end
140
+
141
+ def check_case_expression(case_node)
142
+ case_node.each_when { |when_node| check_expression(when_node.body) if when_node.body }
143
+ check_expression(case_node.else_branch) if case_node.else_branch
144
+ end
145
+
146
+ def check_case_match_expression(case_node)
147
+ case_node.each_in_pattern do |in_pattern_node|
148
+ check_expression(in_pattern_node.body) if in_pattern_node.body
149
+ end
150
+ check_expression(case_node.else_branch) if case_node.else_branch
151
+ end
152
+
135
153
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
136
154
  def check_void_op(node, &block)
137
155
  node = node.children.first while node&.begin_type?
@@ -168,7 +186,9 @@ module RuboCop
168
186
  end
169
187
 
170
188
  def check_literal(node)
171
- return if !entirely_literal?(node) || node.xstr_type? || node.range_type?
189
+ if !entirely_literal?(node) || node.xstr_type? || node.range_type? || node.nil_type?
190
+ return
191
+ end
172
192
 
173
193
  add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
174
194
  autocorrect_void_expression(corrector, node)
@@ -238,7 +258,7 @@ module RuboCop
238
258
  end
239
259
 
240
260
  def autocorrect_void_expression(corrector, node)
241
- return if node.parent.if_type?
261
+ return if node.parent.type?(:if, :case, :when, :case_match, :in_pattern)
242
262
  return if (def_node = node.each_ancestor(:any_def).first) && def_node.assignment_method?
243
263
 
244
264
  corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
6
  # Checks for excessive nesting of conditional and looping constructs.
7
+ # Deeply nested code is harder to read, understand, and maintain.
8
+ # Extracting nested logic into methods improves clarity.
7
9
  #
8
10
  # You can configure if blocks are considered using the `CountBlocks` and `CountModifierForms`
9
11
  # options. When both are set to `false` (the default) blocks and modifier forms are not
@@ -11,6 +13,27 @@ module RuboCop
11
13
  # calculation as well.
12
14
  #
13
15
  # The maximum level of nesting allowed is configurable.
16
+ #
17
+ # @example Max: 3 (default)
18
+ # # bad
19
+ # if condition1
20
+ # if condition2
21
+ # if condition3
22
+ # if condition4
23
+ # do_something
24
+ # end
25
+ # end
26
+ # end
27
+ # end
28
+ #
29
+ # # good
30
+ # if condition1
31
+ # if condition2
32
+ # if condition3
33
+ # do_something
34
+ # end
35
+ # end
36
+ # end
14
37
  class BlockNesting < Base
15
38
  NESTING_BLOCKS = %i[case case_match if while while_post until until_post for resbody].freeze
16
39
 
@@ -1,10 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Lint/RedundantCopDisableDirective needs to be disabled so as
4
+ # to be able to provide examples of rubocop:disable comments.
5
+ # rubocop:disable Lint/RedundantCopDisableDirective
3
6
  module RuboCop
4
7
  module Cop
5
8
  module Migration
6
- # Check that cop names in rubocop:disable comments are given with
9
+ # Checks that cop names in rubocop:disable comments are given with
7
10
  # department name.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # # rubocop:disable AbcSize
15
+ #
16
+ # # good
17
+ # # rubocop:disable Metrics/AbcSize
8
18
  class DepartmentName < Base
9
19
  include RangeHelp
10
20
  extend AutoCorrector
@@ -79,3 +89,4 @@ module RuboCop
79
89
  end
80
90
  end
81
91
  end
92
+ # rubocop:enable Lint/RedundantCopDisableDirective
@@ -222,7 +222,7 @@ module RuboCop
222
222
  def already_on_multiple_lines?(node)
223
223
  return node.first_line != node.last_argument.last_line if node.any_def_type?
224
224
 
225
- !node.single_line?
225
+ node.multiline?
226
226
  end
227
227
 
228
228
  def chained_to_heredoc?(node)
@@ -38,9 +38,9 @@ module RuboCop
38
38
  end
39
39
 
40
40
  def safe_to_split?(node)
41
- node.each_descendant(:if, :case, :kwbegin, :any_def).none? &&
41
+ node.each_descendant(:if, :case, :kwbegin, :any_def, :rescue, :ensure).none? &&
42
42
  node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
43
- node.each_descendant(:begin, :sym).none? { |b| !b.single_line? }
43
+ node.each_descendant(:begin, :sym).none?(&:multiline?)
44
44
  end
45
45
  end
46
46
  end
@@ -13,11 +13,12 @@ module RuboCop
13
13
  `max=` is deprecated. Use `exclude_limit <ParameterName>` instead.
14
14
  WARNING
15
15
 
16
- cfg = config_to_allow_offenses
17
- cfg[:exclude_limit] ||= {}
18
- current_max = cfg[:exclude_limit][max_parameter_name]
19
- value = [current_max, value].max if current_max
20
- cfg[:exclude_limit][max_parameter_name] = value
16
+ cop_dir = RuboCop::ExcludeLimit.cop_dir_for(self.class.badge.to_s)
17
+ return unless cop_dir
18
+
19
+ cop_dir.mkpath
20
+ filepath = cop_dir.join(max_parameter_name)
21
+ filepath.write("#{value}\n", mode: 'a')
21
22
  end
22
23
 
23
24
  def max_parameter_name
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module HashTransformMethod
6
+ # Internal helper class to hold autocorrect data
7
+ Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
8
+ def self.from_each_with_object(node, match)
9
+ new(match, node, 0, 0)
10
+ end
11
+
12
+ def self.from_hash_brackets_map(node, match)
13
+ new(match, node.children.last, 'Hash['.length, ']'.length)
14
+ end
15
+
16
+ def self.from_map_to_h(node, match)
17
+ if node.parent&.block_type? && node.parent.send_node == node
18
+ strip_trailing_chars = 0
19
+ else
20
+ map_range = node.children.first.source_range
21
+ node_range = node.source_range
22
+ strip_trailing_chars = node_range.end_pos - map_range.end_pos
23
+ end
24
+
25
+ new(match, node.children.first, 0, strip_trailing_chars)
26
+ end
27
+
28
+ def self.from_to_h(node, match)
29
+ new(match, node, 0, 0)
30
+ end
31
+
32
+ def strip_prefix_and_suffix(node, corrector)
33
+ expression = node.source_range
34
+ corrector.remove_leading(expression, leading)
35
+ corrector.remove_trailing(expression, trailing)
36
+ end
37
+
38
+ def set_new_method_name(new_method_name, corrector)
39
+ range = block_node.send_node.loc.selector
40
+ if (send_end = block_node.send_node.loc.end)
41
+ # If there are arguments (only true in the `each_with_object`
42
+ # case)
43
+ range = range.begin.join(send_end)
44
+ end
45
+ corrector.replace(range, new_method_name)
46
+ end
47
+
48
+ def set_new_arg_name(transformed_argname, corrector)
49
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
50
+ end
51
+
52
+ def set_new_body_expression(transforming_body_expr, corrector)
53
+ body_source = transforming_body_expr.source
54
+ if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
55
+ body_source = "{ #{body_source} }"
56
+ end
57
+
58
+ corrector.replace(block_node.body, body_source)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end