rubocop 0.49.1 → 0.50.0

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 (292) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/bin/rubocop +1 -1
  4. data/config/default.yml +160 -99
  5. data/config/disabled.yml +4 -5
  6. data/config/enabled.yml +149 -47
  7. data/lib/rubocop.rb +41 -14
  8. data/lib/rubocop/ast/builder.rb +4 -1
  9. data/lib/rubocop/ast/node.rb +36 -42
  10. data/lib/rubocop/ast/node/args_node.rb +1 -13
  11. data/lib/rubocop/ast/node/array_node.rb +9 -0
  12. data/lib/rubocop/ast/node/block_node.rb +9 -0
  13. data/lib/rubocop/ast/node/def_node.rb +71 -0
  14. data/lib/rubocop/ast/node/for_node.rb +8 -0
  15. data/lib/rubocop/ast/node/if_node.rb +10 -2
  16. data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
  17. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
  18. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
  19. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
  20. data/lib/rubocop/ast/node/regexp_node.rb +35 -0
  21. data/lib/rubocop/ast/node/send_node.rb +1 -154
  22. data/lib/rubocop/ast/node/super_node.rb +3 -24
  23. data/lib/rubocop/ast/node/yield_node.rb +21 -0
  24. data/lib/rubocop/ast/traversal.rb +6 -6
  25. data/lib/rubocop/cli.rb +7 -3
  26. data/lib/rubocop/config.rb +45 -8
  27. data/lib/rubocop/config_loader.rb +7 -5
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
  31. data/lib/rubocop/cop/commissioner.rb +8 -2
  32. data/lib/rubocop/cop/cop.rb +3 -1
  33. data/lib/rubocop/cop/generator.rb +94 -21
  34. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
  36. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
  37. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
  38. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
  39. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  40. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
  41. data/lib/rubocop/cop/layout/align_array.rb +2 -2
  42. data/lib/rubocop/cop/layout/align_hash.rb +2 -2
  43. data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  46. data/lib/rubocop/cop/layout/dot_position.rb +9 -0
  47. data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
  49. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
  50. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
  51. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
  52. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
  54. data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
  55. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
  56. data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
  57. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
  58. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
  59. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
  61. data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
  62. data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
  63. data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
  64. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  65. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
  66. data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
  67. data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
  68. data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
  69. data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
  70. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
  71. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  72. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  73. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
  74. data/lib/rubocop/cop/layout/tab.rb +1 -1
  75. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
  76. data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
  77. data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
  78. data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
  79. data/lib/rubocop/cop/lint/condition_position.rb +5 -1
  80. data/lib/rubocop/cop/lint/debugger.rb +16 -9
  81. data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
  82. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
  83. data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
  84. data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
  85. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  86. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  87. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  88. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  89. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  90. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  91. data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
  92. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
  93. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  94. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
  95. data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
  96. data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
  97. data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
  98. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  99. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  100. data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
  101. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  102. data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
  103. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  104. data/lib/rubocop/cop/lint/rand_one.rb +7 -1
  105. data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
  106. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  107. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  108. data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
  109. data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
  110. data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
  111. data/lib/rubocop/cop/lint/script_permission.rb +6 -0
  112. data/lib/rubocop/cop/lint/syntax.rb +17 -20
  113. data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
  114. data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
  115. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  116. data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
  117. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
  118. data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
  119. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
  120. data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
  121. data/lib/rubocop/cop/lint/void.rb +29 -23
  122. data/lib/rubocop/cop/metrics/line_length.rb +2 -2
  123. data/lib/rubocop/cop/metrics/method_length.rb +8 -3
  124. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  125. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
  126. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
  127. data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
  128. data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
  129. data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
  130. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
  131. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
  132. data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
  133. data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
  134. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  135. data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
  136. data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
  137. data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
  138. data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
  139. data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
  140. data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
  141. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
  142. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
  143. data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
  144. data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
  145. data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
  146. data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
  147. data/lib/rubocop/cop/performance/caller.rb +39 -11
  148. data/lib/rubocop/cop/performance/casecmp.rb +4 -4
  149. data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
  150. data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
  151. data/lib/rubocop/cop/performance/end_with.rb +3 -3
  152. data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
  153. data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
  154. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  155. data/lib/rubocop/cop/performance/range_include.rb +2 -2
  156. data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
  157. data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
  158. data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
  159. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
  160. data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
  161. data/lib/rubocop/cop/performance/size.rb +1 -1
  162. data/lib/rubocop/cop/performance/start_with.rb +3 -3
  163. data/lib/rubocop/cop/performance/times_map.rb +23 -12
  164. data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
  165. data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
  166. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  167. data/lib/rubocop/cop/rails/delegate.rb +36 -7
  168. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  169. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
  170. data/lib/rubocop/cop/rails/file_path.rb +3 -4
  171. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  172. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
  173. data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
  174. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  175. data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
  176. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  177. data/lib/rubocop/cop/rails/request_referer.rb +2 -2
  178. data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
  179. data/lib/rubocop/cop/rails/save_bang.rb +8 -6
  180. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  181. data/lib/rubocop/cop/security/eval.rb +2 -2
  182. data/lib/rubocop/cop/security/json_load.rb +2 -2
  183. data/lib/rubocop/cop/security/marshal_load.rb +2 -2
  184. data/lib/rubocop/cop/security/yaml_load.rb +2 -2
  185. data/lib/rubocop/cop/style/alias.rb +44 -20
  186. data/lib/rubocop/cop/style/and_or.rb +48 -34
  187. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
  188. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  189. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  190. data/lib/rubocop/cop/style/command_literal.rb +20 -9
  191. data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
  192. data/lib/rubocop/cop/style/copyright.rb +10 -10
  193. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
  194. data/lib/rubocop/cop/style/dir.rb +52 -0
  195. data/lib/rubocop/cop/style/documentation_method.rb +2 -6
  196. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  197. data/lib/rubocop/cop/style/empty_else.rb +3 -2
  198. data/lib/rubocop/cop/style/empty_literal.rb +1 -2
  199. data/lib/rubocop/cop/style/empty_method.rb +27 -17
  200. data/lib/rubocop/cop/style/flip_flop.rb +2 -2
  201. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  202. data/lib/rubocop/cop/style/guard_clause.rb +4 -2
  203. data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
  204. data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
  205. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
  206. data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
  207. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  208. data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
  209. data/lib/rubocop/cop/style/lambda.rb +19 -9
  210. data/lib/rubocop/cop/style/lambda_call.rb +22 -1
  211. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
  212. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
  213. data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
  214. data/lib/rubocop/cop/style/method_missing.rb +5 -18
  215. data/lib/rubocop/cop/style/min_max.rb +67 -0
  216. data/lib/rubocop/cop/style/missing_else.rb +16 -3
  217. data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
  218. data/lib/rubocop/cop/style/module_function.rb +8 -4
  219. data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
  220. data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
  221. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  222. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  223. data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
  224. data/lib/rubocop/cop/style/negated_if.rb +8 -4
  225. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
  226. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  227. data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
  228. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
  229. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  230. data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
  231. data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
  232. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  233. data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
  234. data/lib/rubocop/cop/style/or_assignment.rb +88 -0
  235. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  236. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
  237. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  238. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  239. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  240. data/lib/rubocop/cop/style/proc.rb +1 -1
  241. data/lib/rubocop/cop/style/raise_args.rb +16 -17
  242. data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
  243. data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
  244. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  245. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
  246. data/lib/rubocop/cop/style/redundant_return.rb +23 -11
  247. data/lib/rubocop/cop/style/redundant_self.rb +18 -9
  248. data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
  249. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  250. data/lib/rubocop/cop/style/return_nil.rb +98 -0
  251. data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
  252. data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
  253. data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
  254. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  255. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
  256. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
  257. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  258. data/lib/rubocop/cop/style/symbol_array.rb +5 -25
  259. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  260. data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
  261. data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
  262. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
  263. data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
  264. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  265. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  266. data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
  267. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  268. data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
  269. data/lib/rubocop/cop/style/word_array.rb +7 -24
  270. data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
  271. data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
  272. data/lib/rubocop/cop/team.rb +16 -8
  273. data/lib/rubocop/cop/util.rb +11 -0
  274. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  275. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  276. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  277. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  278. data/lib/rubocop/formatter/tap_formatter.rb +71 -0
  279. data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
  280. data/lib/rubocop/node_pattern.rb +44 -26
  281. data/lib/rubocop/options.rb +1 -0
  282. data/lib/rubocop/processed_source.rb +3 -1
  283. data/lib/rubocop/remote_config.rb +5 -1
  284. data/lib/rubocop/result_cache.rb +1 -0
  285. data/lib/rubocop/rspec/cop_helper.rb +10 -10
  286. data/lib/rubocop/rspec/expect_offense.rb +6 -8
  287. data/lib/rubocop/rspec/shared_examples.rb +8 -8
  288. data/lib/rubocop/string_util.rb +2 -0
  289. data/lib/rubocop/version.rb +1 -1
  290. metadata +51 -18
  291. data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
  292. data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -33,7 +33,7 @@ module RuboCop
33
33
  def on_percent_literal(node)
34
34
  return unless contains_colons_or_commas?(node)
35
35
 
36
- add_offense(node, :expression, MSG)
36
+ add_offense(node)
37
37
  end
38
38
 
39
39
  private
@@ -31,7 +31,13 @@ module RuboCop
31
31
  def on_send(node)
32
32
  return unless rand_one?(node)
33
33
 
34
- add_offense(node, :expression, format(MSG, node.source))
34
+ add_offense(node)
35
+ end
36
+
37
+ private
38
+
39
+ def message(node)
40
+ format(MSG, node.source)
35
41
  end
36
42
  end
37
43
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for redundant `with_index`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # ary.each_with_index do |v|
11
+ # v
12
+ # end
13
+ #
14
+ # # good
15
+ # ary.each do |v|
16
+ # v
17
+ # end
18
+ #
19
+ # # bad
20
+ # ary.each.with_index do |v|
21
+ # v
22
+ # end
23
+ #
24
+ # # good
25
+ # ary.each do |v|
26
+ # v
27
+ # end
28
+ #
29
+ class RedundantWithIndex < Cop
30
+ MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'.freeze
31
+ MSG_WITH_INDEX = 'Remove redundant `with_index`.'.freeze
32
+
33
+ def_node_matcher :redundant_with_index?, <<-PATTERN
34
+ (block
35
+ $(send
36
+ _ {:each_with_index :with_index})
37
+ (args
38
+ (arg _))
39
+ ...)
40
+ PATTERN
41
+
42
+ def on_block(node)
43
+ redundant_with_index?(node) do |send|
44
+ add_offense(node, with_index_range(send))
45
+ end
46
+ end
47
+
48
+ def autocorrect(node)
49
+ lambda do |corrector|
50
+ redundant_with_index?(node) do |send|
51
+ if send.method_name == :each_with_index
52
+ corrector.replace(send.loc.selector, 'each')
53
+ else
54
+ corrector.remove(send.loc.selector)
55
+ corrector.remove(send.loc.dot)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def message(node)
64
+ if node.method_name == :each_with_index
65
+ MSG_EACH_WITH_INDEX
66
+ else
67
+ MSG_WITH_INDEX
68
+ end
69
+ end
70
+
71
+ def with_index_range(send)
72
+ range_between(send.loc.selector.begin_pos, send.loc.selector.end_pos)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -53,7 +53,7 @@ module RuboCop
53
53
  def check_predicate(predicate, node)
54
54
  return unless predicate.operator_keyword?
55
55
 
56
- add_offense(node, :expression)
56
+ add_offense(node)
57
57
  end
58
58
  end
59
59
  end
@@ -33,7 +33,7 @@ module RuboCop
33
33
  rescue_args = node.children.first.children
34
34
  return unless rescue_args.any? { |a| targets_exception?(a) }
35
35
 
36
- add_offense(node, :expression)
36
+ add_offense(node)
37
37
  end
38
38
 
39
39
  def targets_exception?(rescue_arg_node)
@@ -53,22 +53,29 @@ module RuboCop
53
53
  format(MSG, invalid_exceptions.map(&:source).join(', ')))
54
54
  end
55
55
 
56
+ private
57
+
56
58
  def autocorrect(node)
57
59
  rescued, _, _body = *node
58
- exceptions = *rescued
59
- valid_exceptions = exceptions - invalid_exceptions(exceptions)
60
- correction = valid_exceptions.map(&:source).join(', ')
61
- correction = " #{correction}" unless correction.empty?
62
60
  range = Parser::Source::Range.new(node.loc.expression,
63
61
  node.loc.keyword.end_pos,
64
62
  rescued.loc.expression.end_pos)
65
63
 
66
64
  lambda do |corrector|
67
- corrector.replace(range, correction)
65
+ corrector.replace(range, correction(*rescued))
68
66
  end
69
67
  end
70
68
 
71
- private
69
+ def correction(*exceptions)
70
+ correction = valid_exceptions(exceptions).map(&:source).join(', ')
71
+ correction = " #{correction}" unless correction.empty?
72
+
73
+ correction
74
+ end
75
+
76
+ def valid_exceptions(exceptions)
77
+ exceptions - invalid_exceptions(exceptions)
78
+ end
72
79
 
73
80
  def invalid_exceptions(exceptions)
74
81
  exceptions.select do |exception|
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for uses of `rescue` with no error class specified.
7
+ #
8
+ # @example
9
+ #
10
+ # # good
11
+ # begin
12
+ # foo
13
+ # rescue BarError
14
+ # bar
15
+ # end
16
+ #
17
+ # # bad
18
+ # begin
19
+ # foo
20
+ # rescue
21
+ # bar
22
+ # end
23
+ class RescueWithoutErrorClass < Cop
24
+ MSG = 'Avoid rescuing without specifying an error class.'.freeze
25
+
26
+ def_node_matcher :rescue_without_error_class?, <<-PATTERN
27
+ (resbody nil ...)
28
+ PATTERN
29
+
30
+ def on_resbody(node)
31
+ return unless rescue_without_error_class?(node)
32
+
33
+ add_offense(node, :keyword)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for the use of a return with a value in a context
7
+ # where the value will be ignored. (initialize and setter methods)
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # def initialize
13
+ # foo
14
+ # return :qux if bar?
15
+ # baz
16
+ # end
17
+ #
18
+ # def foo=(bar)
19
+ # return 42
20
+ # end
21
+ #
22
+ # @example
23
+ #
24
+ # # good
25
+ # def initialize
26
+ # foo
27
+ # return if bar?
28
+ # baz
29
+ # end
30
+ #
31
+ # def foo=(bar)
32
+ # return
33
+ # end
34
+ class ReturnInVoidContext < Cop
35
+ MSG = 'Do not return a value in `%s`.'.freeze
36
+ def on_return(return_node)
37
+ method_name = method_name(return_node)
38
+ return unless method_name && return_node.descendants.any? &&
39
+ useless_return_method?(method_name)
40
+
41
+ add_offense(return_node, :keyword, format(message, method_name))
42
+ end
43
+
44
+ private
45
+
46
+ def method_name(return_node)
47
+ method_node = return_node.each_ancestor(:block, :def, :defs).first
48
+ return nil unless method_node.def_type?
49
+ method_node.children.first
50
+ end
51
+
52
+ def method_setter?(method_name)
53
+ method_name.to_s.end_with?('=') &&
54
+ !AST::Node::COMPARISON_OPERATORS.include?(method_name)
55
+ end
56
+
57
+ def useless_return_method?(method_name)
58
+ method_name == :initialize || method_setter?(method_name)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -18,6 +18,12 @@ module RuboCop
18
18
  add_offense(comment, :expression, message)
19
19
  end
20
20
 
21
+ def autocorrect(node)
22
+ lambda do |_corrector|
23
+ FileUtils.chmod('+x', node.loc.expression.source_buffer.name)
24
+ end
25
+ end
26
+
21
27
  private
22
28
 
23
29
  def start_with_shebang?(processed_source)
@@ -5,50 +5,47 @@ module RuboCop
5
5
  module Lint
6
6
  # This is actually not a cop and inspects nothing. It just provides
7
7
  # methods to repack Parser's diagnostics/errors into RuboCop's offenses.
8
- module Syntax
8
+ class Syntax < Cop
9
9
  PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
10
10
  :end_pos)
11
11
 
12
- COP_NAME = 'Syntax'.freeze
13
12
  ERROR_SOURCE_RANGE = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
14
13
 
15
- def self.offenses_from_processed_source(processed_source)
16
- offenses = []
14
+ def self.offenses_from_processed_source(processed_source,
15
+ config, options)
16
+ cop = new(config, options)
17
17
 
18
18
  if processed_source.parser_error
19
- offenses << offense_from_error(processed_source.parser_error)
19
+ cop.add_offense_from_error(processed_source.parser_error)
20
20
  end
21
21
 
22
22
  processed_source.diagnostics.each do |diagnostic|
23
- offenses << offense_from_diagnostic(diagnostic,
24
- processed_source.ruby_version)
23
+ cop.add_offense_from_diagnostic(diagnostic,
24
+ processed_source.ruby_version)
25
25
  end
26
26
 
27
- offenses
27
+ cop.offenses
28
28
  end
29
29
 
30
- def self.offense_from_diagnostic(diagnostic, ruby_version)
31
- Offense.new(
32
- diagnostic.level,
33
- diagnostic.location,
30
+ def add_offense_from_diagnostic(diagnostic, ruby_version)
31
+ message =
34
32
  "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
35
- 'configure using `TargetRubyVersion` parameter, under `AllCops`)',
36
- COP_NAME
37
- )
33
+ 'configure using `TargetRubyVersion` parameter, under `AllCops`)'
34
+ add_offense(nil, diagnostic.location, message, diagnostic.level)
38
35
  end
39
36
 
40
- def self.offense_from_error(error)
37
+ def add_offense_from_error(error)
41
38
  message = beautify_message(error.message)
42
- Offense.new(:fatal, ERROR_SOURCE_RANGE, message, COP_NAME)
39
+ add_offense(nil, ERROR_SOURCE_RANGE, message, :fatal)
43
40
  end
44
41
 
45
- def self.beautify_message(message)
42
+ private
43
+
44
+ def beautify_message(message)
46
45
  message = message.capitalize
47
46
  message << '.' unless message.end_with?('.')
48
47
  message
49
48
  end
50
-
51
- private_class_method :beautify_message
52
49
  end
53
50
  end
54
51
  end
@@ -20,12 +20,13 @@ module RuboCop
20
20
  class UnifiedInteger < Cop
21
21
  MSG = 'Use `Integer` instead of `%s`.'.freeze
22
22
 
23
- def_node_matcher :fixnum_or_bignum_const?, <<-PATTERN
23
+ def_node_matcher :fixnum_or_bignum_const, <<-PATTERN
24
24
  (:const {nil (:cbase)} ${:Fixnum :Bignum})
25
25
  PATTERN
26
26
 
27
27
  def on_const(node)
28
- klass = fixnum_or_bignum_const?(node)
28
+ klass = fixnum_or_bignum_const(node)
29
+
29
30
  return unless klass
30
31
 
31
32
  add_offense(node, :expression, format(MSG, klass))
@@ -137,7 +137,7 @@ module RuboCop
137
137
  end
138
138
 
139
139
  def all_disabled?(comment)
140
- comment.text =~ /rubocop:disable\s+all\b/
140
+ comment.text =~ /rubocop\s*:\s*disable\s+all\b/
141
141
  end
142
142
 
143
143
  def ignore_offense?(disabled_ranges, line_range)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  (method_argument?(node) || part_of_an_array?(node))
74
74
  add_offense(node, :expression, ARRAY_PARAM_MSG)
75
75
  else
76
- add_offense(node, :expression)
76
+ add_offense(node)
77
77
  end
78
78
  end
79
79
  end
@@ -16,6 +16,17 @@ module RuboCop
16
16
  # do_something
17
17
  # end
18
18
  #
19
+ # # bad
20
+ #
21
+ # def some_method
22
+ # if cond
23
+ # return
24
+ # else
25
+ # return
26
+ # end
27
+ # do_something
28
+ # end
29
+ #
19
30
  # @example
20
31
  #
21
32
  # # good
@@ -26,25 +37,59 @@ module RuboCop
26
37
  class UnreachableCode < Cop
27
38
  MSG = 'Unreachable code detected.'.freeze
28
39
 
29
- NODE_TYPES = %i[return next break retry redo].freeze
30
- FLOW_COMMANDS = %i[throw raise fail].freeze
31
-
32
40
  def on_begin(node)
33
41
  expressions = *node
34
42
 
35
43
  expressions.each_cons(2) do |e1, e2|
36
- next unless NODE_TYPES.include?(e1.type) || flow_command?(e1)
44
+ next unless flow_expression?(e1)
37
45
 
38
- add_offense(e2, :expression)
46
+ add_offense(e2)
39
47
  end
40
48
  end
41
49
 
50
+ alias on_kwbegin on_begin
51
+
42
52
  private
43
53
 
44
- def flow_command?(node)
45
- return false unless node.send_type?
54
+ def_node_matcher :flow_command?, <<-PATTERN
55
+ {
56
+ return next break retry redo
57
+ (send
58
+ {nil (const {nil cbase} :Kernel)}
59
+ {:raise :fail :throw}
60
+ ...)
61
+ }
62
+ PATTERN
63
+
64
+ def flow_expression?(node)
65
+ return true if flow_command?(node)
66
+ case node.type
67
+ when :begin, :kwbegin
68
+ expressions = *node
69
+ expressions.any? { |expr| flow_expression?(expr) }
70
+ when :if
71
+ check_if(node)
72
+ when :case
73
+ check_case(node)
74
+ else
75
+ false
76
+ end
77
+ end
46
78
 
47
- FLOW_COMMANDS.any? { |c| node.command?(c) }
79
+ def check_if(node)
80
+ if_branch = node.if_branch
81
+ else_branch = node.else_branch
82
+ if_branch && else_branch &&
83
+ flow_expression?(if_branch) && flow_expression?(else_branch)
84
+ end
85
+
86
+ def check_case(node)
87
+ else_branch = node.else_branch
88
+ return false unless else_branch
89
+ return false unless flow_expression?(else_branch)
90
+ node.when_branches.all? do |branch|
91
+ branch.body && flow_expression?(branch.body)
92
+ end
48
93
  end
49
94
  end
50
95
  end