rubocop 1.50.2 → 1.57.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) 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/lsp.rb +19 -0
  6. data/lib/rubocop/cli.rb +4 -1
  7. data/lib/rubocop/config.rb +4 -0
  8. data/lib/rubocop/config_finder.rb +2 -2
  9. data/lib/rubocop/config_loader_resolver.rb +4 -3
  10. data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
  11. data/lib/rubocop/config_obsoletion.rb +2 -2
  12. data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
  13. data/lib/rubocop/cop/base.rb +6 -2
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
  15. data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
  16. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  17. data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
  18. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
  19. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
  21. data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
  22. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
  24. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
  26. data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
  27. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
  29. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
  30. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  31. data/lib/rubocop/cop/layout/class_structure.rb +7 -0
  32. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  33. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  34. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
  35. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
  36. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
  37. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  38. data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -0
  39. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  40. data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
  41. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  42. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
  43. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  44. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  45. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  46. data/lib/rubocop/cop/layout/redundant_line_break.rb +14 -4
  47. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  48. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
  50. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
  52. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  53. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  54. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
  55. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  56. data/lib/rubocop/cop/lint/debugger.rb +18 -5
  57. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
  58. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
  59. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  60. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
  61. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
  62. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  63. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  64. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  65. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  66. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  67. data/lib/rubocop/cop/lint/missing_super.rb +34 -5
  68. data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
  69. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
  70. data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
  71. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  72. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  73. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  74. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
  75. data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -3
  76. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
  77. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  78. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
  79. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  80. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  81. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
  82. data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
  83. data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
  84. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  85. data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
  86. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  87. data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
  88. data/lib/rubocop/cop/lint/void.rb +57 -7
  89. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  90. data/lib/rubocop/cop/metrics/class_length.rb +2 -2
  91. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  92. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  93. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
  94. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  95. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  96. data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
  97. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  98. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  100. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
  101. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  102. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +5 -7
  103. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  104. data/lib/rubocop/cop/mixin/string_help.rb +4 -2
  105. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  106. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  107. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  108. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
  110. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
  111. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
  112. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  113. data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
  114. data/lib/rubocop/cop/style/alias.rb +9 -8
  115. data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -63
  116. data/lib/rubocop/cop/style/array_intersect.rb +13 -5
  117. data/lib/rubocop/cop/style/attr.rb +11 -1
  118. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  119. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  120. data/lib/rubocop/cop/style/block_delimiters.rb +5 -4
  121. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  122. data/lib/rubocop/cop/style/class_equality_comparison.rb +24 -39
  123. data/lib/rubocop/cop/style/collection_compact.rb +16 -6
  124. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  125. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  126. data/lib/rubocop/cop/style/combinable_loops.rb +30 -8
  127. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  128. data/lib/rubocop/cop/style/conditional_assignment.rb +5 -3
  129. data/lib/rubocop/cop/style/copyright.rb +5 -2
  130. data/lib/rubocop/cop/style/dir.rb +1 -1
  131. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  132. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  133. data/lib/rubocop/cop/style/documentation.rb +1 -1
  134. data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
  135. data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
  136. data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
  137. data/lib/rubocop/cop/style/file_read.rb +2 -2
  138. data/lib/rubocop/cop/style/for.rb +1 -1
  139. data/lib/rubocop/cop/style/format_string.rb +24 -3
  140. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
  141. data/lib/rubocop/cop/style/guard_clause.rb +28 -0
  142. data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
  143. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  144. data/lib/rubocop/cop/style/hash_except.rb +19 -8
  145. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  146. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  147. data/lib/rubocop/cop/style/identical_conditional_branches.rb +23 -5
  148. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  149. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
  150. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  151. data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -6
  152. data/lib/rubocop/cop/style/lambda.rb +3 -3
  153. data/lib/rubocop/cop/style/lambda_call.rb +5 -0
  154. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -5
  155. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  156. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  157. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  158. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  159. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  160. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  161. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  162. data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
  163. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  164. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  165. data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
  166. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  167. data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
  168. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -10
  169. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
  170. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +86 -5
  171. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  172. data/lib/rubocop/cop/style/redundant_filter_chain.rb +117 -0
  173. data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -3
  174. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -7
  175. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
  176. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  177. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  178. data/lib/rubocop/cop/style/redundant_return.rb +7 -2
  179. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
  180. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  181. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
  182. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  183. data/lib/rubocop/cop/style/require_order.rb +11 -5
  184. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  185. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  186. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
  187. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  188. data/lib/rubocop/cop/style/semicolon.rb +12 -4
  189. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  190. data/lib/rubocop/cop/style/single_line_do_end_block.rb +65 -0
  191. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  192. data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -2
  193. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  194. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
  195. data/lib/rubocop/cop/style/symbol_array.rb +35 -15
  196. data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
  197. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  198. data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
  199. data/lib/rubocop/cop/team.rb +1 -1
  200. data/lib/rubocop/cop/util.rb +1 -1
  201. data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
  202. data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
  203. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  204. data/lib/rubocop/cop/variable_force.rb +1 -0
  205. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  206. data/lib/rubocop/ext/regexp_parser.rb +4 -1
  207. data/lib/rubocop/file_finder.rb +4 -7
  208. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  209. data/lib/rubocop/lsp/logger.rb +22 -0
  210. data/lib/rubocop/lsp/routes.rb +246 -0
  211. data/lib/rubocop/lsp/runtime.rb +99 -0
  212. data/lib/rubocop/lsp/server.rb +68 -0
  213. data/lib/rubocop/lsp/severity.rb +27 -0
  214. data/lib/rubocop/magic_comment.rb +12 -10
  215. data/lib/rubocop/options.rb +11 -1
  216. data/lib/rubocop/result_cache.rb +5 -1
  217. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  218. data/lib/rubocop/rspec/shared_contexts.rb +2 -3
  219. data/lib/rubocop/runner.rb +5 -3
  220. data/lib/rubocop/server/cache.rb +1 -0
  221. data/lib/rubocop/server/client_command/exec.rb +3 -2
  222. data/lib/rubocop/string_interpreter.rb +3 -3
  223. data/lib/rubocop/target_finder.rb +7 -3
  224. data/lib/rubocop/target_ruby.rb +12 -7
  225. data/lib/rubocop/version.rb +10 -6
  226. data/lib/rubocop.rb +15 -0
  227. metadata +63 -15
@@ -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
@@ -41,6 +41,10 @@ module RuboCop
41
41
  # do_something(some_array)
42
42
  # end
43
43
  class Void < Base
44
+ extend AutoCorrector
45
+
46
+ include RangeHelp
47
+
44
48
  OP_MSG = 'Operator `%<op>s` used in void context.'
45
49
  VAR_MSG = 'Variable `%<var>s` used in void context.'
46
50
  LIT_MSG = 'Literal `%<lit>s` used in void context.'
@@ -100,35 +104,53 @@ module RuboCop
100
104
  def check_void_op(node)
101
105
  return unless node.send_type? && OPERATORS.include?(node.method_name)
102
106
 
103
- add_offense(node.loc.selector, message: format(OP_MSG, op: node.method_name))
107
+ add_offense(node.loc.selector,
108
+ message: format(OP_MSG, op: node.method_name)) do |corrector|
109
+ autocorrect_void_op(corrector, node)
110
+ end
104
111
  end
105
112
 
106
113
  def check_var(node)
107
114
  return unless node.variable? || node.const_type?
108
115
 
109
- add_offense(node.loc.name, message: format(VAR_MSG, var: node.loc.name.source))
116
+ if node.const_type? && node.special_keyword?
117
+ add_offense(node, message: format(VAR_MSG, var: node.source)) do |corrector|
118
+ autocorrect_void_expression(corrector, node)
119
+ end
120
+ else
121
+ add_offense(node.loc.name,
122
+ message: format(VAR_MSG, var: node.loc.name.source)) do |corrector|
123
+ autocorrect_void_expression(corrector, node)
124
+ end
125
+ end
110
126
  end
111
127
 
112
128
  def check_literal(node)
113
129
  return if !node.literal? || node.xstr_type? || node.range_type?
114
130
 
115
- add_offense(node, message: format(LIT_MSG, lit: node.source))
131
+ add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
132
+ autocorrect_void_expression(corrector, node)
133
+ end
116
134
  end
117
135
 
118
136
  def check_self(node)
119
137
  return unless node.self_type?
120
138
 
121
- add_offense(node, message: SELF_MSG)
139
+ add_offense(node, message: SELF_MSG) do |corrector|
140
+ autocorrect_void_expression(corrector, node)
141
+ end
122
142
  end
123
143
 
124
144
  def check_void_expression(node)
125
145
  return unless node.defined_type? || node.lambda_or_proc?
126
146
 
127
- add_offense(node, message: format(EXPRESSION_MSG, expression: node.source))
147
+ add_offense(node, message: format(EXPRESSION_MSG, expression: node.source)) do |corrector|
148
+ autocorrect_void_expression(corrector, node)
149
+ end
128
150
  end
129
151
 
130
152
  def check_nonmutating(node)
131
- return unless node.respond_to?(:method_name)
153
+ return if !node.send_type? && !node.block_type? && !node.numblock_type?
132
154
 
133
155
  method_name = node.method_name
134
156
  return unless NONMUTATING_METHODS.include?(method_name)
@@ -139,7 +161,10 @@ module RuboCop
139
161
  "#{method_name}!"
140
162
  end
141
163
  add_offense(node,
142
- message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
164
+ message: format(NONMUTATING_MSG, method: method_name,
165
+ suggest: suggestion)) do |corrector|
166
+ autocorrect_nonmutating_send(corrector, node, suggestion)
167
+ end
143
168
  end
144
169
 
145
170
  def in_void_context?(node)
@@ -149,6 +174,31 @@ module RuboCop
149
174
 
150
175
  VOID_CONTEXT_TYPES.include?(parent.type) && parent.void_context?
151
176
  end
177
+
178
+ def autocorrect_void_op(corrector, node)
179
+ if node.arguments.empty?
180
+ corrector.replace(node, node.receiver.source)
181
+ else
182
+ corrector.replace(
183
+ range_with_surrounding_space(range: node.loc.selector, side: :both,
184
+ newlines: false),
185
+ "\n"
186
+ )
187
+ end
188
+ end
189
+
190
+ def autocorrect_void_expression(corrector, node)
191
+ corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
192
+ end
193
+
194
+ def autocorrect_nonmutating_send(corrector, node, suggestion)
195
+ send_node = if node.send_type?
196
+ node
197
+ else
198
+ node.send_node
199
+ end
200
+ corrector.replace(send_node.loc.selector, suggestion)
201
+ end
152
202
  end
153
203
  end
154
204
  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,8 +36,6 @@ 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
 
@@ -54,7 +54,7 @@ module RuboCop
54
54
  alias on_defs on_def
55
55
 
56
56
  def on_block(node)
57
- return unless node.send_node.method?(:define_method)
57
+ return unless node.method?(:define_method)
58
58
 
59
59
  check_code_length(node)
60
60
  end
@@ -117,8 +117,7 @@ module RuboCop
117
117
  end
118
118
 
119
119
  def capturing_variable?(name)
120
- # TODO: Remove `Symbol#to_s` after supporting only Ruby >= 2.7.
121
- name && !name.to_s.start_with?('_')
120
+ name && !name.start_with?('_')
122
121
  end
123
122
 
124
123
  def branch?(node)