rubocop 1.50.2 → 1.57.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (232) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/config/default.yml +110 -14
  4. data/config/obsoletion.yml +5 -0
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  6. data/lib/rubocop/cli/command/lsp.rb +19 -0
  7. data/lib/rubocop/cli.rb +4 -1
  8. data/lib/rubocop/config.rb +4 -0
  9. data/lib/rubocop/config_finder.rb +2 -2
  10. data/lib/rubocop/config_loader_resolver.rb +4 -3
  11. data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
  12. data/lib/rubocop/config_obsoletion.rb +2 -2
  13. data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
  14. data/lib/rubocop/cop/base.rb +6 -2
  15. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
  16. data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
  17. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  18. data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
  19. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
  20. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
  22. data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
  23. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  24. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
  25. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  26. data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
  27. data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
  28. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  29. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
  30. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
  31. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  32. data/lib/rubocop/cop/layout/class_structure.rb +7 -0
  33. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  34. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  35. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
  36. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
  37. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
  38. data/lib/rubocop/cop/layout/end_alignment.rb +7 -1
  39. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  40. data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -0
  41. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  42. data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
  43. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  44. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
  45. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  46. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  47. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  48. data/lib/rubocop/cop/layout/redundant_line_break.rb +14 -4
  49. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  50. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  51. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
  52. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
  53. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
  54. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  55. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  56. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
  57. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  58. data/lib/rubocop/cop/lint/debugger.rb +18 -5
  59. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
  60. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
  61. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  62. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
  63. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
  64. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  65. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  66. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  67. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  68. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  69. data/lib/rubocop/cop/lint/missing_super.rb +34 -5
  70. data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
  71. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
  72. data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
  73. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  74. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  75. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  76. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
  77. data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -3
  78. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
  79. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  80. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
  81. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  82. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  83. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
  84. data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
  85. data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
  86. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  87. data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
  88. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  89. data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
  90. data/lib/rubocop/cop/lint/void.rb +78 -10
  91. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  92. data/lib/rubocop/cop/metrics/class_length.rb +8 -3
  93. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  94. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  95. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
  96. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  97. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  98. data/lib/rubocop/cop/mixin/comments_help.rb +19 -11
  99. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  100. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  101. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  102. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  103. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
  104. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  105. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +5 -7
  106. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  107. data/lib/rubocop/cop/mixin/string_help.rb +4 -2
  108. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  109. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  110. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  111. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
  113. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
  114. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
  115. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  116. data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
  117. data/lib/rubocop/cop/style/alias.rb +9 -8
  118. data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -63
  119. data/lib/rubocop/cop/style/array_intersect.rb +13 -5
  120. data/lib/rubocop/cop/style/attr.rb +11 -1
  121. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  122. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  123. data/lib/rubocop/cop/style/block_delimiters.rb +5 -4
  124. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  125. data/lib/rubocop/cop/style/class_equality_comparison.rb +24 -39
  126. data/lib/rubocop/cop/style/collection_compact.rb +16 -6
  127. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  128. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  129. data/lib/rubocop/cop/style/combinable_loops.rb +30 -8
  130. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  131. data/lib/rubocop/cop/style/conditional_assignment.rb +5 -3
  132. data/lib/rubocop/cop/style/copyright.rb +5 -2
  133. data/lib/rubocop/cop/style/dir.rb +1 -1
  134. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  135. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  136. data/lib/rubocop/cop/style/documentation.rb +1 -1
  137. data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
  138. data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
  139. data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
  140. data/lib/rubocop/cop/style/file_read.rb +2 -2
  141. data/lib/rubocop/cop/style/for.rb +1 -1
  142. data/lib/rubocop/cop/style/format_string.rb +24 -3
  143. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
  144. data/lib/rubocop/cop/style/guard_clause.rb +28 -0
  145. data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
  146. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  147. data/lib/rubocop/cop/style/hash_except.rb +19 -8
  148. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  149. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  150. data/lib/rubocop/cop/style/identical_conditional_branches.rb +31 -5
  151. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  152. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
  153. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  154. data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -6
  155. data/lib/rubocop/cop/style/lambda.rb +3 -3
  156. data/lib/rubocop/cop/style/lambda_call.rb +5 -0
  157. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -5
  158. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  159. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  160. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  161. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  162. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  163. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  164. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  165. data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
  166. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  167. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  168. data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
  169. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  170. data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
  171. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -10
  172. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
  173. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  174. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  175. data/lib/rubocop/cop/style/redundant_filter_chain.rb +118 -0
  176. data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -3
  177. data/lib/rubocop/cop/style/redundant_parentheses.rb +42 -16
  178. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
  179. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  180. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  181. data/lib/rubocop/cop/style/redundant_return.rb +7 -2
  182. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
  183. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  184. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
  185. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  186. data/lib/rubocop/cop/style/require_order.rb +11 -5
  187. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  188. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  189. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
  190. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  191. data/lib/rubocop/cop/style/semicolon.rb +12 -4
  192. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  193. data/lib/rubocop/cop/style/single_argument_dig.rb +2 -1
  194. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  195. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  196. data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -2
  197. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  198. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
  199. data/lib/rubocop/cop/style/symbol_array.rb +35 -15
  200. data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
  201. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  202. data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
  203. data/lib/rubocop/cop/team.rb +1 -1
  204. data/lib/rubocop/cop/util.rb +1 -1
  205. data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
  206. data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
  207. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  208. data/lib/rubocop/cop/variable_force.rb +1 -0
  209. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  210. data/lib/rubocop/ext/regexp_parser.rb +4 -1
  211. data/lib/rubocop/file_finder.rb +4 -7
  212. data/lib/rubocop/formatter/html_formatter.rb +4 -2
  213. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  214. data/lib/rubocop/lsp/logger.rb +22 -0
  215. data/lib/rubocop/lsp/routes.rb +246 -0
  216. data/lib/rubocop/lsp/runtime.rb +99 -0
  217. data/lib/rubocop/lsp/server.rb +68 -0
  218. data/lib/rubocop/lsp/severity.rb +27 -0
  219. data/lib/rubocop/magic_comment.rb +12 -10
  220. data/lib/rubocop/options.rb +11 -1
  221. data/lib/rubocop/result_cache.rb +5 -1
  222. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  223. data/lib/rubocop/rspec/shared_contexts.rb +2 -3
  224. data/lib/rubocop/runner.rb +5 -3
  225. data/lib/rubocop/server/cache.rb +1 -0
  226. data/lib/rubocop/server/client_command/exec.rb +3 -2
  227. data/lib/rubocop/string_interpreter.rb +3 -3
  228. data/lib/rubocop/target_finder.rb +7 -3
  229. data/lib/rubocop/target_ruby.rb +12 -7
  230. data/lib/rubocop/version.rb +10 -6
  231. data/lib/rubocop.rb +15 -0
  232. metadata +45 -11
@@ -4,8 +4,12 @@ module RuboCop
4
4
  module Cop
5
5
  module Lint
6
6
  # Checks for redundant safe navigation calls.
7
- # `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
8
- # are checked by default. These are customizable with `AllowedMethods` option.
7
+ # Use cases where a constant, named in camel case for classes and modules is `nil` are rare,
8
+ # and an offense is not detected when the receiver is a snake case constant.
9
+ #
10
+ # For all receivers, the `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`,
11
+ # and `equal?` methods are checked by default.
12
+ # These are customizable with `AllowedMethods` option.
9
13
  #
10
14
  # The `AllowedMethods` option specifies nil-safe methods,
11
15
  # in other words, it is a method that is allowed to skip safe navigation.
@@ -22,6 +26,9 @@ module RuboCop
22
26
  #
23
27
  # @example
24
28
  # # bad
29
+ # CamelCaseConst&.do_something
30
+ #
31
+ # # bad
25
32
  # do_something if attrs&.respond_to?(:[])
26
33
  #
27
34
  # # good
@@ -33,6 +40,9 @@ module RuboCop
33
40
  # end
34
41
  #
35
42
  # # good
43
+ # CamelCaseConst.do_something
44
+ #
45
+ # # good
36
46
  # while node.is_a?(BeginNode)
37
47
  # node = node.parent
38
48
  # end
@@ -57,18 +67,24 @@ module RuboCop
57
67
 
58
68
  NIL_SPECIFIC_METHODS = (nil.methods - Object.new.methods).to_set.freeze
59
69
 
70
+ SNAKE_CASE = /\A[[:digit:][:upper:]_]+\z/.freeze
71
+
60
72
  # @!method respond_to_nil_specific_method?(node)
61
73
  def_node_matcher :respond_to_nil_specific_method?, <<~PATTERN
62
74
  (csend _ :respond_to? (sym %NIL_SPECIFIC_METHODS))
63
75
  PATTERN
64
76
 
77
+ # rubocop:disable Metrics/AbcSize
65
78
  def on_csend(node)
66
- return unless check?(node) && allowed_method?(node.method_name)
67
- return if respond_to_nil_specific_method?(node)
79
+ unless node.receiver.const_type? && !node.receiver.source.match?(SNAKE_CASE)
80
+ return unless check?(node) && allowed_method?(node.method_name)
81
+ return if respond_to_nil_specific_method?(node)
82
+ end
68
83
 
69
84
  range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
70
85
  add_offense(range) { |corrector| corrector.replace(node.loc.dot, '.') }
71
86
  end
87
+ # rubocop:enable Metrics/AbcSize
72
88
 
73
89
  private
74
90
 
@@ -47,7 +47,7 @@ module RuboCop
47
47
  return if node.receiver
48
48
 
49
49
  node.each_child_node(:send) do |child|
50
- next unless child.method?(:to_s)
50
+ next if !child.method?(:to_s) || child.arguments.any?
51
51
 
52
52
  register_offense(child, "`#{node.method_name}`")
53
53
  end
@@ -82,16 +82,23 @@ module RuboCop
82
82
  def autocorrect(corrector, offense_range:, send_node:)
83
83
  corrector.replace(
84
84
  offense_range,
85
- add_safe_navigation_operator(
86
- offense_range: offense_range,
87
- send_node: send_node
88
- )
85
+ add_safe_navigation_operator(offense_range: offense_range, send_node: send_node)
89
86
  )
87
+
88
+ corrector.wrap(send_node, '(', ')') if require_parentheses?(send_node)
90
89
  end
91
90
 
92
91
  def brackets?(send_node)
93
92
  send_node.method?(:[]) || send_node.method?(:[]=)
94
93
  end
94
+
95
+ def require_parentheses?(send_node)
96
+ return false unless send_node.comparison_method?
97
+ return false unless (node = send_node.parent)
98
+
99
+ (node.respond_to?(:logical_operator?) && node.logical_operator?) ||
100
+ (node.respond_to?(:comparison_method?) && node.comparison_method?)
101
+ end
95
102
  end
96
103
  end
97
104
  end
@@ -3,8 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- #
7
- # This cop checks for `send`, `public_send`, and `__send__` methods
6
+ # Checks for `send`, `public_send`, and `__send__` methods
8
7
  # when using mix-in.
9
8
  #
10
9
  # `include` and `prepend` methods were private methods until Ruby 2.0,
@@ -121,18 +121,12 @@ module RuboCop
121
121
 
122
122
  if rescued_exceptions.any?
123
123
  rescued_exceptions.each_with_object([]) do |exception, converted|
124
- # FIXME: Workaround `rubocop:disable` comment for JRuby.
125
- # https://github.com/jruby/jruby/issues/6642
126
- # rubocop:disable Style/RedundantBegin
127
- begin
128
- RuboCop::Util.silence_warnings do
129
- # Avoid printing deprecation warnings about constants
130
- converted << Kernel.const_get(exception.source)
131
- end
132
- rescue NameError
133
- converted << nil
124
+ RuboCop::Util.silence_warnings do
125
+ # Avoid printing deprecation warnings about constants
126
+ converted << Kernel.const_get(exception.source)
134
127
  end
135
- # rubocop:enable Style/RedundantBegin
128
+ rescue NameError
129
+ converted << nil
136
130
  end
137
131
  else
138
132
  # treat an empty `rescue` as `rescue StandardError`
@@ -68,7 +68,7 @@ module RuboCop
68
68
 
69
69
  def same_conditions_node_different_branch?(variable, outer_local_variable)
70
70
  variable_node = variable_node(variable)
71
- return false unless variable_node.conditional?
71
+ return false unless node_or_its_ascendant_conditional?(variable_node)
72
72
 
73
73
  outer_local_variable_node =
74
74
  find_conditional_node_from_ascendant(outer_local_variable.declaration_node)
@@ -96,6 +96,12 @@ module RuboCop
96
96
 
97
97
  find_conditional_node_from_ascendant(parent)
98
98
  end
99
+
100
+ def node_or_its_ascendant_conditional?(node)
101
+ return true if node.conditional?
102
+
103
+ !!find_conditional_node_from_ascendant(node)
104
+ end
99
105
  end
100
106
  end
101
107
  end
@@ -32,25 +32,25 @@ module RuboCop
32
32
  # @!method struct_new(node)
33
33
  def_node_matcher :struct_new, <<~PATTERN
34
34
  (send
35
- (const ${nil? cbase} :Struct) :new ...)
35
+ (const {nil? cbase} :Struct) :new ...)
36
36
  PATTERN
37
37
 
38
38
  def on_send(node)
39
- return unless struct_new(node) do
40
- node.arguments.each_with_index do |arg, index|
41
- # Ignore if the first argument is a class name
42
- next if index.zero? && arg.str_type?
39
+ return unless struct_new(node)
43
40
 
44
- # Ignore if the argument is not a member name
45
- next unless STRUCT_MEMBER_NAME_TYPES.include?(arg.type)
41
+ node.arguments.each_with_index do |arg, index|
42
+ # Ignore if the first argument is a class name
43
+ next if index.zero? && arg.str_type?
46
44
 
47
- member_name = arg.value
45
+ # Ignore if the argument is not a member name
46
+ next unless STRUCT_MEMBER_NAME_TYPES.include?(arg.type)
48
47
 
49
- next unless STRUCT_METHOD_NAMES.include?(member_name.to_sym)
48
+ member_name = arg.value
50
49
 
51
- message = format(MSG, member_name: member_name.inspect, method_name: member_name.to_s)
52
- add_offense(arg, message: message)
53
- end
50
+ next unless STRUCT_METHOD_NAMES.include?(member_name.to_sym)
51
+
52
+ message = format(MSG, member_name: member_name.inspect, method_name: member_name.to_s)
53
+ add_offense(arg, message: message)
54
54
  end
55
55
  end
56
56
  end
@@ -117,9 +117,9 @@ module RuboCop
117
117
 
118
118
  def comment_between_rescue_and_end?(node)
119
119
  ancestor = node.each_ancestor(:kwbegin, :def, :defs, :block, :numblock).first
120
- return unless ancestor
120
+ return false unless ancestor
121
121
 
122
- end_line = ancestor.loc.end.line
122
+ end_line = ancestor.loc.end&.line || ancestor.loc.last_line
123
123
  processed_source[node.first_line...end_line].any? { |line| comment_line?(line) }
124
124
  end
125
125
 
@@ -124,7 +124,7 @@ module RuboCop
124
124
  source == value ||
125
125
  # `Symbol#inspect` uses double quotes, but allow single-quoted
126
126
  # symbols to work as well.
127
- source.tr("'", '"') == value
127
+ source.gsub('"', '\"').tr("'", '"') == value
128
128
  end
129
129
 
130
130
  def requires_quotes?(sym_node)
@@ -74,7 +74,7 @@ module RuboCop
74
74
  end
75
75
  end
76
76
 
77
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
77
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
78
78
  def argument_match?(send_arg, def_arg)
79
79
  def_arg_name = def_arg.children[0]
80
80
 
@@ -87,12 +87,14 @@ module RuboCop
87
87
  send_arg.hash_type? &&
88
88
  send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
89
89
  when :kwrestarg
90
- send_arg.each_child_node(:kwsplat).any? { |child| child.source == def_arg.source }
90
+ send_arg.each_child_node(:kwsplat, :forwarded_kwrestarg).any? do |child|
91
+ child.source == def_arg.source
92
+ end
91
93
  when :forward_arg
92
94
  send_arg.forwarded_args_type?
93
95
  end
94
96
  end
95
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
97
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
96
98
  end
97
99
  end
98
100
  end
@@ -8,25 +8,39 @@ module RuboCop
8
8
  # always ignored. This is detected automatically since Ruby 2.7.
9
9
  #
10
10
  # @example
11
+ # # bad
12
+ # return 1
11
13
  #
12
- # # Detected since Ruby 2.7
13
- # return 1 # 1 is always ignored.
14
+ # # good
15
+ # return
14
16
  class TopLevelReturnWithArgument < Base
15
- # This cop works by validating the ancestors of the return node. A
16
- # top-level return node's ancestors should not be of block, def, or
17
- # defs type.
17
+ extend AutoCorrector
18
18
 
19
19
  MSG = 'Top level return with argument detected.'
20
20
 
21
21
  def on_return(return_node)
22
- add_offense(return_node) if return_node.arguments? && ancestors_valid?(return_node)
22
+ return unless top_level_return_with_any_argument?(return_node)
23
+
24
+ add_offense(return_node) do |corrector|
25
+ remove_arguments(corrector, return_node)
26
+ end
23
27
  end
24
28
 
25
29
  private
26
30
 
27
- def ancestors_valid?(return_node)
28
- prohibited_ancestors = return_node.each_ancestor(:block, :def, :defs)
29
- prohibited_ancestors.none?
31
+ def top_level_return_with_any_argument?(return_node)
32
+ top_level_return?(return_node) && return_node.arguments?
33
+ end
34
+
35
+ def remove_arguments(corrector, return_node)
36
+ corrector.replace(return_node, 'return')
37
+ end
38
+
39
+ # This cop works by validating the ancestors of the return node. A
40
+ # top-level return node's ancestors should not be of block, def, or
41
+ # defs type.
42
+ def top_level_return?(return_node)
43
+ return_node.each_ancestor(:block, :def, :defs).none?
30
44
  end
31
45
  end
32
46
  end
@@ -7,12 +7,24 @@ module RuboCop
7
7
  # scope.
8
8
  # The basic idea for this cop was from the warning of `ruby -cw`:
9
9
  #
10
- # assigned but unused variable - foo
10
+ # [source,console]
11
+ # ----
12
+ # assigned but unused variable - foo
13
+ # ----
11
14
  #
12
15
  # Currently this cop has advanced logic that detects unreferenced
13
16
  # reassignments and properly handles varied cases such as branch, loop,
14
17
  # rescue, ensure, etc.
15
18
  #
19
+ # NOTE: Given the assignment `foo = 1, bar = 2`, removing unused variables
20
+ # can lead to a syntax error, so this case is not autocorrected.
21
+ #
22
+ # @safety
23
+ # This cop's autocorrection is unsafe because removing assignment from
24
+ # operator assignment can cause NameError if this assignment has been used to declare
25
+ # local variable. For example, replacing `a ||= 1` to `a || 1` may cause
26
+ # "undefined local variable or method `a' for main:Object (NameError)".
27
+ #
16
28
  # @example
17
29
  #
18
30
  # # bad
@@ -31,6 +43,10 @@ module RuboCop
31
43
  # do_something(some_var)
32
44
  # end
33
45
  class UselessAssignment < Base
46
+ extend AutoCorrector
47
+
48
+ include RangeHelp
49
+
34
50
  MSG = 'Useless assignment to variable - `%<variable>s`.'
35
51
 
36
52
  def self.joining_forces
@@ -41,23 +57,24 @@ module RuboCop
41
57
  scope.variables.each_value { |variable| check_for_unused_assignments(variable) }
42
58
  end
43
59
 
60
+ # rubocop:disable Metrics/AbcSize
44
61
  def check_for_unused_assignments(variable)
45
62
  return if variable.should_be_unused?
46
63
 
47
64
  variable.assignments.each do |assignment|
48
- next if assignment.used?
65
+ next if assignment.used? || part_of_ignored_node?(assignment.node)
49
66
 
50
67
  message = message_for_useless_assignment(assignment)
68
+ range = offense_range(assignment)
51
69
 
52
- location = if assignment.regexp_named_capture?
53
- assignment.node.children.first.source_range
54
- else
55
- assignment.node.loc.name
56
- end
70
+ add_offense(range, message: message) do |corrector|
71
+ autocorrect(corrector, assignment) unless sequential_assignment?(assignment.node)
72
+ end
57
73
 
58
- add_offense(location, message: message)
74
+ ignore_node(assignment.node) if chained_assignment?(assignment.node)
59
75
  end
60
76
  end
77
+ # rubocop:enable Metrics/AbcSize
61
78
 
62
79
  def message_for_useless_assignment(assignment)
63
80
  variable = assignment.variable
@@ -65,6 +82,28 @@ module RuboCop
65
82
  format(MSG, variable: variable.name) + message_specification(assignment, variable).to_s
66
83
  end
67
84
 
85
+ def offense_range(assignment)
86
+ if assignment.regexp_named_capture?
87
+ assignment.node.children.first.source_range
88
+ else
89
+ assignment.node.loc.name
90
+ end
91
+ end
92
+
93
+ def sequential_assignment?(node)
94
+ if node.lvasgn_type? && node.expression&.array_type? &&
95
+ node.each_descendant.any?(&:assignment?)
96
+ return true
97
+ end
98
+ return false unless node.parent
99
+
100
+ sequential_assignment?(node.parent)
101
+ end
102
+
103
+ def chained_assignment?(node)
104
+ node.respond_to?(:expression) && node.expression&.lvasgn_type?
105
+ end
106
+
68
107
  def message_specification(assignment, variable)
69
108
  if assignment.multiple_assignment?
70
109
  multiple_assignment_message(variable.name)
@@ -84,8 +123,7 @@ module RuboCop
84
123
  return_value_node = return_value_node_of_scope(scope)
85
124
  return unless assignment.meta_assignment_node.equal?(return_value_node)
86
125
 
87
- " Use `#{assignment.operator.sub(/=$/, '')}` " \
88
- "instead of `#{assignment.operator}`."
126
+ " Use `#{assignment.operator.delete_suffix('=')}` instead of `#{assignment.operator}`."
89
127
  end
90
128
 
91
129
  def similar_name_message(variable)
@@ -119,6 +157,52 @@ module RuboCop
119
157
 
120
158
  node.receiver.nil? && !node.arguments?
121
159
  end
160
+
161
+ # rubocop:disable Metrics/AbcSize
162
+ def autocorrect(corrector, assignment)
163
+ if assignment.exception_assignment?
164
+ remove_exception_assignment_part(corrector, assignment.node)
165
+ elsif assignment.multiple_assignment? || assignment.rest_assignment? ||
166
+ assignment.for_assignment?
167
+ rename_variable_with_underscore(corrector, assignment.node)
168
+ elsif assignment.operator_assignment?
169
+ remove_trailing_character_from_operator(corrector, assignment.node)
170
+ elsif assignment.regexp_named_capture?
171
+ replace_named_capture_group_with_non_capturing_group(corrector, assignment.node,
172
+ assignment.variable.name)
173
+ else
174
+ remove_local_variable_assignment_part(corrector, assignment.node)
175
+ end
176
+ end
177
+ # rubocop:enable Metrics/AbcSize
178
+
179
+ def remove_exception_assignment_part(corrector, node)
180
+ corrector.remove(
181
+ range_between(
182
+ (node.parent.children.first&.source_range || node.parent.location.keyword).end_pos,
183
+ node.source_range.end_pos
184
+ )
185
+ )
186
+ end
187
+
188
+ def rename_variable_with_underscore(corrector, node)
189
+ corrector.replace(node, '_')
190
+ end
191
+
192
+ def remove_trailing_character_from_operator(corrector, node)
193
+ corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
194
+ end
195
+
196
+ def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
197
+ corrector.replace(
198
+ node.children.first,
199
+ node.children.first.source.sub(/\(\?<#{variable_name}>/, '(?:')
200
+ )
201
+ end
202
+
203
+ def remove_local_variable_assignment_part(corrector, node)
204
+ corrector.replace(node, node.expression.source)
205
+ end
122
206
  end
123
207
  end
124
208
  end
@@ -6,6 +6,16 @@ module RuboCop
6
6
  # Checks for operators, variables, literals, lambda, proc and nonmutating
7
7
  # methods used in void context.
8
8
  #
9
+ # `each` blocks are allowed to prevent false positives.
10
+ # For example, the expression inside the `each` block below.
11
+ # It's not void, especially when the receiver is an `Enumerator`:
12
+ #
13
+ # [source,ruby]
14
+ # ----
15
+ # enumerator = [1, 2, 3].filter
16
+ # enumerator.each { |item| item >= 2 } #=> [2, 3]
17
+ # ----
18
+ #
9
19
  # @example CheckForMethodsWithNoSideEffects: false (default)
10
20
  # # bad
11
21
  # def some_method
@@ -41,8 +51,13 @@ module RuboCop
41
51
  # do_something(some_array)
42
52
  # end
43
53
  class Void < Base
54
+ extend AutoCorrector
55
+
56
+ include RangeHelp
57
+
44
58
  OP_MSG = 'Operator `%<op>s` used in void context.'
45
59
  VAR_MSG = 'Variable `%<var>s` used in void context.'
60
+ CONST_MSG = 'Constant `%<var>s` used in void context.'
46
61
  LIT_MSG = 'Literal `%<lit>s` used in void context.'
47
62
  SELF_MSG = '`self` used in void context.'
48
63
  EXPRESSION_MSG = '`%<expression>s` used in void context.'
@@ -68,6 +83,7 @@ module RuboCop
68
83
  return unless node.body && !node.body.begin_type?
69
84
  return unless in_void_context?(node.body)
70
85
 
86
+ check_void_op(node.body) { node.method?(:each) }
71
87
  check_expression(node.body)
72
88
  end
73
89
 
@@ -83,11 +99,13 @@ module RuboCop
83
99
  def check_begin(node)
84
100
  expressions = *node
85
101
  expressions.pop unless in_void_context?(node)
86
- expressions.each { |expr| check_expression(expr) }
102
+ expressions.each do |expr|
103
+ check_void_op(expr)
104
+ check_expression(expr)
105
+ end
87
106
  end
88
107
 
89
108
  def check_expression(expr)
90
- check_void_op(expr)
91
109
  check_literal(expr)
92
110
  check_var(expr)
93
111
  check_self(expr)
@@ -97,38 +115,60 @@ module RuboCop
97
115
  check_nonmutating(expr)
98
116
  end
99
117
 
100
- def check_void_op(node)
118
+ def check_void_op(node, &block)
101
119
  return unless node.send_type? && OPERATORS.include?(node.method_name)
120
+ return if block && yield(node)
102
121
 
103
- add_offense(node.loc.selector, message: format(OP_MSG, op: node.method_name))
122
+ add_offense(node.loc.selector,
123
+ message: format(OP_MSG, op: node.method_name)) do |corrector|
124
+ autocorrect_void_op(corrector, node)
125
+ end
104
126
  end
105
127
 
106
128
  def check_var(node)
107
129
  return unless node.variable? || node.const_type?
108
130
 
109
- add_offense(node.loc.name, message: format(VAR_MSG, var: node.loc.name.source))
131
+ if node.const_type?
132
+ template = node.special_keyword? ? VAR_MSG : CONST_MSG
133
+
134
+ offense_range = node
135
+ message = format(template, var: node.source)
136
+ else
137
+ offense_range = node.loc.name
138
+ message = format(VAR_MSG, var: node.loc.name.source)
139
+ end
140
+
141
+ add_offense(offense_range, message: message) do |corrector|
142
+ autocorrect_void_expression(corrector, node)
143
+ end
110
144
  end
111
145
 
112
146
  def check_literal(node)
113
147
  return if !node.literal? || node.xstr_type? || node.range_type?
114
148
 
115
- add_offense(node, message: format(LIT_MSG, lit: node.source))
149
+ add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
150
+ autocorrect_void_expression(corrector, node)
151
+ end
116
152
  end
117
153
 
118
154
  def check_self(node)
119
155
  return unless node.self_type?
120
156
 
121
- add_offense(node, message: SELF_MSG)
157
+ add_offense(node, message: SELF_MSG) do |corrector|
158
+ autocorrect_void_expression(corrector, node)
159
+ end
122
160
  end
123
161
 
124
162
  def check_void_expression(node)
125
163
  return unless node.defined_type? || node.lambda_or_proc?
126
164
 
127
- add_offense(node, message: format(EXPRESSION_MSG, expression: node.source))
165
+ add_offense(node, message: format(EXPRESSION_MSG, expression: node.source)) do |corrector|
166
+ autocorrect_void_expression(corrector, node)
167
+ end
128
168
  end
129
169
 
130
170
  def check_nonmutating(node)
131
- return unless node.respond_to?(:method_name)
171
+ return if !node.send_type? && !node.block_type? && !node.numblock_type?
132
172
 
133
173
  method_name = node.method_name
134
174
  return unless NONMUTATING_METHODS.include?(method_name)
@@ -139,7 +179,10 @@ module RuboCop
139
179
  "#{method_name}!"
140
180
  end
141
181
  add_offense(node,
142
- message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
182
+ message: format(NONMUTATING_MSG, method: method_name,
183
+ suggest: suggestion)) do |corrector|
184
+ autocorrect_nonmutating_send(corrector, node, suggestion)
185
+ end
143
186
  end
144
187
 
145
188
  def in_void_context?(node)
@@ -149,6 +192,31 @@ module RuboCop
149
192
 
150
193
  VOID_CONTEXT_TYPES.include?(parent.type) && parent.void_context?
151
194
  end
195
+
196
+ def autocorrect_void_op(corrector, node)
197
+ if node.arguments.empty?
198
+ corrector.replace(node, node.receiver.source)
199
+ else
200
+ corrector.replace(
201
+ range_with_surrounding_space(range: node.loc.selector, side: :both,
202
+ newlines: false),
203
+ "\n"
204
+ )
205
+ end
206
+ end
207
+
208
+ def autocorrect_void_expression(corrector, node)
209
+ corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
210
+ end
211
+
212
+ def autocorrect_nonmutating_send(corrector, node, suggestion)
213
+ send_node = if node.send_type?
214
+ node
215
+ else
216
+ node.send_node
217
+ end
218
+ corrector.replace(send_node.loc.selector, suggestion)
219
+ end
152
220
  end
153
221
  end
154
222
  end
@@ -12,6 +12,7 @@ module RuboCop
12
12
  # Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
13
13
  # will be counted as one line regardless of its actual size.
14
14
  #
15
+ # NOTE: This cop does not apply for `Struct` definitions.
15
16
  #
16
17
  # NOTE: The `ExcludedMethods` configuration is deprecated and only kept
17
18
  # for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns`
@@ -40,7 +41,6 @@ module RuboCop
40
41
  # )
41
42
  # end # 6 points
42
43
  #
43
- # NOTE: This cop does not apply for `Struct` definitions.
44
44
  class BlockLength < Base
45
45
  include CodeLength
46
46
  include AllowedMethods
@@ -11,6 +11,8 @@ module RuboCop
11
11
  # Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
12
12
  # will be counted as one line regardless of its actual size.
13
13
  #
14
+ # NOTE: This cop also applies for `Struct` definitions.
15
+ #
14
16
  # @example CountAsOne: ['array', 'heredoc', 'method_call']
15
17
  #
16
18
  # class Foo
@@ -34,15 +36,18 @@ module RuboCop
34
36
  # )
35
37
  # end # 6 points
36
38
  #
37
- #
38
- # NOTE: This cop also applies for `Struct` definitions.
39
39
  class ClassLength < Base
40
40
  include CodeLength
41
41
 
42
42
  def on_class(node)
43
43
  check_code_length(node)
44
44
  end
45
- alias on_sclass on_class
45
+
46
+ def on_sclass(node)
47
+ return if node.each_ancestor(:class).any?
48
+
49
+ on_class(node)
50
+ end
46
51
 
47
52
  def on_casgn(node)
48
53
  parent = node.parent