rubocop 1.59.0 → 1.68.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (352) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +69 -70
  4. data/assets/output.css.erb +159 -0
  5. data/assets/output.html.erb +1 -160
  6. data/config/default.yml +165 -24
  7. data/config/internal_affairs.yml +11 -0
  8. data/exe/rubocop +4 -3
  9. data/lib/rubocop/cached_data.rb +21 -5
  10. data/lib/rubocop/cli/command/auto_generate_config.rb +18 -10
  11. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  12. data/lib/rubocop/cli/command/lsp.rb +4 -4
  13. data/lib/rubocop/cli/command/show_docs_url.rb +2 -2
  14. data/lib/rubocop/cli/command/version.rb +2 -2
  15. data/lib/rubocop/cli.rb +10 -1
  16. data/lib/rubocop/comment_config.rb +1 -1
  17. data/lib/rubocop/config.rb +41 -13
  18. data/lib/rubocop/config_finder.rb +12 -2
  19. data/lib/rubocop/config_loader.rb +15 -10
  20. data/lib/rubocop/config_loader_resolver.rb +13 -8
  21. data/lib/rubocop/config_obsoletion.rb +1 -1
  22. data/lib/rubocop/config_validator.rb +17 -9
  23. data/lib/rubocop/cop/autocorrect_logic.rb +28 -3
  24. data/lib/rubocop/cop/base.rb +73 -18
  25. data/lib/rubocop/cop/bundler/gem_version.rb +4 -5
  26. data/lib/rubocop/cop/cop.rb +30 -4
  27. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -12
  28. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
  29. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
  30. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -0
  31. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  32. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +10 -0
  33. data/lib/rubocop/cop/documentation.rb +32 -5
  34. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  35. data/lib/rubocop/cop/force.rb +12 -0
  36. data/lib/rubocop/cop/gemspec/add_runtime_dependency.rb +38 -0
  37. data/lib/rubocop/cop/gemspec/dependency_version.rb +3 -5
  38. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  39. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
  40. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
  41. data/lib/rubocop/cop/internal_affairs/cop_description.rb +0 -4
  42. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +2 -1
  43. data/lib/rubocop/cop/internal_affairs/example_description.rb +6 -5
  44. data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
  45. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +123 -29
  46. data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
  47. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +6 -21
  48. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +8 -1
  49. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +11 -1
  50. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +0 -5
  51. data/lib/rubocop/cop/internal_affairs.rb +17 -0
  52. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +5 -1
  53. data/lib/rubocop/cop/layout/assignment_indentation.rb +3 -2
  54. data/lib/rubocop/cop/layout/block_alignment.rb +30 -12
  55. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  56. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  57. data/lib/rubocop/cop/layout/condition_position.rb +0 -4
  58. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  59. data/lib/rubocop/cop/layout/empty_comment.rb +3 -1
  60. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  61. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
  62. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
  63. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  64. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +8 -3
  65. data/lib/rubocop/cop/layout/end_alignment.rb +8 -2
  66. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  67. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +18 -4
  68. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +8 -0
  69. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  70. data/lib/rubocop/cop/layout/indentation_width.rb +5 -6
  71. data/lib/rubocop/cop/layout/leading_comment_space.rb +56 -1
  72. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
  73. data/lib/rubocop/cop/layout/line_length.rb +20 -20
  74. data/lib/rubocop/cop/layout/redundant_line_break.rb +14 -2
  75. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -0
  76. data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
  77. data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -5
  78. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +4 -0
  79. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
  80. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +3 -4
  81. data/lib/rubocop/cop/legacy/corrector.rb +12 -2
  82. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +0 -2
  83. data/lib/rubocop/cop/lint/ambiguous_operator.rb +0 -2
  84. data/lib/rubocop/cop/lint/ambiguous_range.rb +4 -1
  85. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +0 -2
  86. data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -2
  87. data/lib/rubocop/cop/lint/big_decimal_new.rb +4 -7
  88. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -3
  89. data/lib/rubocop/cop/lint/circular_argument_reference.rb +0 -13
  90. data/lib/rubocop/cop/lint/debugger.rb +27 -6
  91. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  92. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +0 -10
  93. data/lib/rubocop/cop/lint/duplicate_branch.rb +39 -4
  94. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -5
  95. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +0 -4
  96. data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -10
  97. data/lib/rubocop/cop/lint/duplicate_set_element.rb +74 -0
  98. data/lib/rubocop/cop/lint/each_with_object_argument.rb +0 -4
  99. data/lib/rubocop/cop/lint/else_layout.rb +0 -2
  100. data/lib/rubocop/cop/lint/empty_conditional_body.rb +29 -8
  101. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -11
  102. data/lib/rubocop/cop/lint/empty_interpolation.rb +0 -4
  103. data/lib/rubocop/cop/lint/empty_when.rb +1 -3
  104. data/lib/rubocop/cop/lint/ensure_return.rb +1 -9
  105. data/lib/rubocop/cop/lint/erb_new_arguments.rb +21 -14
  106. data/lib/rubocop/cop/lint/float_comparison.rb +1 -1
  107. data/lib/rubocop/cop/lint/float_out_of_range.rb +0 -4
  108. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +0 -10
  109. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +23 -12
  110. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +0 -7
  111. data/lib/rubocop/cop/lint/interpolation_check.rb +0 -4
  112. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +5 -14
  113. data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
  114. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +13 -6
  115. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +25 -6
  116. data/lib/rubocop/cop/lint/loop.rb +6 -12
  117. data/lib/rubocop/cop/lint/mixed_case_range.rb +9 -4
  118. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -7
  119. data/lib/rubocop/cop/lint/next_without_accumulator.rb +0 -4
  120. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +0 -5
  121. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +7 -0
  122. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +1 -1
  123. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +5 -6
  124. data/lib/rubocop/cop/lint/percent_string_array.rb +0 -4
  125. data/lib/rubocop/cop/lint/percent_symbol_array.rb +0 -4
  126. data/lib/rubocop/cop/lint/rand_one.rb +0 -4
  127. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +3 -1
  128. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +14 -9
  129. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  130. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +0 -4
  131. data/lib/rubocop/cop/lint/redundant_with_index.rb +4 -0
  132. data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/lint/rescue_exception.rb +0 -4
  134. data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
  135. data/lib/rubocop/cop/lint/return_in_void_context.rb +0 -2
  136. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +9 -4
  137. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +107 -41
  138. data/lib/rubocop/cop/lint/script_permission.rb +3 -3
  139. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  140. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +6 -10
  141. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  142. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  143. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -3
  144. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +88 -0
  145. data/lib/rubocop/cop/lint/unified_integer.rb +0 -4
  146. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  147. data/lib/rubocop/cop/lint/unreachable_code.rb +4 -7
  148. data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
  149. data/lib/rubocop/cop/lint/uri_regexp.rb +25 -7
  150. data/lib/rubocop/cop/lint/useless_assignment.rb +19 -16
  151. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +0 -4
  152. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +77 -0
  153. data/lib/rubocop/cop/lint/useless_setter_call.rb +0 -4
  154. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  155. data/lib/rubocop/cop/lint/void.rb +41 -9
  156. data/lib/rubocop/cop/metrics/block_length.rb +6 -5
  157. data/lib/rubocop/cop/metrics/block_nesting.rb +19 -7
  158. data/lib/rubocop/cop/metrics/class_length.rb +6 -5
  159. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +4 -1
  160. data/lib/rubocop/cop/metrics/method_length.rb +6 -5
  161. data/lib/rubocop/cop/metrics/module_length.rb +6 -5
  162. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +5 -5
  163. data/lib/rubocop/cop/mixin/alignment.rb +5 -1
  164. data/lib/rubocop/cop/mixin/allowed_methods.rb +7 -1
  165. data/lib/rubocop/cop/mixin/allowed_pattern.rb +15 -3
  166. data/lib/rubocop/cop/mixin/annotation_comment.rb +0 -2
  167. data/lib/rubocop/cop/mixin/check_line_breakable.rb +10 -0
  168. data/lib/rubocop/cop/mixin/code_length.rb +12 -1
  169. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  170. data/lib/rubocop/cop/mixin/configurable_max.rb +5 -1
  171. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +24 -0
  172. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +22 -10
  173. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +9 -2
  174. data/lib/rubocop/cop/mixin/line_length_help.rb +7 -2
  175. data/lib/rubocop/cop/mixin/method_complexity.rb +15 -6
  176. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  177. data/lib/rubocop/cop/mixin/percent_array.rb +1 -1
  178. data/lib/rubocop/cop/mixin/rescue_node.rb +4 -0
  179. data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -1
  180. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -2
  181. data/lib/rubocop/cop/mixin/string_literals_help.rb +12 -0
  182. data/lib/rubocop/cop/naming/accessor_method_name.rb +5 -0
  183. data/lib/rubocop/cop/naming/block_forwarding.rb +33 -6
  184. data/lib/rubocop/cop/naming/file_name.rb +2 -2
  185. data/lib/rubocop/cop/naming/inclusive_language.rb +13 -5
  186. data/lib/rubocop/cop/naming/predicate_name.rb +55 -29
  187. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -1
  188. data/lib/rubocop/cop/offense.rb +4 -5
  189. data/lib/rubocop/cop/registry.rb +1 -1
  190. data/lib/rubocop/cop/security/compound_hash.rb +2 -2
  191. data/lib/rubocop/cop/security/open.rb +2 -2
  192. data/lib/rubocop/cop/style/access_modifier_declarations.rb +62 -2
  193. data/lib/rubocop/cop/style/accessor_grouping.rb +10 -2
  194. data/lib/rubocop/cop/style/alias.rb +2 -1
  195. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +79 -0
  196. data/lib/rubocop/cop/style/arguments_forwarding.rb +141 -24
  197. data/lib/rubocop/cop/style/bitwise_predicate.rb +100 -0
  198. data/lib/rubocop/cop/style/block_delimiters.rb +31 -3
  199. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  200. data/lib/rubocop/cop/style/class_vars.rb +3 -3
  201. data/lib/rubocop/cop/style/collection_compact.rb +19 -10
  202. data/lib/rubocop/cop/style/combinable_defined.rb +115 -0
  203. data/lib/rubocop/cop/style/combinable_loops.rb +7 -0
  204. data/lib/rubocop/cop/style/commented_keyword.rb +12 -3
  205. data/lib/rubocop/cop/style/conditional_assignment.rb +7 -8
  206. data/lib/rubocop/cop/style/copyright.rb +31 -21
  207. data/lib/rubocop/cop/style/data_inheritance.rb +1 -1
  208. data/lib/rubocop/cop/style/def_with_parentheses.rb +0 -2
  209. data/lib/rubocop/cop/style/documentation.rb +24 -24
  210. data/lib/rubocop/cop/style/documentation_method.rb +20 -0
  211. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -8
  212. data/lib/rubocop/cop/style/empty_else.rb +6 -5
  213. data/lib/rubocop/cop/style/empty_heredoc.rb +1 -14
  214. data/lib/rubocop/cop/style/empty_literal.rb +31 -22
  215. data/lib/rubocop/cop/style/endless_method.rb +1 -14
  216. data/lib/rubocop/cop/style/eval_with_location.rb +16 -24
  217. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  218. data/lib/rubocop/cop/style/file_read.rb +2 -5
  219. data/lib/rubocop/cop/style/file_write.rb +2 -5
  220. data/lib/rubocop/cop/style/for.rb +2 -0
  221. data/lib/rubocop/cop/style/format_string.rb +9 -9
  222. data/lib/rubocop/cop/style/format_string_token.rb +2 -2
  223. data/lib/rubocop/cop/style/global_std_stream.rb +7 -1
  224. data/lib/rubocop/cop/style/guard_clause.rb +17 -2
  225. data/lib/rubocop/cop/style/hash_each_methods.rb +35 -8
  226. data/lib/rubocop/cop/style/hash_except.rb +8 -5
  227. data/lib/rubocop/cop/style/hash_syntax.rb +26 -4
  228. data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -2
  229. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  230. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +5 -4
  231. data/lib/rubocop/cop/style/if_with_semicolon.rb +49 -6
  232. data/lib/rubocop/cop/style/in_pattern_then.rb +6 -2
  233. data/lib/rubocop/cop/style/inverse_methods.rb +8 -8
  234. data/lib/rubocop/cop/style/invertible_unless_condition.rb +46 -4
  235. data/lib/rubocop/cop/style/keyword_arguments_merging.rb +67 -0
  236. data/lib/rubocop/cop/style/lambda.rb +1 -1
  237. data/lib/rubocop/cop/style/magic_comment_format.rb +1 -1
  238. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +81 -50
  239. data/lib/rubocop/cop/style/map_into_array.rb +233 -0
  240. data/lib/rubocop/cop/style/map_to_hash.rb +10 -6
  241. data/lib/rubocop/cop/style/map_to_set.rb +1 -1
  242. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +29 -11
  243. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
  244. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -2
  245. data/lib/rubocop/cop/style/missing_else.rb +0 -4
  246. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  247. data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -1
  248. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +5 -3
  249. data/lib/rubocop/cop/style/multiline_when_then.rb +0 -4
  250. data/lib/rubocop/cop/style/multiple_comparison.rb +28 -47
  251. data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
  252. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  253. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  254. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  255. data/lib/rubocop/cop/style/numeric_predicate.rb +12 -4
  256. data/lib/rubocop/cop/style/object_then.rb +5 -3
  257. data/lib/rubocop/cop/style/one_line_conditional.rb +6 -2
  258. data/lib/rubocop/cop/style/operator_method_call.rb +25 -6
  259. data/lib/rubocop/cop/style/parallel_assignment.rb +8 -9
  260. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  261. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -3
  262. data/lib/rubocop/cop/style/raise_args.rb +4 -1
  263. data/lib/rubocop/cop/style/redundant_argument.rb +25 -2
  264. data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
  265. data/lib/rubocop/cop/style/redundant_begin.rb +5 -1
  266. data/lib/rubocop/cop/style/redundant_condition.rb +4 -4
  267. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +5 -4
  268. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  269. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
  270. data/lib/rubocop/cop/style/redundant_filter_chain.rb +1 -1
  271. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +46 -0
  272. data/lib/rubocop/cop/style/redundant_line_continuation.rb +40 -7
  273. data/lib/rubocop/cop/style/redundant_parentheses.rb +27 -13
  274. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  275. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -1
  276. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -24
  277. data/lib/rubocop/cop/style/redundant_return.rb +6 -0
  278. data/lib/rubocop/cop/style/require_order.rb +2 -2
  279. data/lib/rubocop/cop/style/rescue_modifier.rb +13 -1
  280. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +54 -12
  281. data/lib/rubocop/cop/style/safe_navigation.rb +106 -52
  282. data/lib/rubocop/cop/style/safe_navigation_chain_length.rb +52 -0
  283. data/lib/rubocop/cop/style/sample.rb +1 -3
  284. data/lib/rubocop/cop/style/select_by_regexp.rb +9 -6
  285. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  286. data/lib/rubocop/cop/style/send.rb +4 -4
  287. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +104 -0
  288. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  289. data/lib/rubocop/cop/style/sole_nested_conditional.rb +21 -2
  290. data/lib/rubocop/cop/style/special_global_vars.rb +1 -2
  291. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  292. data/lib/rubocop/cop/style/super_arguments.rb +174 -0
  293. data/lib/rubocop/cop/style/symbol_proc.rb +75 -5
  294. data/lib/rubocop/cop/style/ternary_parentheses.rb +26 -5
  295. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
  296. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  297. data/lib/rubocop/cop/style/while_until_do.rb +0 -2
  298. data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
  299. data/lib/rubocop/cop/style/zero_length_predicate.rb +32 -24
  300. data/lib/rubocop/cop/team.rb +27 -3
  301. data/lib/rubocop/cop/util.rb +8 -2
  302. data/lib/rubocop/cop/utils/regexp_ranges.rb +1 -1
  303. data/lib/rubocop/cop/variable_force/assignment.rb +18 -3
  304. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  305. data/lib/rubocop/cop/variable_force/variable.rb +5 -1
  306. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  307. data/lib/rubocop/cop/variable_force.rb +13 -1
  308. data/lib/rubocop/cops_documentation_generator.rb +96 -43
  309. data/lib/rubocop/core_ext/string.rb +2 -6
  310. data/lib/rubocop/directive_comment.rb +10 -8
  311. data/lib/rubocop/ext/regexp_node.rb +18 -35
  312. data/lib/rubocop/ext/regexp_parser.rb +4 -21
  313. data/lib/rubocop/file_finder.rb +9 -4
  314. data/lib/rubocop/formatter/clang_style_formatter.rb +3 -7
  315. data/lib/rubocop/formatter/disabled_config_formatter.rb +24 -9
  316. data/lib/rubocop/formatter/formatter_set.rb +7 -1
  317. data/lib/rubocop/formatter/html_formatter.rb +32 -10
  318. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  319. data/lib/rubocop/formatter/junit_formatter.rb +70 -23
  320. data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
  321. data/lib/rubocop/formatter/tap_formatter.rb +3 -7
  322. data/lib/rubocop/formatter.rb +1 -1
  323. data/lib/rubocop/lockfile.rb +58 -7
  324. data/lib/rubocop/lsp/logger.rb +1 -1
  325. data/lib/rubocop/lsp/routes.rb +12 -15
  326. data/lib/rubocop/lsp/runtime.rb +3 -1
  327. data/lib/rubocop/lsp/server.rb +6 -2
  328. data/lib/rubocop/lsp/severity.rb +1 -1
  329. data/lib/rubocop/lsp.rb +36 -0
  330. data/lib/rubocop/magic_comment.rb +1 -1
  331. data/lib/rubocop/options.rb +17 -12
  332. data/lib/rubocop/path_util.rb +6 -2
  333. data/lib/rubocop/rake_task.rb +1 -1
  334. data/lib/rubocop/remote_config.rb +5 -1
  335. data/lib/rubocop/result_cache.rb +2 -8
  336. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  337. data/lib/rubocop/rspec/expect_offense.rb +17 -8
  338. data/lib/rubocop/rspec/shared_contexts.rb +75 -18
  339. data/lib/rubocop/rspec/support.rb +3 -0
  340. data/lib/rubocop/runner.rb +31 -9
  341. data/lib/rubocop/server/cache.rb +16 -2
  342. data/lib/rubocop/server/client_command/exec.rb +2 -3
  343. data/lib/rubocop/server/client_command/start.rb +1 -1
  344. data/lib/rubocop/server/core.rb +5 -0
  345. data/lib/rubocop/server/server_command/exec.rb +0 -1
  346. data/lib/rubocop/target_finder.rb +84 -78
  347. data/lib/rubocop/target_ruby.rb +87 -81
  348. data/lib/rubocop/version.rb +45 -9
  349. data/lib/rubocop/yaml_duplication_checker.rb +20 -26
  350. data/lib/rubocop.rb +21 -1
  351. metadata +33 -35
  352. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -29,36 +29,36 @@ module RuboCop
29
29
  # end
30
30
  #
31
31
  # # allowed
32
- # # Class without body
32
+ # # Class without body
33
+ # class Person
34
+ # end
35
+ #
36
+ # # Namespace - A namespace can be a class or a module
37
+ # # Containing a class
38
+ # module Namespace
39
+ # # Description/Explanation of Person class
33
40
  # class Person
41
+ # # ...
34
42
  # end
43
+ # end
35
44
  #
36
- # # Namespace - A namespace can be a class or a module
37
- # # Containing a class
38
- # module Namespace
39
- # # Description/Explanation of Person class
40
- # class Person
41
- # # ...
42
- # end
45
+ # # Containing constant visibility declaration
46
+ # module Namespace
47
+ # class Private
43
48
  # end
44
49
  #
45
- # # Containing constant visibility declaration
46
- # module Namespace
47
- # class Private
48
- # end
49
- #
50
- # private_constant :Private
51
- # end
50
+ # private_constant :Private
51
+ # end
52
52
  #
53
- # # Containing constant definition
54
- # module Namespace
55
- # Public = Class.new
56
- # end
53
+ # # Containing constant definition
54
+ # module Namespace
55
+ # Public = Class.new
56
+ # end
57
57
  #
58
- # # Macro calls
59
- # module Namespace
60
- # extend Foo
61
- # end
58
+ # # Macro calls
59
+ # module Namespace
60
+ # extend Foo
61
+ # end
62
62
  #
63
63
  # @example AllowedConstants: ['ClassMethods']
64
64
  #
@@ -67,7 +67,7 @@ module RuboCop
67
67
  # module ClassMethods
68
68
  # # ...
69
69
  # end
70
- # end
70
+ # end
71
71
  #
72
72
  class Documentation < Base
73
73
  include DocumentationComment
@@ -95,6 +95,17 @@ module RuboCop
95
95
  # end
96
96
  # end
97
97
  #
98
+ # @example AllowedMethods: ['method_missing', 'respond_to_missing?']
99
+ #
100
+ # # good
101
+ # class Foo
102
+ # def method_missing(name, *args)
103
+ # end
104
+ #
105
+ # def respond_to_missing?(symbol, include_private)
106
+ # end
107
+ # end
108
+ #
98
109
  class DocumentationMethod < Base
99
110
  include DocumentationComment
100
111
  include DefNode
@@ -119,6 +130,7 @@ module RuboCop
119
130
  def check(node)
120
131
  return if non_public?(node) && !require_for_non_public_methods?
121
132
  return if documentation_comment?(node)
133
+ return if method_allowed?(node)
122
134
 
123
135
  add_offense(node)
124
136
  end
@@ -126,6 +138,14 @@ module RuboCop
126
138
  def require_for_non_public_methods?
127
139
  cop_config['RequireForNonPublicMethods']
128
140
  end
141
+
142
+ def method_allowed?(node)
143
+ allowed_methods.include?(node.method_name)
144
+ end
145
+
146
+ def allowed_methods
147
+ @allowed_methods ||= cop_config.fetch('AllowedMethods', []).map(&:to_sym)
148
+ end
129
149
  end
130
150
  end
131
151
  end
@@ -16,7 +16,6 @@ module RuboCop
16
16
  # # good
17
17
  # 5.times { }
18
18
  #
19
- # @example
20
19
  # # bad
21
20
  # (0...10).each {}
22
21
  #
@@ -32,27 +31,27 @@ module RuboCop
32
31
 
33
32
  send_node = node.send_node
34
33
 
35
- range = send_node.receiver.source_range.join(send_node.loc.selector)
36
-
37
- add_offense(range) do |corrector|
34
+ add_offense(send_node) do |corrector|
38
35
  range_type, min, max = each_range(node)
39
36
 
40
37
  max += 1 if range_type == :irange
41
38
 
42
- corrector.replace(node.send_node, "#{max - min}.times")
39
+ corrector.replace(send_node, "#{max - min}.times")
43
40
  end
44
41
  end
45
42
 
46
43
  private
47
44
 
48
45
  def offending?(node)
46
+ return false unless node.arguments.empty?
47
+
49
48
  each_range_with_zero_origin?(node) || each_range_without_block_argument?(node)
50
49
  end
51
50
 
52
51
  # @!method each_range(node)
53
52
  def_node_matcher :each_range, <<~PATTERN
54
53
  (block
55
- (send
54
+ (call
56
55
  (begin
57
56
  (${irange erange}
58
57
  (int $_) (int $_)))
@@ -64,7 +63,7 @@ module RuboCop
64
63
  # @!method each_range_with_zero_origin?(node)
65
64
  def_node_matcher :each_range_with_zero_origin?, <<~PATTERN
66
65
  (block
67
- (send
66
+ (call
68
67
  (begin
69
68
  ({irange erange}
70
69
  (int 0) (int _)))
@@ -76,7 +75,7 @@ module RuboCop
76
75
  # @!method each_range_without_block_argument?(node)
77
76
  def_node_matcher :each_range_without_block_argument?, <<~PATTERN
78
77
  (block
79
- (send
78
+ (call
80
79
  (begin
81
80
  ({irange erange}
82
81
  (int _) (int _)))
@@ -143,7 +143,7 @@ module RuboCop
143
143
  private
144
144
 
145
145
  def check(node)
146
- return if cop_config['AllowComments'] && comment_in_else?(node.loc)
146
+ return if cop_config['AllowComments'] && comment_in_else?(node)
147
147
 
148
148
  empty_check(node) if empty_style?
149
149
  nil_check(node) if nil_style?
@@ -171,16 +171,17 @@ module RuboCop
171
171
 
172
172
  def autocorrect(corrector, node)
173
173
  return false if autocorrect_forbidden?(node.type.to_s)
174
- return false if comment_in_else?(node.loc)
174
+ return false if comment_in_else?(node)
175
175
 
176
176
  end_pos = base_node(node).loc.end.begin_pos
177
177
  corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
178
178
  end
179
179
 
180
- def comment_in_else?(loc)
181
- return false if loc.else.nil? || loc.end.nil?
180
+ def comment_in_else?(node)
181
+ node = node.parent while node.if_type? && node.elsif?
182
+ return false unless node.else?
182
183
 
183
- processed_source.contains_comment?(loc.else.join(loc.end))
184
+ processed_source.contains_comment?(node.loc.else.join(node.source_range.end))
184
185
  end
185
186
 
186
187
  def base_node(node)
@@ -36,6 +36,7 @@ module RuboCop
36
36
  class EmptyHeredoc < Base
37
37
  include Heredoc
38
38
  include RangeHelp
39
+ include StringLiteralsHelp
39
40
  extend AutoCorrector
40
41
 
41
42
  MSG = 'Use an empty string literal instead of heredoc.'
@@ -53,20 +54,6 @@ module RuboCop
53
54
  corrector.remove(range_by_whole_lines(heredoc_end, include_final_newline: true))
54
55
  end
55
56
  end
56
-
57
- private
58
-
59
- def preferred_string_literal
60
- enforce_double_quotes? ? '""' : "''"
61
- end
62
-
63
- def enforce_double_quotes?
64
- string_literals_config['EnforcedStyle'] == 'double_quotes'
65
- end
66
-
67
- def string_literals_config
68
- config.for_cop('Style/StringLiterals')
69
- end
70
57
  end
71
58
  end
72
59
  end
@@ -9,7 +9,9 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # a = Array.new
12
+ # a = Array[]
12
13
  # h = Hash.new
14
+ # h = Hash[]
13
15
  # s = String.new
14
16
  #
15
17
  # # good
@@ -19,16 +21,17 @@ module RuboCop
19
21
  class EmptyLiteral < Base
20
22
  include FrozenStringLiteral
21
23
  include RangeHelp
24
+ include StringLiteralsHelp
22
25
  extend AutoCorrector
23
26
 
24
- ARR_MSG = 'Use array literal `[]` instead of `Array.new`.'
25
- HASH_MSG = 'Use hash literal `{}` instead of `Hash.new`.'
27
+ ARR_MSG = 'Use array literal `[]` instead of `%<current>s`.'
28
+ HASH_MSG = 'Use hash literal `{}` instead of `%<current>s`.'
26
29
  STR_MSG = 'Use string literal `%<prefer>s` instead of `String.new`.'
27
30
 
28
- RESTRICT_ON_SEND = %i[new].freeze
31
+ RESTRICT_ON_SEND = %i[new [] Array Hash].freeze
29
32
 
30
33
  # @!method array_node(node)
31
- def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new)'
34
+ def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new (array)?)'
32
35
 
33
36
  # @!method hash_node(node)
34
37
  def_node_matcher :hash_node, '(send (const {nil? cbase} :Hash) :new)'
@@ -47,6 +50,22 @@ module RuboCop
47
50
  }
48
51
  PATTERN
49
52
 
53
+ # @!method array_with_index(node)
54
+ def_node_matcher :array_with_index, <<~PATTERN
55
+ {
56
+ (send (const {nil? cbase} :Array) :[])
57
+ (send nil? :Array (array))
58
+ }
59
+ PATTERN
60
+
61
+ # @!method hash_with_index(node)
62
+ def_node_matcher :hash_with_index, <<~PATTERN
63
+ {
64
+ (send (const {nil? cbase} :Hash) :[])
65
+ (send nil? :Hash (array))
66
+ }
67
+ PATTERN
68
+
50
69
  def on_send(node)
51
70
  return unless (message = offense_message(node))
52
71
 
@@ -59,26 +78,14 @@ module RuboCop
59
78
 
60
79
  def offense_message(node)
61
80
  if offense_array_node?(node)
62
- ARR_MSG
81
+ format(ARR_MSG, current: node.source)
63
82
  elsif offense_hash_node?(node)
64
- HASH_MSG
83
+ format(HASH_MSG, current: node.source)
65
84
  elsif str_node(node) && !frozen_strings?
66
85
  format(STR_MSG, prefer: preferred_string_literal)
67
86
  end
68
87
  end
69
88
 
70
- def preferred_string_literal
71
- enforce_double_quotes? ? '""' : "''"
72
- end
73
-
74
- def enforce_double_quotes?
75
- string_literals_config['EnforcedStyle'] == 'double_quotes'
76
- end
77
-
78
- def string_literals_config
79
- config.for_cop('Style/StringLiterals')
80
- end
81
-
82
89
  def first_argument_unparenthesized?(node)
83
90
  parent = node.parent
84
91
  return false unless parent && %i[send super zsuper].include?(parent.type)
@@ -100,12 +107,12 @@ module RuboCop
100
107
  end
101
108
 
102
109
  def offense_array_node?(node)
103
- array_node(node) && !array_with_block(node.parent)
110
+ (array_node(node) && !array_with_block(node.parent)) || array_with_index(node)
104
111
  end
105
112
 
106
113
  def offense_hash_node?(node)
107
114
  # If Hash.new takes a block, it can't be changed to {}.
108
- hash_node(node) && !hash_with_block(node.parent)
115
+ (hash_node(node) && !hash_with_block(node.parent)) || hash_with_index(node)
109
116
  end
110
117
 
111
118
  def correction(node)
@@ -129,8 +136,10 @@ module RuboCop
129
136
  def frozen_strings?
130
137
  return true if frozen_string_literals_enabled?
131
138
 
132
- frozen_string_cop_enabled = config.for_cop('Style/FrozenStringLiteral')['Enabled']
133
- frozen_string_cop_enabled && !frozen_string_literals_disabled?
139
+ frozen_string_cop_enabled = config.for_cop('Style/FrozenStringLiteralComment')['Enabled']
140
+ frozen_string_cop_enabled &&
141
+ !frozen_string_literals_disabled? &&
142
+ string_literals_frozen_by_default?.nil?
134
143
  end
135
144
  end
136
145
  end
@@ -48,6 +48,7 @@ module RuboCop
48
48
  #
49
49
  class EndlessMethod < Base
50
50
  include ConfigurableEnforcedStyle
51
+ include EndlessMethodRewriter
51
52
  extend TargetRubyVersion
52
53
  extend AutoCorrector
53
54
 
@@ -81,20 +82,6 @@ module RuboCop
81
82
 
82
83
  add_offense(node) { |corrector| correct_to_multiline(corrector, node) }
83
84
  end
84
-
85
- def correct_to_multiline(corrector, node)
86
- replacement = <<~RUBY.strip
87
- def #{node.method_name}#{arguments(node)}
88
- #{node.body.source}
89
- end
90
- RUBY
91
-
92
- corrector.replace(node, replacement)
93
- end
94
-
95
- def arguments(node, missing = '')
96
- node.arguments.any? ? node.arguments.source : missing
97
- end
98
85
  end
99
86
  end
100
87
  end
@@ -17,6 +17,18 @@ module RuboCop
17
17
  # will not attempt to automatically add a binding, or add filename and
18
18
  # line values.
19
19
  #
20
+ # NOTE: This cop works only when a string literal is given as a code string.
21
+ # No offense is reported if a string variable is given as below:
22
+ #
23
+ # [source,ruby]
24
+ # ----
25
+ # code = <<-RUBY
26
+ # def do_something
27
+ # end
28
+ # RUBY
29
+ # eval code # not checked.
30
+ # ----
31
+ #
20
32
  # @example
21
33
  # # bad
22
34
  # eval <<-RUBY
@@ -42,17 +54,6 @@ module RuboCop
42
54
  # end
43
55
  # RUBY
44
56
  #
45
- # This cop works only when a string literal is given as a code string.
46
- # No offense is reported if a string variable is given as below:
47
- #
48
- # @example
49
- # # not checked
50
- # code = <<-RUBY
51
- # def do_something
52
- # end
53
- # RUBY
54
- # eval code
55
- #
56
57
  class EvalWithLocation < Base
57
58
  extend AutoCorrector
58
59
 
@@ -128,17 +129,6 @@ module RuboCop
128
129
  node.method?(:eval) ? node.arguments.size >= 2 : true
129
130
  end
130
131
 
131
- # FIXME: It's a Style/ConditionalAssignment's false positive.
132
- # rubocop:disable Style/ConditionalAssignment
133
- def with_lineno?(node)
134
- if node.method?(:eval)
135
- node.arguments.size == 4
136
- else
137
- node.arguments.size == 3
138
- end
139
- end
140
- # rubocop:enable Style/ConditionalAssignment
141
-
142
132
  def add_offense_for_incorrect_line(method_name, line_node, sign, line_diff)
143
133
  expected = expected_line(sign, line_diff)
144
134
  message = format(MSG_INCORRECT_LINE,
@@ -146,13 +136,13 @@ module RuboCop
146
136
  actual: line_node.source,
147
137
  expected: expected)
148
138
 
149
- add_offense(line_node.source_range, message: message) do |corrector|
139
+ add_offense(line_node, message: message) do |corrector|
150
140
  corrector.replace(line_node, expected)
151
141
  end
152
142
  end
153
143
 
154
144
  def check_file(node, file_node)
155
- return true if special_file_keyword?(file_node)
145
+ return if special_file_keyword?(file_node)
156
146
 
157
147
  message = format(MSG_INCORRECT_FILE,
158
148
  method_name: node.method_name,
@@ -166,6 +156,8 @@ module RuboCop
166
156
 
167
157
  def check_line(node, code)
168
158
  line_node = node.last_argument
159
+ return if line_node.variable? || (line_node.send_type? && !line_node.method?(:+))
160
+
169
161
  line_diff = line_difference(line_node, code)
170
162
  if line_diff.zero?
171
163
  add_offense_for_same_line(node, line_node)
@@ -38,12 +38,13 @@ module RuboCop
38
38
  PATTERN
39
39
 
40
40
  def on_send(node)
41
+ return unless (receiver = node.receiver)
41
42
  return unless (regexp = exact_regexp_match(node))
42
43
 
43
44
  parsed_regexp = Regexp::Parser.parse(regexp)
44
45
  return unless exact_match_pattern?(parsed_regexp)
45
46
 
46
- prefer = "#{node.receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
47
+ prefer = "#{receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
47
48
 
48
49
  add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
49
50
  corrector.replace(node, prefer)
@@ -6,8 +6,7 @@ module RuboCop
6
6
  # Favor `File.(bin)read` convenience methods.
7
7
  #
8
8
  # @example
9
- # ## text mode
10
- # # bad
9
+ # # bad - text mode
11
10
  # File.open(filename).read
12
11
  # File.open(filename, &:read)
13
12
  # File.open(filename) { |f| f.read }
@@ -23,9 +22,7 @@ module RuboCop
23
22
  # # good
24
23
  # File.read(filename)
25
24
  #
26
- # @example
27
- # ## binary mode
28
- # # bad
25
+ # # bad - binary mode
29
26
  # File.open(filename, 'rb').read
30
27
  # File.open(filename, 'rb', &:read)
31
28
  # File.open(filename, 'rb') do |f|
@@ -17,8 +17,7 @@ module RuboCop
17
17
  # ----
18
18
  #
19
19
  # @example
20
- # ## text mode
21
- # # bad
20
+ # # bad - text mode
22
21
  # File.open(filename, 'w').write(content)
23
22
  # File.open(filename, 'w') do |f|
24
23
  # f.write(content)
@@ -27,9 +26,7 @@ module RuboCop
27
26
  # # good
28
27
  # File.write(filename, content)
29
28
  #
30
- # @example
31
- # ## binary mode
32
- # # bad
29
+ # # bad - binary mode
33
30
  # File.open(filename, 'wb').write(content)
34
31
  # File.open(filename, 'wb') do |f|
35
32
  # f.write(content)
@@ -66,6 +66,8 @@ module RuboCop
66
66
  return unless suspect_enumerable?(node)
67
67
 
68
68
  if style == :for
69
+ return unless node.receiver
70
+
69
71
  add_offense(node, message: PREFER_FOR) do |corrector|
70
72
  EachToForCorrector.new(node).call(corrector)
71
73
  opposite_style_detected
@@ -25,27 +25,27 @@ module RuboCop
25
25
  #
26
26
  # @example EnforcedStyle: format (default)
27
27
  # # bad
28
- # puts sprintf('%10s', 'hoge')
29
- # puts '%10s' % 'hoge'
28
+ # puts sprintf('%10s', 'foo')
29
+ # puts '%10s' % 'foo'
30
30
  #
31
31
  # # good
32
- # puts format('%10s', 'hoge')
32
+ # puts format('%10s', 'foo')
33
33
  #
34
34
  # @example EnforcedStyle: sprintf
35
35
  # # bad
36
- # puts format('%10s', 'hoge')
37
- # puts '%10s' % 'hoge'
36
+ # puts format('%10s', 'foo')
37
+ # puts '%10s' % 'foo'
38
38
  #
39
39
  # # good
40
- # puts sprintf('%10s', 'hoge')
40
+ # puts sprintf('%10s', 'foo')
41
41
  #
42
42
  # @example EnforcedStyle: percent
43
43
  # # bad
44
- # puts format('%10s', 'hoge')
45
- # puts sprintf('%10s', 'hoge')
44
+ # puts format('%10s', 'foo')
45
+ # puts sprintf('%10s', 'foo')
46
46
  #
47
47
  # # good
48
- # puts '%10s' % 'hoge'
48
+ # puts '%10s' % 'foo'
49
49
  #
50
50
  class FormatString < Base
51
51
  include ConfigurableEnforcedStyle
@@ -11,8 +11,8 @@ module RuboCop
11
11
  # The reason is that _unannotated_ format is very similar
12
12
  # to encoded URLs or Date/Time formatting strings.
13
13
  #
14
- # This cop can be customized allowed methods with `AllowedMethods`.
15
- # By default, there are no methods to allowed.
14
+ # This cop's allowed methods can be customized with `AllowedMethods`.
15
+ # By default, there are no allowed methods.
16
16
  #
17
17
  # @example EnforcedStyle: annotated (default)
18
18
  #
@@ -44,7 +44,9 @@ module RuboCop
44
44
  PATTERN
45
45
 
46
46
  def on_const(node)
47
- const_name = node.children[1]
47
+ return if namespaced?(node)
48
+
49
+ const_name = node.short_name
48
50
  return unless STD_STREAMS.include?(const_name)
49
51
 
50
52
  gvar_name = gvar_name(const_name).to_sym
@@ -61,6 +63,10 @@ module RuboCop
61
63
  format(MSG, gvar_name: gvar_name(const_name), const_name: const_name)
62
64
  end
63
65
 
66
+ def namespaced?(node)
67
+ !node.namespace.nil? && (node.relative? || !node.namespace.cbase_type?)
68
+ end
69
+
64
70
  def gvar_name(const_name)
65
71
  "$#{const_name.to_s.downcase}"
66
72
  end
@@ -234,11 +234,11 @@ module RuboCop
234
234
  end
235
235
 
236
236
  def autocorrect_heredoc_argument(corrector, node, heredoc_branch, leave_branch, guard)
237
+ remove_whole_lines(corrector, node.loc.end)
237
238
  return unless node.else?
238
239
 
239
240
  remove_whole_lines(corrector, leave_branch.source_range)
240
241
  remove_whole_lines(corrector, node.loc.else)
241
- remove_whole_lines(corrector, node.loc.end)
242
242
  remove_whole_lines(corrector, range_of_branch_to_remove(node, guard))
243
243
  corrector.insert_after(
244
244
  heredoc_branch.last_argument.loc.heredoc_end, "\n#{leave_branch.source}"
@@ -277,11 +277,14 @@ module RuboCop
277
277
  end
278
278
 
279
279
  def trivial?(node)
280
+ return false unless node.if_branch
281
+
280
282
  node.branches.one? && !node.if_branch.if_type? && !node.if_branch.begin_type?
281
283
  end
282
284
 
283
285
  def accepted_if?(node, ending)
284
- return true if node.modifier_form? || node.ternary? || node.elsif_conditional?
286
+ return true if node.modifier_form? || node.ternary? || node.elsif_conditional? ||
287
+ assigned_lvar_used_in_if_branch?(node)
285
288
 
286
289
  if ending
287
290
  node.else?
@@ -290,6 +293,18 @@ module RuboCop
290
293
  end
291
294
  end
292
295
 
296
+ def assigned_lvar_used_in_if_branch?(node)
297
+ return false unless (if_branch = node.if_branch)
298
+
299
+ assigned_lvars_in_condition = node.condition.each_descendant(:lvasgn).map do |lvasgn|
300
+ lvar_name, = *lvasgn
301
+ lvar_name.to_s
302
+ end
303
+ used_lvars_in_branch = if_branch.each_descendant(:lvar).map(&:source) || []
304
+
305
+ (assigned_lvars_in_condition & used_lvars_in_branch).any?
306
+ end
307
+
293
308
  def remove_whole_lines(corrector, range)
294
309
  corrector.remove(range_by_whole_lines(range, include_final_newline: true))
295
310
  end