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
@@ -45,13 +45,13 @@ module RuboCop
45
45
  (block
46
46
  (send _ _)
47
47
  (args
48
- (arg _)
48
+ $(arg _)
49
49
  (arg _))
50
50
  {
51
- (send
51
+ $(send
52
52
  _ {:== :!= :eql? :include?} _)
53
53
  (send
54
- (send
54
+ $(send
55
55
  _ {:== :!= :eql? :include?} _) :!)
56
56
  })
57
57
  PATTERN
@@ -61,13 +61,13 @@ module RuboCop
61
61
  (block
62
62
  (send _ _)
63
63
  (args
64
- (arg _)
64
+ $(arg _)
65
65
  (arg _))
66
66
  {
67
- (send
67
+ $(send
68
68
  _ {:== :!= :eql? :in? :include? :exclude?} _)
69
69
  (send
70
- (send
70
+ $(send
71
71
  _ {:== :!= :eql? :in? :include? :exclude?} _) :!)
72
72
  })
73
73
  PATTERN
@@ -89,13 +89,24 @@ module RuboCop
89
89
 
90
90
  private
91
91
 
92
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
92
93
  def bad_method?(block)
93
94
  if active_support_extensions_enabled?
94
- bad_method_with_active_support?(block)
95
+ bad_method_with_active_support?(block) do |key_arg, send_node|
96
+ if send_node.method?(:in?) && send_node.receiver&.source != key_arg.source
97
+ return false
98
+ end
99
+ return true if !send_node.method?(:include?) && !send_node.method?(:exclude?)
100
+
101
+ send_node.first_argument&.source == key_arg.source
102
+ end
95
103
  else
96
- bad_method_with_poro?(block)
104
+ bad_method_with_poro?(block) do |key_arg, send_node|
105
+ !send_node.method?(:include?) || send_node.first_argument&.source == key_arg.source
106
+ end
97
107
  end
98
108
  end
109
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
99
110
 
100
111
  def semantically_except_method?(send, block)
101
112
  body = block.body
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Looks for uses of `_.each_with_object({}) {...}`,
7
- # `_.map {...}.to_h`, and `Hash[_.map {...}]` that are actually just
6
+ # Looks for uses of `\_.each_with_object({}) {...}`,
7
+ # `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
8
8
  # transforming the keys of a hash, and tries to use a simpler & faster
9
9
  # call to `transform_keys` instead.
10
10
  # It should only be enabled on Ruby version 2.5 or newer.
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Looks for uses of `_.each_with_object({}) {...}`,
7
- # `_.map {...}.to_h`, and `Hash[_.map {...}]` that are actually just
6
+ # Looks for uses of `\_.each_with_object({}) {...}`,
7
+ # `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
8
8
  # transforming the values of a hash, and tries to use a simpler & faster
9
9
  # call to `transform_values` instead.
10
10
  #
@@ -136,7 +136,7 @@ module RuboCop
136
136
 
137
137
  private
138
138
 
139
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
139
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
140
140
  def check_branches(node, branches)
141
141
  # return if any branch is empty. An empty branch can be an `if`
142
142
  # without an `else` or a branch that contains only comments.
@@ -149,22 +149,39 @@ module RuboCop
149
149
  branches.any? { |branch| single_child_branch?(branch) }
150
150
 
151
151
  heads = branches.map { |branch| head(branch) }
152
- check_expressions(node, heads, :before_condition) if duplicated_expressions?(node, heads)
152
+
153
+ return unless duplicated_expressions?(node, heads)
154
+
155
+ condition_variable = assignable_condition_value(node)
156
+ return if heads.first.assignment? && condition_variable == heads.first.name.to_s
157
+
158
+ check_expressions(node, heads, :before_condition)
153
159
  end
154
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
160
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
155
161
 
156
162
  def duplicated_expressions?(node, expressions)
157
163
  unique_expressions = expressions.uniq
158
164
  return false unless expressions.size >= 1 && unique_expressions.one?
159
165
 
160
166
  unique_expression = unique_expressions.first
161
- return true unless unique_expression.assignment?
167
+ return true unless unique_expression&.assignment?
162
168
 
163
169
  lhs = unique_expression.child_nodes.first
164
170
  node.condition.child_nodes.none? { |n| n.source == lhs.source if n.variable? }
165
171
  end
166
172
 
167
- def check_expressions(node, expressions, insert_position) # rubocop:disable Metrics/MethodLength
173
+ def assignable_condition_value(node)
174
+ if node.condition.call_type?
175
+ (receiver = node.condition.receiver) ? receiver.source : node.condition.source
176
+ elsif node.condition.variable?
177
+ node.condition.source
178
+ end
179
+ end
180
+
181
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
182
+ def check_expressions(node, expressions, insert_position)
183
+ return if expressions.any?(&:nil?)
184
+
168
185
  inserted_expression = false
169
186
 
170
187
  expressions.each do |expression|
@@ -184,6 +201,7 @@ module RuboCop
184
201
  end
185
202
  end
186
203
  end
204
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
187
205
 
188
206
  def last_child_of_parent?(node)
189
207
  return true unless (parent = node.parent)
@@ -59,11 +59,13 @@ module RuboCop
59
59
  # end
60
60
  #
61
61
  class IfInsideElse < Base
62
+ include IgnoredNode
62
63
  include RangeHelp
63
64
  extend AutoCorrector
64
65
 
65
66
  MSG = 'Convert `if` nested inside `else` to `elsif`.'
66
67
 
68
+ # rubocop:disable Metrics/CyclomaticComplexity
67
69
  def on_if(node)
68
70
  return if node.ternary? || node.unless?
69
71
 
@@ -73,9 +75,13 @@ module RuboCop
73
75
  return if allow_if_modifier_in_else_branch?(else_branch)
74
76
 
75
77
  add_offense(else_branch.loc.keyword) do |corrector|
78
+ next if part_of_ignored_node?(node)
79
+
76
80
  autocorrect(corrector, else_branch)
81
+ ignore_node(node)
77
82
  end
78
83
  end
84
+ # rubocop:enable Metrics/CyclomaticComplexity
79
85
 
80
86
  private
81
87
 
@@ -84,7 +84,10 @@ module RuboCop
84
84
  return unless (msg = message(node))
85
85
 
86
86
  add_offense(node.loc.keyword, message: format(msg, keyword: node.keyword)) do |corrector|
87
+ next if part_of_ignored_node?(node)
88
+
87
89
  autocorrect(corrector, node)
90
+ ignore_node(node)
88
91
  end
89
92
  end
90
93
 
@@ -47,7 +47,7 @@ module RuboCop
47
47
  def correct_elsif(node)
48
48
  <<~RUBY.chop
49
49
  if #{node.condition.source}
50
- #{node.if_branch.source}
50
+ #{node.if_branch&.source}
51
51
  #{build_else_branch(node.else_branch).chop}
52
52
  end
53
53
  RUBY
@@ -56,7 +56,7 @@ module RuboCop
56
56
  def build_else_branch(second_condition)
57
57
  result = <<~RUBY
58
58
  elsif #{second_condition.condition.source}
59
- #{second_condition.if_branch.source}
59
+ #{second_condition.if_branch&.source}
60
60
  RUBY
61
61
 
62
62
  if second_condition.else_branch
@@ -10,12 +10,16 @@ module RuboCop
10
10
  # Methods that can be inverted should be defined in `InverseMethods`. Note that
11
11
  # the relationship of inverse methods needs to be defined in both directions.
12
12
  # For example,
13
- # InverseMethods:
14
- # :!=: :==
15
- # :even?: :odd?
16
- # :odd?: :even?
17
13
  #
18
- # will suggest both `even?` and `odd?` to be inverted, but only `!=` (and not `==`).
14
+ # [source,yaml]
15
+ # ----
16
+ # InverseMethods:
17
+ # :!=: :==
18
+ # :even?: :odd?
19
+ # :odd?: :even?
20
+ # ----
21
+ #
22
+ # will suggest both `even?` and `odd?` to be inverted, but only `!=` (and not `==`).
19
23
  #
20
24
  # @safety
21
25
  # This cop is unsafe because it cannot be guaranteed that the method
@@ -68,7 +72,7 @@ module RuboCop
68
72
  when :begin
69
73
  invertible?(node.children.first)
70
74
  when :send
71
- return if inheritance_check?(node)
75
+ return false if inheritance_check?(node)
72
76
 
73
77
  node.method?(:!) || inverse_methods.key?(node.method_name)
74
78
  when :or, :and
@@ -69,10 +69,10 @@ module RuboCop
69
69
  return unless offending_selector?(node, selector)
70
70
 
71
71
  add_offense(node.send_node.source_range, message: message(node, selector)) do |corrector|
72
- if node.send_node.source == 'lambda'
73
- autocorrect_method_to_literal(corrector, node)
74
- else
72
+ if node.send_node.lambda_literal?
75
73
  LambdaLiteralToMethodCorrector.new(node).call(corrector)
74
+ else
75
+ autocorrect_method_to_literal(corrector, node)
76
76
  end
77
77
  end
78
78
  end
@@ -20,6 +20,7 @@ module RuboCop
20
20
  # lambda.(x, y)
21
21
  class LambdaCall < Base
22
22
  include ConfigurableEnforcedStyle
23
+ include IgnoredNode
23
24
  extend AutoCorrector
24
25
 
25
26
  MSG = 'Prefer the use of `%<prefer>s` over `%<current>s`.'
@@ -33,8 +34,12 @@ module RuboCop
33
34
  current = node.source
34
35
 
35
36
  add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector|
37
+ next if part_of_ignored_node?(node)
38
+
36
39
  opposite_style_detected
37
40
  corrector.replace(node, prefer)
41
+
42
+ ignore_node(node)
38
43
  end
39
44
  else
40
45
  correct_style_detected
@@ -98,7 +98,7 @@ module RuboCop
98
98
 
99
99
  def call_in_literals?(node)
100
100
  parent = node.parent&.block_type? ? node.parent.parent : node.parent
101
- return unless parent
101
+ return false unless parent
102
102
 
103
103
  parent.pair_type? ||
104
104
  parent.array_type? ||
@@ -109,7 +109,7 @@ module RuboCop
109
109
 
110
110
  def call_in_logical_operators?(node)
111
111
  parent = node.parent&.block_type? ? node.parent.parent : node.parent
112
- return unless parent
112
+ return false unless parent
113
113
 
114
114
  logical_operator?(parent) ||
115
115
  (parent.send_type? &&
@@ -124,9 +124,10 @@ module RuboCop
124
124
  node.parent&.class_type? && node.parent&.single_line?
125
125
  end
126
126
 
127
- def call_with_ambiguous_arguments?(node)
127
+ def call_with_ambiguous_arguments?(node) # rubocop:disable Metrics/PerceivedComplexity
128
128
  call_with_braced_block?(node) ||
129
129
  call_as_argument_or_chain?(node) ||
130
+ call_in_match_pattern?(node) ||
130
131
  hash_literal_in_arguments?(node) ||
131
132
  node.descendants.any? do |n|
132
133
  n.forwarded_args_type? || ambiguous_literal?(n) || logical_operator?(n) ||
@@ -135,8 +136,7 @@ module RuboCop
135
136
  end
136
137
 
137
138
  def call_with_braced_block?(node)
138
- (node.send_type? || node.super_type?) &&
139
- ((node.parent&.block_type? || node.parent&.numblock_type?) && node.parent&.braces?)
139
+ (node.send_type? || node.super_type?) && node.block_node&.braces?
140
140
  end
141
141
 
142
142
  def call_as_argument_or_chain?(node)
@@ -145,6 +145,12 @@ module RuboCop
145
145
  node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?)
146
146
  end
147
147
 
148
+ def call_in_match_pattern?(node)
149
+ return false unless (parent = node.parent)
150
+
151
+ parent.match_pattern_type? || parent.match_pattern_p_type?
152
+ end
153
+
148
154
  def hash_literal_in_arguments?(node)
149
155
  node.arguments.any? do |n|
150
156
  hash_literal?(n) ||
@@ -40,7 +40,7 @@ module RuboCop
40
40
  def on_class(node)
41
41
  begin_node = node.child_nodes.find(&:begin_type?) || node
42
42
  begin_node.each_child_node(:send).select(&:macro?).each do |macro|
43
- next unless MIXIN_METHODS.include?(macro.method_name)
43
+ next if !MIXIN_METHODS.include?(macro.method_name) || macro.arguments.empty?
44
44
 
45
45
  check(macro)
46
46
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  MSG = 'Avoid multi-line chains of blocks.'
29
29
 
30
30
  def on_block(node)
31
- node.send_node.each_node(:send) do |send_node|
31
+ node.send_node.each_node(:send, :csend) do |send_node|
32
32
  receiver = send_node.receiver
33
33
 
34
34
  next unless (receiver&.block_type? || receiver&.numblock_type?) && receiver&.multiline?
@@ -39,7 +39,7 @@ module RuboCop
39
39
 
40
40
  MSG_IF = 'Avoid multi-line ternary operators, use `if` or `unless` instead.'
41
41
  MSG_SINGLE_LINE = 'Avoid multi-line ternary operators, use single-line instead.'
42
- SINGLE_LINE_TYPES = %i[return break next send].freeze
42
+ SINGLE_LINE_TYPES = %i[return break next send csend].freeze
43
43
 
44
44
  def on_if(node)
45
45
  return unless offense?(node)
@@ -40,6 +40,15 @@ module RuboCop
40
40
  #
41
41
  # # good
42
42
  # foo if [b.lightweight, b.heavyweight].include?(a)
43
+ #
44
+ # @example ComparisonsThreshold: 2 (default)
45
+ # # bad
46
+ # foo if a == 'a' || a == 'b'
47
+ #
48
+ # @example ComparisonsThreshold: 3
49
+ # # good
50
+ # foo if a == 'a' || a == 'b'
51
+ #
43
52
  class MultipleComparison < Base
44
53
  extend AutoCorrector
45
54
 
@@ -58,6 +67,7 @@ module RuboCop
58
67
  return unless node == root_of_or_node
59
68
  return unless nested_variable_comparison?(root_of_or_node)
60
69
  return if @allowed_method_comparison
70
+ return if @compared_elements.size < comparisons_threshold
61
71
 
62
72
  add_offense(node) do |corrector|
63
73
  elements = @compared_elements.join(', ')
@@ -151,6 +161,10 @@ module RuboCop
151
161
  def allow_method_comparison?
152
162
  cop_config.fetch('AllowMethodComparison', true)
153
163
  end
164
+
165
+ def comparisons_threshold
166
+ cop_config.fetch('ComparisonsThreshold', 2)
167
+ end
154
168
  end
155
169
  end
156
170
  end
@@ -27,24 +27,16 @@ module RuboCop
27
27
 
28
28
  node.each_descendant(:if).select(&:ternary?).each do |nested_ternary|
29
29
  add_offense(nested_ternary) do |corrector|
30
- if_node = if_node(nested_ternary)
31
- next if part_of_ignored_node?(if_node)
30
+ next if part_of_ignored_node?(node)
32
31
 
33
- autocorrect(corrector, if_node)
34
- ignore_node(if_node)
32
+ autocorrect(corrector, node)
33
+ ignore_node(node)
35
34
  end
36
35
  end
37
36
  end
38
37
 
39
38
  private
40
39
 
41
- def if_node(node)
42
- node = node.parent
43
- return node if node.if_type?
44
-
45
- if_node(node)
46
- end
47
-
48
40
  def autocorrect(corrector, if_node)
49
41
  replace_loc_and_whitespace(corrector, if_node.loc.question, "\n")
50
42
  replace_loc_and_whitespace(corrector, if_node.loc.colon, "\nelse\n")
@@ -121,7 +121,7 @@ module RuboCop
121
121
 
122
122
  def allowed_patterns
123
123
  # Convert the patterns to be anchored
124
- super.map { |regexp| Regexp.new(/\A#{regexp}\z/) }
124
+ super.map { |regexp| /\A#{regexp}\z/ }
125
125
  end
126
126
  end
127
127
  end
@@ -45,7 +45,7 @@ module RuboCop
45
45
  MSG = 'Avoid using `OpenStruct`; use `Struct`, `Hash`, a class or test doubles instead.'
46
46
 
47
47
  # @!method uses_open_struct?(node)
48
- def_node_matcher :uses_open_struct?, <<-PATTERN
48
+ def_node_matcher :uses_open_struct?, <<~PATTERN
49
49
  (const {nil? (cbase)} :OpenStruct)
50
50
  PATTERN
51
51
 
@@ -23,6 +23,7 @@ module RuboCop
23
23
  MSG = 'Redundant dot detected.'
24
24
  RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ ! != !~].freeze
25
25
 
26
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
26
27
  def on_send(node)
27
28
  return unless (dot = node.loc.dot)
28
29
  return if node.receiver.const_type? || !node.arguments.one?
@@ -33,8 +34,12 @@ module RuboCop
33
34
  add_offense(dot) do |corrector|
34
35
  wrap_in_parentheses_if_chained(corrector, node)
35
36
  corrector.replace(dot, ' ')
37
+
38
+ selector = node.loc.selector
39
+ corrector.insert_after(selector, ' ') if selector.end_pos == rhs.source_range.begin_pos
36
40
  end
37
41
  end
42
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
38
43
 
39
44
  private
40
45
 
@@ -54,6 +59,7 @@ module RuboCop
54
59
 
55
60
  def wrap_in_parentheses_if_chained(corrector, node)
56
61
  return unless node.parent&.call_type?
62
+ return if node.parent.first_argument == node
57
63
 
58
64
  operator = node.loc.selector
59
65
 
@@ -93,7 +93,7 @@ module RuboCop
93
93
  def contains_delimiter?(node, delimiters)
94
94
  delimiters_regexp = Regexp.union(delimiters)
95
95
 
96
- node.children.map { |n| string_source(n) }.compact.any?(delimiters_regexp)
96
+ node.children.filter_map { |n| string_source(n) }.any?(delimiters_regexp)
97
97
  end
98
98
 
99
99
  def string_source(node)
@@ -61,7 +61,7 @@ module RuboCop
61
61
  if style == :verbose
62
62
  "has_#{method_name}"
63
63
  else
64
- method_name.to_s.sub(/has_/, '')
64
+ method_name.to_s.delete_prefix('has_')
65
65
  end
66
66
  end
67
67
 
@@ -35,6 +35,8 @@ module RuboCop
35
35
  # array.join('')
36
36
  # [1, 2, 3].join("")
37
37
  # array.sum(0)
38
+ # exit(true)
39
+ # exit!(false)
38
40
  # string.split(" ")
39
41
  # "first\nsecond".split(" ")
40
42
  # string.chomp("\n")
@@ -45,6 +47,8 @@ module RuboCop
45
47
  # array.join
46
48
  # [1, 2, 3].join
47
49
  # array.sum
50
+ # exit
51
+ # exit!
48
52
  # string.split
49
53
  # "first second".split
50
54
  # string.chomp
@@ -55,9 +59,10 @@ module RuboCop
55
59
  extend AutoCorrector
56
60
 
57
61
  MSG = 'Argument %<arg>s is redundant because it is implied by default.'
62
+ NO_RECEIVER_METHODS = %i[exit exit!].freeze
58
63
 
59
64
  def on_send(node)
60
- return if node.receiver.nil?
65
+ return if !NO_RECEIVER_METHODS.include?(node.method_name) && node.receiver.nil?
61
66
  return if node.arguments.count != 1
62
67
  return unless redundant_argument?(node)
63
68
 
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for the instantiation of array using redundant `Array` constructor.
7
+ # Autocorrect replaces to array literal which is the simplest and fastest.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Array.new([])
13
+ # Array[]
14
+ # Array([])
15
+ # Array.new(['foo', 'foo', 'foo'])
16
+ # Array['foo', 'foo', 'foo']
17
+ # Array(['foo', 'foo', 'foo'])
18
+ #
19
+ # # good
20
+ # []
21
+ # ['foo', 'foo', 'foo']
22
+ # Array.new(3, 'foo')
23
+ # Array.new(3) { 'foo' }
24
+ #
25
+ class RedundantArrayConstructor < Base
26
+ extend AutoCorrector
27
+
28
+ MSG = 'Remove the redundant `Array` constructor.'
29
+
30
+ RESTRICT_ON_SEND = %i[new [] Array].freeze
31
+
32
+ # @!method redundant_array_constructor(node)
33
+ def_node_matcher :redundant_array_constructor, <<~PATTERN
34
+ {
35
+ (send
36
+ (const {nil? cbase} :Array) :new
37
+ $(array ...))
38
+ (send
39
+ (const {nil? cbase} :Array) :[]
40
+ $...)
41
+ (send
42
+ nil? :Array
43
+ $(array ...))
44
+ }
45
+ PATTERN
46
+
47
+ def on_send(node)
48
+ return unless (array_literal = redundant_array_constructor(node))
49
+
50
+ receiver = node.receiver
51
+ selector = node.loc.selector
52
+
53
+ if node.method?(:new)
54
+ range = receiver.source_range.join(selector)
55
+ replacement = array_literal
56
+ elsif node.method?(:Array)
57
+ range = selector
58
+ replacement = array_literal
59
+ else
60
+ range = receiver
61
+ replacement = selector.begin.join(node.source_range.end)
62
+ end
63
+
64
+ register_offense(range, node, replacement)
65
+ end
66
+
67
+ private
68
+
69
+ def register_offense(range, node, replacement)
70
+ add_offense(range) do |corrector|
71
+ corrector.replace(node, replacement.source)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -114,7 +114,7 @@ module RuboCop
114
114
  if node.parent&.assignment?
115
115
  replace_begin_with_statement(corrector, offense_range, node)
116
116
  else
117
- corrector.remove(offense_range)
117
+ remove_begin(corrector, offense_range, node)
118
118
  end
119
119
 
120
120
  if use_modifier_form_after_multiline_begin_block?(node)
@@ -136,6 +136,14 @@ module RuboCop
136
136
  restore_removed_comments(corrector, offense_range, node, first_child)
137
137
  end
138
138
 
139
+ def remove_begin(corrector, offense_range, node)
140
+ if node.parent.respond_to?(:endless?) && node.parent.endless?
141
+ offense_range = range_with_surrounding_space(offense_range, newlines: true)
142
+ end
143
+
144
+ corrector.remove(offense_range)
145
+ end
146
+
139
147
  # Restore comments that occur between "begin" and "first_child".
140
148
  # These comments will be moved to above the assignment line.
141
149
  def restore_removed_comments(corrector, offense_range, node, first_child)
@@ -146,7 +154,7 @@ module RuboCop
146
154
  end
147
155
 
148
156
  def use_modifier_form_after_multiline_begin_block?(node)
149
- return unless (parent = node.parent)
157
+ return false unless (parent = node.parent)
150
158
 
151
159
  node.multiline? && parent.if_type? && parent.modifier_form?
152
160
  end
@@ -63,26 +63,18 @@ module RuboCop
63
63
  RUBY
64
64
 
65
65
  def offense?(node)
66
- return if node.modifier_form?
66
+ return false if node.modifier_form?
67
67
 
68
68
  redundant_condition?(node) || redundant_condition_inverted?(node)
69
69
  end
70
70
 
71
71
  def replacement_condition(node)
72
72
  condition = node.condition.source
73
- expression = invert_expression?(node) ? "!(#{condition})" : condition
73
+ expression = redundant_condition_inverted?(node) ? "!(#{condition})" : condition
74
74
 
75
75
  node.elsif? ? indented_else_node(expression, node) : expression
76
76
  end
77
77
 
78
- def invert_expression?(node)
79
- (
80
- (node.if? || node.elsif? || node.ternary?) && redundant_condition_inverted?(node)
81
- ) || (
82
- node.unless? && redundant_condition?(node)
83
- )
84
- end
85
-
86
78
  def indented_else_node(expression, node)
87
79
  "else\n#{indentation(node)}#{expression}"
88
80
  end