rubocop 1.9.0 → 1.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -14
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +70 -17
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +5 -0
  7. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  8. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
  9. data/lib/rubocop/comment_config.rb +43 -94
  10. data/lib/rubocop/config.rb +4 -1
  11. data/lib/rubocop/cop/base.rb +1 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  14. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  15. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  16. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  17. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  18. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  21. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  22. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  23. data/lib/rubocop/cop/generator.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  25. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
  26. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  27. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  29. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  30. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  31. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  36. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  37. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
  38. data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
  39. data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
  40. data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
  41. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  42. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
  43. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
  46. data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
  47. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +20 -3
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  50. data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -7
  51. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
  52. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
  54. data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
  55. data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
  56. data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
  57. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  59. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -5
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +10 -5
  61. data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
  62. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  63. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  64. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  65. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  66. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  67. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  68. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  69. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  70. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  71. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  72. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  73. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  74. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  75. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  76. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  77. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  78. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  79. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  80. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  81. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  82. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  83. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  84. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  85. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  86. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  87. data/lib/rubocop/cop/lint/number_conversion.rb +11 -2
  88. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  89. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  90. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
  91. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  92. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  93. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  94. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  95. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  96. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  97. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  98. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  99. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  100. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  101. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  102. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  103. data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
  104. data/lib/rubocop/cop/lint/symbol_conversion.rb +91 -3
  105. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  106. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  107. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  108. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  109. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  110. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  111. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  112. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  113. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  114. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  115. data/lib/rubocop/cop/message_annotator.rb +4 -1
  116. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  117. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  118. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  119. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  120. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  121. data/lib/rubocop/cop/mixin/alignment.rb +10 -3
  122. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  123. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  124. data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
  125. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  126. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  127. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  128. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  129. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  130. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  131. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  132. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
  133. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  134. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  135. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -23
  136. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  137. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  138. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  139. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  140. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
  141. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  142. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  143. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  144. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
  145. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  146. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  147. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
  148. data/lib/rubocop/cop/registry.rb +10 -1
  149. data/lib/rubocop/cop/security/eval.rb +1 -0
  150. data/lib/rubocop/cop/security/json_load.rb +1 -0
  151. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  152. data/lib/rubocop/cop/security/open.rb +1 -0
  153. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  154. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -2
  155. data/lib/rubocop/cop/style/alias.rb +1 -0
  156. data/lib/rubocop/cop/style/and_or.rb +3 -1
  157. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  158. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  159. data/lib/rubocop/cop/style/array_join.rb +1 -0
  160. data/lib/rubocop/cop/style/attr.rb +1 -0
  161. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
  162. data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
  163. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  164. data/lib/rubocop/cop/style/case_like_if.rb +15 -4
  165. data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
  166. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  167. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  168. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  169. data/lib/rubocop/cop/style/commented_keyword.rb +10 -10
  170. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  171. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  172. data/lib/rubocop/cop/style/date_time.rb +3 -0
  173. data/lib/rubocop/cop/style/dir.rb +1 -0
  174. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  175. data/lib/rubocop/cop/style/documentation.rb +30 -3
  176. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  177. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  178. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  179. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  180. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  181. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  182. data/lib/rubocop/cop/style/eval_with_location.rb +90 -28
  183. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  184. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  185. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
  186. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  187. data/lib/rubocop/cop/style/float_division.rb +4 -0
  188. data/lib/rubocop/cop/style/format_string.rb +2 -0
  189. data/lib/rubocop/cop/style/format_string_token.rb +1 -0
  190. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +0 -3
  191. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  192. data/lib/rubocop/cop/style/hash_conversion.rb +108 -0
  193. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  194. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  195. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  196. data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
  197. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  198. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  199. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +37 -11
  200. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  201. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  202. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +46 -2
  203. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -3
  204. data/lib/rubocop/cop/style/min_max.rb +1 -0
  205. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  206. data/lib/rubocop/cop/style/module_function.rb +5 -0
  207. data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -3
  208. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  209. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  210. data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
  211. data/lib/rubocop/cop/style/nil_comparison.rb +6 -1
  212. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  213. data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
  214. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  215. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  216. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  217. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  218. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  219. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  220. data/lib/rubocop/cop/style/proc.rb +1 -0
  221. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  222. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  223. data/lib/rubocop/cop/style/redundant_begin.rb +44 -4
  224. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  225. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  226. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  227. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  228. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  229. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  230. data/lib/rubocop/cop/style/redundant_return.rb +4 -0
  231. data/lib/rubocop/cop/style/redundant_self.rb +7 -3
  232. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  233. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  234. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  235. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  236. data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
  237. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  238. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  239. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  240. data/lib/rubocop/cop/style/sample.rb +1 -0
  241. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  242. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  243. data/lib/rubocop/cop/style/single_line_methods.rb +4 -1
  244. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  245. data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -4
  246. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  247. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  248. data/lib/rubocop/cop/style/string_chars.rb +38 -0
  249. data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
  250. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  251. data/lib/rubocop/cop/style/strip.rb +1 -0
  252. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  253. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  254. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
  255. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
  256. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  257. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  258. data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
  259. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  260. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  261. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  262. data/lib/rubocop/cop/util.rb +4 -1
  263. data/lib/rubocop/directive_comment.rb +69 -9
  264. data/lib/rubocop/ext/regexp_parser.rb +3 -6
  265. data/lib/rubocop/formatter/clang_style_formatter.rb +4 -2
  266. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  267. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  268. data/lib/rubocop/formatter/tap_formatter.rb +4 -2
  269. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  270. data/lib/rubocop/magic_comment.rb +1 -1
  271. data/lib/rubocop/name_similarity.rb +1 -1
  272. data/lib/rubocop/target_finder.rb +1 -0
  273. data/lib/rubocop/target_ruby.rb +21 -13
  274. data/lib/rubocop/version.rb +1 -1
  275. metadata +14 -7
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # These can be replaced by their respective predicate methods.
9
9
  # The cop can also be configured to do the reverse.
10
10
  #
11
- # The cop disregards `#nonzero?` as it its value is truthy or falsey,
11
+ # The cop disregards `#nonzero?` as its value is truthy or falsey,
12
12
  # but not `true` and `false`, and thus not always interchangeable with
13
13
  # `!= 0`.
14
14
  #
@@ -115,14 +115,17 @@ module RuboCop
115
115
  end
116
116
  end
117
117
 
118
+ # @!method predicate(node)
118
119
  def_node_matcher :predicate, <<~PATTERN
119
120
  (send $(...) ${:zero? :positive? :negative?})
120
121
  PATTERN
121
122
 
123
+ # @!method comparison(node)
122
124
  def_node_matcher :comparison, <<~PATTERN
123
125
  (send [$(...) !gvar_type?] ${:== :> :<} (int 0))
124
126
  PATTERN
125
127
 
128
+ # @!method inverted_comparison(node)
126
129
  def_node_matcher :inverted_comparison, <<~PATTERN
127
130
  (send (int 0) ${:== :> :<} [$(...) !gvar_type?])
128
131
  PATTERN
@@ -22,6 +22,7 @@ module RuboCop
22
22
  class OptionHash < Base
23
23
  MSG = 'Prefer keyword arguments to options hashes.'
24
24
 
25
+ # @!method option_hash(node)
25
26
  def_node_matcher :option_hash, <<~PATTERN
26
27
  (args ... $(optarg [#suspicious_name? _] (hash)))
27
28
  PATTERN
@@ -31,6 +31,7 @@ module RuboCop
31
31
 
32
32
  MSG = 'Use the double pipe equals operator `||=` instead.'
33
33
 
34
+ # @!method ternary_assignment?(node)
34
35
  def_node_matcher :ternary_assignment?, <<~PATTERN
35
36
  ({lvasgn ivasgn cvasgn gvasgn} _var
36
37
  (if
@@ -39,6 +40,7 @@ module RuboCop
39
40
  $_))
40
41
  PATTERN
41
42
 
43
+ # @!method unless_assignment?(node)
42
44
  def_node_matcher :unless_assignment?, <<~PATTERN
43
45
  (if
44
46
  ({lvar ivar cvar gvar} _var) nil?
@@ -115,6 +115,7 @@ module RuboCop
115
115
  end
116
116
  end
117
117
 
118
+ # @!method implicit_self_getter?(node)
118
119
  def_node_matcher :implicit_self_getter?, '(send nil? $_)'
119
120
 
120
121
  # Helper class necessitated by silly design of TSort prior to Ruby 2.1
@@ -123,8 +124,13 @@ module RuboCop
123
124
  include TSort
124
125
  extend RuboCop::NodePattern::Macros
125
126
 
127
+ # @!method var_name(node)
126
128
  def_node_matcher :var_name, '{(casgn _ $_) (_ $_)}'
129
+
130
+ # @!method uses_var?(node)
127
131
  def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}'
132
+
133
+ # @!method matching_calls(node, receiver, method_name)
128
134
  def_node_search :matching_calls, '(send %1 %2 $...)'
129
135
 
130
136
  def initialize(assignments)
@@ -71,6 +71,7 @@ module RuboCop
71
71
 
72
72
  private
73
73
 
74
+ # @!method control_op_condition(node)
74
75
  def_node_matcher :control_op_condition, <<~PATTERN
75
76
  (begin $_ ...)
76
77
  PATTERN
@@ -18,6 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `proc` instead of `Proc.new`.'
20
20
 
21
+ # @!method proc_new?(node)
21
22
  def_node_matcher :proc_new?,
22
23
  '(block $(send (const {nil? cbase} :Proc) :new) ...)'
23
24
 
@@ -30,6 +30,7 @@ module RuboCop
30
30
  'integers with offsets.'
31
31
  RESTRICT_ON_SEND = %i[+ - succ pred next].freeze
32
32
 
33
+ # @!method integer_op_rand?(node)
33
34
  def_node_matcher :integer_op_rand?, <<~PATTERN
34
35
  (send
35
36
  int {:+ :-}
@@ -39,6 +40,7 @@ module RuboCop
39
40
  {int (irange int int) (erange int int)}))
40
41
  PATTERN
41
42
 
43
+ # @!method rand_op_integer?(node)
42
44
  def_node_matcher :rand_op_integer?, <<~PATTERN
43
45
  (send
44
46
  (send
@@ -49,6 +51,7 @@ module RuboCop
49
51
  int)
50
52
  PATTERN
51
53
 
54
+ # @!method rand_modified?(node)
52
55
  def_node_matcher :rand_modified?, <<~PATTERN
53
56
  (send
54
57
  (send
@@ -71,6 +74,7 @@ module RuboCop
71
74
 
72
75
  private
73
76
 
77
+ # @!method random_call(node)
74
78
  def_node_matcher :random_call, <<~PATTERN
75
79
  {(send (send $_ _ $_) ...)
76
80
  (send _ _ (send $_ _ $_))}
@@ -144,6 +148,7 @@ module RuboCop
144
148
  end
145
149
  end
146
150
 
151
+ # @!method to_int(node)
147
152
  def_node_matcher :to_int, <<~PATTERN
148
153
  (int $_)
149
154
  PATTERN
@@ -42,6 +42,7 @@ module RuboCop
42
42
 
43
43
  MSG = 'Redundant assignment before returning detected.'
44
44
 
45
+ # @!method redundant_assignment?(node)
45
46
  def_node_matcher :redundant_assignment?, <<~PATTERN
46
47
  (... $(lvasgn _name _expression) (lvar _name))
47
48
  PATTERN
@@ -63,6 +63,7 @@ module RuboCop
63
63
  # end
64
64
  # end
65
65
  class RedundantBegin < Base
66
+ include RangeHelp
66
67
  extend AutoCorrector
67
68
 
68
69
  MSG = 'Redundant `begin` block detected.'
@@ -85,7 +86,9 @@ module RuboCop
85
86
  end
86
87
 
87
88
  def on_kwbegin(node)
88
- return if contain_rescue_or_ensure?(node) || valid_context_using_only_begin?(node)
89
+ return if empty_begin?(node) ||
90
+ contain_rescue_or_ensure?(node) ||
91
+ valid_context_using_only_begin?(node)
89
92
 
90
93
  register_offense(node)
91
94
  end
@@ -93,12 +96,41 @@ module RuboCop
93
96
  private
94
97
 
95
98
  def register_offense(node)
96
- add_offense(node.loc.begin) do |corrector|
97
- corrector.remove(node.loc.begin)
99
+ offense_range = node.loc.begin
100
+
101
+ add_offense(offense_range) do |corrector|
102
+ if any_ancestor_assignment_node?(node)
103
+ replace_begin_with_statement(corrector, offense_range, node)
104
+ else
105
+ corrector.remove(offense_range)
106
+ end
107
+
98
108
  corrector.remove(node.loc.end)
99
109
  end
100
110
  end
101
111
 
112
+ def replace_begin_with_statement(corrector, offense_range, node)
113
+ first_child = node.children.first
114
+
115
+ corrector.replace(offense_range, first_child.source)
116
+ corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
117
+
118
+ restore_removed_comments(corrector, offense_range, node, first_child)
119
+ end
120
+
121
+ # Restore comments that occur between "begin" and "first_child".
122
+ # These comments will be moved to above the assignment line.
123
+ def restore_removed_comments(corrector, offense_range, node, first_child)
124
+ comments_range = range_between(offense_range.end_pos, first_child.source_range.begin_pos)
125
+ comments = comments_range.source
126
+
127
+ corrector.insert_before(node.parent, comments) unless comments.blank?
128
+ end
129
+
130
+ def empty_begin?(node)
131
+ node.children.empty?
132
+ end
133
+
102
134
  def contain_rescue_or_ensure?(node)
103
135
  first_child = node.children.first
104
136
 
@@ -108,9 +140,17 @@ module RuboCop
108
140
  def valid_context_using_only_begin?(node)
109
141
  parent = node.parent
110
142
 
111
- node.each_ancestor.any?(&:assignment?) || parent&.post_condition_loop? ||
143
+ valid_begin_assignment?(node) || parent&.post_condition_loop? ||
112
144
  parent&.send_type? || parent&.operator_keyword?
113
145
  end
146
+
147
+ def valid_begin_assignment?(node)
148
+ any_ancestor_assignment_node?(node) && !node.children.one?
149
+ end
150
+
151
+ def any_ancestor_assignment_node?(node)
152
+ node.each_ancestor.any?(&:assignment?)
153
+ end
114
154
  end
115
155
  end
116
156
  end
@@ -53,10 +53,12 @@ module RuboCop
53
53
  format(MSG, msg: msg)
54
54
  end
55
55
 
56
+ # @!method redundant_condition?(node)
56
57
  def_node_matcher :redundant_condition?, <<~RUBY
57
58
  (if (send _ #{COMPARISON_OPERATOR_MATCHER} _) true false)
58
59
  RUBY
59
60
 
61
+ # @!method redundant_condition_inverted?(node)
60
62
  def_node_matcher :redundant_condition_inverted?, <<~RUBY
61
63
  (if (send _ #{COMPARISON_OPERATOR_MATCHER} _) false true)
62
64
  RUBY
@@ -53,10 +53,12 @@ module RuboCop
53
53
  end
54
54
  end
55
55
 
56
+ # @!method exploded?(node)
56
57
  def_node_matcher :exploded?, <<~PATTERN
57
58
  (send nil? ${:raise :fail} (const {nil? cbase} :RuntimeError) $_)
58
59
  PATTERN
59
60
 
61
+ # @!method compact?(node)
60
62
  def_node_matcher :compact?, <<~PATTERN
61
63
  (send nil? {:raise :fail} $(send (const {nil? cbase} :RuntimeError) :new $_))
62
64
  PATTERN
@@ -38,6 +38,7 @@ module RuboCop
38
38
 
39
39
  MSG = 'Use `%<good>s` instead of `%<bad>s`.'
40
40
 
41
+ # @!method redundant_fetch_block_candidate?(node)
41
42
  def_node_matcher :redundant_fetch_block_candidate?, <<~PATTERN
42
43
  (block
43
44
  $(send _ :fetch _)
@@ -78,6 +79,7 @@ module RuboCop
78
79
  rails_cache?(send.receiver)
79
80
  end
80
81
 
82
+ # @!method rails_cache?(node)
81
83
  def_node_matcher :rails_cache?, <<~PATTERN
82
84
  (send (const _ :Rails) :cache)
83
85
  PATTERN
@@ -30,6 +30,7 @@ module RuboCop
30
30
  MSG = 'Redundant `.rb` file extension detected.'
31
31
  RESTRICT_ON_SEND = %i[require require_relative].freeze
32
32
 
33
+ # @!method require_call?(node)
33
34
  def_node_matcher :require_call?, <<~PATTERN
34
35
  (send nil? {:require :require_relative} $str_type?)
35
36
  PATTERN
@@ -53,6 +53,7 @@ module RuboCop
53
53
  end
54
54
  end
55
55
 
56
+ # @!method operation_produces_immutable_object?(node)
56
57
  def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
57
58
  {
58
59
  (begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
@@ -17,11 +17,20 @@ module RuboCop
17
17
  include Parentheses
18
18
  extend AutoCorrector
19
19
 
20
+ # @!method square_brackets?(node)
20
21
  def_node_matcher :square_brackets?,
21
22
  '(send {(send _recv _msg) str array hash} :[] ...)'
23
+
24
+ # @!method range_end?(node)
22
25
  def_node_matcher :range_end?, '^^{irange erange}'
26
+
27
+ # @!method method_node_and_args(node)
23
28
  def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
29
+
30
+ # @!method rescue?(node)
24
31
  def_node_matcher :rescue?, '{^resbody ^^resbody}'
32
+
33
+ # @!method arg_in_call_with_block?(node)
25
34
  def_node_matcher :arg_in_call_with_block?,
26
35
  '^^(block (send _ _ equal?(%0) ...) ...)'
27
36
 
@@ -109,6 +118,7 @@ module RuboCop
109
118
  check_send(begin_node, node) if node.call_type?
110
119
  end
111
120
 
121
+ # @!method interpolation?(node)
112
122
  def_node_matcher :interpolation?, '[^begin ^^dstr]'
113
123
 
114
124
  def check_send(begin_node, node)
@@ -220,14 +230,17 @@ module RuboCop
220
230
  first_yield_argument?(node)
221
231
  end
222
232
 
233
+ # @!method first_send_argument?(node)
223
234
  def_node_matcher :first_send_argument?, <<~PATTERN
224
235
  ^(send _ _ equal?(%0) ...)
225
236
  PATTERN
226
237
 
238
+ # @!method first_super_argument?(node)
227
239
  def_node_matcher :first_super_argument?, <<~PATTERN
228
240
  ^(super equal?(%0) ...)
229
241
  PATTERN
230
242
 
243
+ # @!method first_yield_argument?(node)
231
244
  def_node_matcher :first_yield_argument?, <<~PATTERN
232
245
  ^(yield equal?(%0) ...)
233
246
  PATTERN
@@ -71,6 +71,10 @@ module RuboCop
71
71
  elsif hash_without_braces?(return_node.first_argument)
72
72
  add_braces(corrector, return_node.first_argument)
73
73
  end
74
+ if return_node.splat_argument?
75
+ first_argument = return_node.first_argument
76
+ corrector.replace(first_argument, first_argument.source.gsub(/\A\*/, ''))
77
+ end
74
78
 
75
79
  keyword = range_with_surrounding_space(range: return_node.loc.keyword,
76
80
  side: :right)
@@ -11,7 +11,7 @@ module RuboCop
11
11
  # presence of a method name clash with an argument or a local
12
12
  # variable.
13
13
  #
14
- # * Calling an attribute writer to prevent an local variable assignment.
14
+ # * Calling an attribute writer to prevent a local variable assignment.
15
15
  #
16
16
  # Note, with using explicit self you can only send messages with public or
17
17
  # protected scope, you cannot send private messages this way.
@@ -144,8 +144,12 @@ module RuboCop
144
144
  end
145
145
 
146
146
  def on_argument(node)
147
- name, = *node
148
- @local_variables_scopes[node] << name
147
+ if node.mlhs_type?
148
+ on_args(node)
149
+ else
150
+ name, = *node
151
+ @local_variables_scopes[node] << name
152
+ end
149
153
  end
150
154
 
151
155
  def allow_self(node)
@@ -82,6 +82,7 @@ module RuboCop
82
82
  METHODS_RETURNING_SELF.include?(method_name)
83
83
  end
84
84
 
85
+ # @!method redundant_self_assignment?(node, method_name)
85
86
  def_node_matcher :redundant_self_assignment?, <<~PATTERN
86
87
  (send
87
88
  (self) _
@@ -91,6 +92,7 @@ module RuboCop
91
92
  ...))
92
93
  PATTERN
93
94
 
95
+ # @!method redundant_nonself_assignment?(node, receiver, method_name)
94
96
  def_node_matcher :redundant_nonself_assignment?, <<~PATTERN
95
97
  (send
96
98
  %1 _
@@ -58,6 +58,7 @@ module RuboCop
58
58
 
59
59
  RESTRICT_ON_SEND = %i[sort sort_by].freeze
60
60
 
61
+ # @!method redundant_sort?(node)
61
62
  def_node_matcher :redundant_sort?, <<~MATCHER
62
63
  {
63
64
  (send $(send _ $:sort ...) ${:last :first})
@@ -21,6 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
23
23
 
24
+ # @!method redundant_sort_by(node)
24
25
  def_node_matcher :redundant_sort_by, <<~PATTERN
25
26
  (block $(send _ :sort_by) (args (arg $_x)) (lvar _x))
26
27
  PATTERN
@@ -150,7 +150,7 @@ module RuboCop
150
150
 
151
151
  def preferred_delimiters
152
152
  config.for_cop('Style/PercentLiteralDelimiters') \
153
- ['PreferredDelimiters']['%r'].split(//)
153
+ ['PreferredDelimiters']['%r'].chars
154
154
  end
155
155
 
156
156
  def correct_delimiters(node, corrector)
@@ -39,23 +39,23 @@ module RuboCop
39
39
  # rescue SomeException
40
40
  # handle_error
41
41
  # end
42
- class RescueModifier < Cop
42
+ class RescueModifier < Base
43
43
  include Alignment
44
+ include RangeHelp
44
45
  include RescueNode
46
+ extend AutoCorrector
45
47
 
46
48
  MSG = 'Avoid using `rescue` in its modifier form.'
47
49
 
48
50
  def on_resbody(node)
49
51
  return unless rescue_modifier?(node)
50
52
 
51
- add_offense(node.parent)
52
- end
53
+ rescue_node = node.parent
54
+ add_offense(rescue_node) do |corrector|
55
+ parenthesized = parenthesized?(rescue_node)
53
56
 
54
- def autocorrect(node)
55
- parenthesized = parenthesized?(node)
56
- lambda do |corrector|
57
- corrector.replace(node, corrected_block(node, parenthesized))
58
- ParenthesesCorrector.correct(corrector, node.parent) if parenthesized
57
+ correct_rescue_block(corrector, rescue_node, parenthesized)
58
+ ParenthesesCorrector.correct(corrector, rescue_node.parent) if parenthesized
59
59
  end
60
60
  end
61
61
 
@@ -65,17 +65,20 @@ module RuboCop
65
65
  node.parent && parentheses?(node.parent)
66
66
  end
67
67
 
68
- def corrected_block(node, parenthesized)
68
+ def correct_rescue_block(corrector, node, parenthesized)
69
69
  operation, rescue_modifier, = *node
70
70
  *_, rescue_args = *rescue_modifier
71
71
 
72
72
  node_indentation, node_offset = indentation_and_offset(node, parenthesized)
73
73
 
74
- "begin\n" \
75
- "#{operation.source.gsub(/^/, node_indentation)}" \
76
- "\n#{node_offset}rescue\n" \
77
- "#{rescue_args.source.gsub(/^/, node_indentation)}" \
78
- "\n#{node_offset}end"
74
+ corrector.remove(range_between(operation.source_range.end_pos, node.source_range.end_pos))
75
+ corrector.insert_before(operation, "begin\n#{node_indentation}")
76
+ corrector.insert_after(operation, <<~RESCUE_CLAUSE.chop)
77
+
78
+ #{node_offset}rescue
79
+ #{node_indentation}#{rescue_args.source}
80
+ #{node_offset}end
81
+ RESCUE_CLAUSE
79
82
  end
80
83
 
81
84
  def indentation_and_offset(node, parenthesized)