rubocop 0.90.0 → 0.93.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 (201) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +79 -3
  4. data/lib/rubocop.rb +20 -5
  5. data/lib/rubocop/cached_data.rb +2 -1
  6. data/lib/rubocop/cli/command/execute_runner.rb +8 -0
  7. data/lib/rubocop/comment_config.rb +9 -5
  8. data/lib/rubocop/config_loader.rb +3 -3
  9. data/lib/rubocop/config_regeneration.rb +33 -0
  10. data/lib/rubocop/config_store.rb +3 -3
  11. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -1
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  13. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -0
  14. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -10
  16. data/lib/rubocop/cop/generator.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  18. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  19. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -0
  20. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  21. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -0
  22. data/lib/rubocop/cop/layout/array_alignment.rb +1 -0
  23. data/lib/rubocop/cop/layout/begin_end_alignment.rb +77 -0
  24. data/lib/rubocop/cop/layout/case_indentation.rb +4 -7
  25. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  26. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/dot_position.rb +6 -9
  28. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
  29. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +4 -12
  30. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +13 -8
  31. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -2
  32. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -2
  33. data/lib/rubocop/cop/layout/end_alignment.rb +5 -10
  34. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +26 -4
  35. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +4 -13
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -7
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +0 -4
  38. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +6 -21
  39. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +3 -8
  40. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -2
  41. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -0
  42. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -0
  43. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +18 -1
  44. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -2
  45. data/lib/rubocop/cop/lint/boolean_symbol.rb +3 -0
  46. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +74 -0
  47. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  48. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -3
  49. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -4
  50. data/lib/rubocop/cop/lint/duplicate_require.rb +7 -2
  51. data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +2 -4
  52. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  53. data/lib/rubocop/cop/lint/empty_file.rb +1 -4
  54. data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -0
  55. data/lib/rubocop/cop/lint/float_comparison.rb +2 -2
  56. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  57. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +37 -0
  58. data/lib/rubocop/cop/lint/identity_comparison.rb +51 -0
  59. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +2 -5
  60. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  61. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  62. data/lib/rubocop/cop/lint/multiple_comparison.rb +3 -1
  63. data/lib/rubocop/cop/lint/number_conversion.rb +1 -0
  64. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -2
  65. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  66. data/lib/rubocop/cop/lint/raise_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/rand_one.rb +2 -1
  68. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +22 -12
  69. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -4
  70. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  71. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +78 -0
  72. data/lib/rubocop/cop/lint/rescue_type.rb +0 -1
  73. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +3 -1
  74. data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -6
  75. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  76. data/lib/rubocop/cop/lint/to_json.rb +16 -5
  77. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -6
  78. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +3 -1
  79. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -1
  80. data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -9
  81. data/lib/rubocop/cop/lint/useless_method_definition.rb +20 -27
  82. data/lib/rubocop/cop/lint/useless_times.rb +106 -0
  83. data/lib/rubocop/cop/metrics/block_length.rb +3 -1
  84. data/lib/rubocop/cop/metrics/class_length.rb +14 -6
  85. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +25 -16
  86. data/lib/rubocop/cop/mixin/comments_help.rb +3 -9
  87. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  88. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  89. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +9 -0
  90. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -2
  91. data/lib/rubocop/cop/mixin/rescue_node.rb +1 -0
  92. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -3
  93. data/lib/rubocop/cop/mixin/visibility_help.rb +4 -16
  94. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  95. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  96. data/lib/rubocop/cop/offense.rb +15 -2
  97. data/lib/rubocop/cop/security/eval.rb +1 -0
  98. data/lib/rubocop/cop/security/json_load.rb +1 -0
  99. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  100. data/lib/rubocop/cop/security/open.rb +1 -0
  101. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  102. data/lib/rubocop/cop/style/access_modifier_declarations.rb +7 -11
  103. data/lib/rubocop/cop/style/accessor_grouping.rb +3 -0
  104. data/lib/rubocop/cop/style/alias.rb +2 -0
  105. data/lib/rubocop/cop/style/array_coercion.rb +4 -0
  106. data/lib/rubocop/cop/style/array_join.rb +1 -0
  107. data/lib/rubocop/cop/style/attr.rb +1 -0
  108. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -0
  109. data/lib/rubocop/cop/style/case_equality.rb +3 -0
  110. data/lib/rubocop/cop/style/case_like_if.rb +20 -4
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -0
  112. data/lib/rubocop/cop/style/class_check.rb +6 -9
  113. data/lib/rubocop/cop/style/class_equality_comparison.rb +64 -0
  114. data/lib/rubocop/cop/style/class_methods_definitions.rb +42 -16
  115. data/lib/rubocop/cop/style/class_vars.rb +1 -2
  116. data/lib/rubocop/cop/style/combinable_loops.rb +13 -11
  117. data/lib/rubocop/cop/style/comment_annotation.rb +6 -0
  118. data/lib/rubocop/cop/style/commented_keyword.rb +7 -8
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +49 -60
  120. data/lib/rubocop/cop/style/date_time.rb +12 -1
  121. data/lib/rubocop/cop/style/dir.rb +1 -0
  122. data/lib/rubocop/cop/style/double_negation.rb +1 -0
  123. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  124. data/lib/rubocop/cop/style/eval_with_location.rb +1 -3
  125. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  126. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  127. data/lib/rubocop/cop/style/explicit_block_argument.rb +7 -3
  128. data/lib/rubocop/cop/style/float_division.rb +2 -0
  129. data/lib/rubocop/cop/style/for.rb +0 -4
  130. data/lib/rubocop/cop/style/format_string.rb +1 -4
  131. data/lib/rubocop/cop/style/format_string_token.rb +1 -1
  132. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +24 -5
  133. data/lib/rubocop/cop/style/hash_transform_keys.rb +5 -11
  134. data/lib/rubocop/cop/style/hash_transform_values.rb +5 -11
  135. data/lib/rubocop/cop/style/if_unless_modifier.rb +0 -4
  136. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  137. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -6
  138. data/lib/rubocop/cop/style/lambda_call.rb +3 -1
  139. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  140. data/lib/rubocop/cop/style/mixin_usage.rb +8 -27
  141. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -2
  142. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +14 -1
  143. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -0
  144. data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -0
  145. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  146. data/lib/rubocop/cop/style/non_nil_check.rb +2 -0
  147. data/lib/rubocop/cop/style/not.rb +1 -0
  148. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -3
  149. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -1
  150. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +12 -1
  151. data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -0
  152. data/lib/rubocop/cop/style/raise_args.rb +2 -3
  153. data/lib/rubocop/cop/style/random_with_offset.rb +4 -3
  154. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -9
  155. data/lib/rubocop/cop/style/redundant_begin.rb +36 -8
  156. data/lib/rubocop/cop/style/redundant_condition.rb +5 -1
  157. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -5
  158. data/lib/rubocop/cop/style/redundant_exception.rb +1 -3
  159. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  160. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -1
  161. data/lib/rubocop/cop/style/redundant_interpolation.rb +6 -1
  162. data/lib/rubocop/cop/style/redundant_parentheses.rb +14 -6
  163. data/lib/rubocop/cop/style/redundant_percent_q.rb +9 -11
  164. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +39 -24
  165. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -15
  166. data/lib/rubocop/cop/style/redundant_return.rb +17 -17
  167. data/lib/rubocop/cop/style/redundant_self.rb +7 -9
  168. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  169. data/lib/rubocop/cop/style/redundant_sort.rb +12 -29
  170. data/lib/rubocop/cop/style/redundant_sort_by.rb +5 -9
  171. data/lib/rubocop/cop/style/rescue_standard_error.rb +20 -16
  172. data/lib/rubocop/cop/style/safe_navigation.rb +5 -0
  173. data/lib/rubocop/cop/style/sample.rb +2 -1
  174. data/lib/rubocop/cop/style/send.rb +2 -3
  175. data/lib/rubocop/cop/style/signal_exception.rb +2 -0
  176. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  177. data/lib/rubocop/cop/style/slicing_with_range.rb +2 -1
  178. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  179. data/lib/rubocop/cop/style/string_concatenation.rb +17 -3
  180. data/lib/rubocop/cop/style/strip.rb +1 -0
  181. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  182. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +4 -3
  183. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  184. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -5
  185. data/lib/rubocop/cop/util.rb +0 -1
  186. data/lib/rubocop/cop/variable_force/branch.rb +0 -4
  187. data/lib/rubocop/core_ext/string.rb +1 -1
  188. data/lib/rubocop/directive_comment.rb +32 -0
  189. data/lib/rubocop/ext/regexp_node.rb +23 -7
  190. data/lib/rubocop/formatter/disabled_config_formatter.rb +12 -5
  191. data/lib/rubocop/options.rb +37 -17
  192. data/lib/rubocop/result_cache.rb +38 -15
  193. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  194. data/lib/rubocop/rspec/expect_offense.rb +5 -5
  195. data/lib/rubocop/runner.rb +37 -18
  196. data/lib/rubocop/target_finder.rb +27 -26
  197. data/lib/rubocop/target_ruby.rb +1 -1
  198. data/lib/rubocop/version.rb +6 -1
  199. metadata +19 -18
  200. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +0 -43
  201. data/lib/rubocop/cop/tokens_util.rb +0 -84
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # alive_threads.map do |t|
23
23
  # t.object_id
24
24
  # end
25
- class MultilineBlockChain < Cop
25
+ class MultilineBlockChain < Base
26
26
  include RangeHelp
27
27
 
28
28
  MSG = 'Avoid multi-line chains of blocks.'
@@ -36,7 +36,7 @@ module RuboCop
36
36
  range = range_between(receiver.loc.end.begin_pos,
37
37
  node.send_node.source_range.end_pos)
38
38
 
39
- add_offense(nil, location: range)
39
+ add_offense(range)
40
40
 
41
41
  # Done. If there are more blocks in the chain, they will be
42
42
  # found by subsequent calls to on_block.
@@ -5,6 +5,9 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for multi-line ternary op expressions.
7
7
  #
8
+ # NOTE: `return if ... else ... end` is syntax error. If `return` is used before
9
+ # multiline ternary operator expression, it cannot be auto-corrected.
10
+ #
8
11
  # @example
9
12
  # # bad
10
13
  # a = cond ?
@@ -29,9 +32,13 @@ module RuboCop
29
32
  'use `if` or `unless` instead.'
30
33
 
31
34
  def on_if(node)
32
- return unless node.ternary? && node.multiline?
35
+ return unless offense?(node)
33
36
 
34
37
  add_offense(node) do |corrector|
38
+ # `return if ... else ... end` is syntax error. If `return` is used before
39
+ # multiline ternary operator expression, it cannot be auto-corrected.
40
+ next unless offense?(node) && !node.parent.return_type?
41
+
35
42
  corrector.replace(node, <<~RUBY.chop)
36
43
  if #{node.condition.source}
37
44
  #{node.if_branch.source}
@@ -41,6 +48,12 @@ module RuboCop
41
48
  RUBY
42
49
  end
43
50
  end
51
+
52
+ private
53
+
54
+ def offense?(node)
55
+ node.ternary? && node.multiline?
56
+ end
44
57
  end
45
58
  end
46
59
  end
@@ -58,6 +58,7 @@ module RuboCop
58
58
  private
59
59
 
60
60
  def require_then?(when_node)
61
+ return true if when_node.conditions.count >= 2
61
62
  return false unless when_node.body
62
63
 
63
64
  when_node.loc.line == when_node.body.loc.line
@@ -49,6 +49,8 @@ module RuboCop
49
49
  end
50
50
 
51
51
  def remove_parentheses(source)
52
+ return source unless source.start_with?('(')
53
+
52
54
  source.gsub(/\A\(/, '').gsub(/\)\z/, '')
53
55
  end
54
56
  end
@@ -35,6 +35,8 @@ module RuboCop
35
35
  PREDICATE_MSG = 'Prefer the use of the `nil?` predicate.'
36
36
  EXPLICIT_MSG = 'Prefer the use of the `==` comparison.'
37
37
 
38
+ RESTRICT_ON_SEND = %i[== === nil?].freeze
39
+
38
40
  def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
39
41
  def_node_matcher :nil_check?, '(send _ :nil?)'
40
42
 
@@ -41,6 +41,8 @@ module RuboCop
41
41
  class NonNilCheck < Base
42
42
  extend AutoCorrector
43
43
 
44
+ RESTRICT_ON_SEND = %i[!= nil? !].freeze
45
+
44
46
  def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
45
47
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
46
48
  def_node_matcher :nil_check?, '(send _ :nil?)'
@@ -18,6 +18,7 @@ module RuboCop
18
18
  extend AutoCorrector
19
19
 
20
20
  MSG = 'Use `!` instead of `not`.'
21
+ RESTRICT_ON_SEND = %i[!].freeze
21
22
 
22
23
  OPPOSITE_METHODS = {
23
24
  :== => :!=,
@@ -54,11 +54,9 @@ module RuboCop
54
54
  'negative?' => '<'
55
55
  }.freeze
56
56
 
57
- COMPARISON_METHODS = %i[== > < positive? negative? zero?].to_set.freeze
57
+ RESTRICT_ON_SEND = %i[== > < positive? negative? zero?].freeze
58
58
 
59
59
  def on_send(node)
60
- return unless COMPARISON_METHODS.include?(node.method_name)
61
-
62
60
  numeric, replacement = check(node)
63
61
  return unless numeric
64
62
 
@@ -89,7 +89,9 @@ module RuboCop
89
89
  end
90
90
 
91
91
  def else_branch_to_multiline(else_branch, indentation)
92
- if else_branch.if_type? && else_branch.elsif?
92
+ if else_branch.nil?
93
+ 'end'
94
+ elsif else_branch.if_type? && else_branch.elsif?
93
95
  multiline_replacement(else_branch, indentation)
94
96
  else
95
97
  <<~RUBY.chomp
@@ -4,7 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # This cop checks for places where keyword arguments can be used instead of
7
- # boolean arguments when defining methods.
7
+ # boolean arguments when defining methods. `respond_to_missing?` method is allowed by default.
8
+ # These are customizable with `AllowedMethods` option.
8
9
  #
9
10
  # @example
10
11
  # # bad
@@ -23,11 +24,21 @@ module RuboCop
23
24
  # puts bar
24
25
  # end
25
26
  #
27
+ # @example AllowedMethods: ['some_method']
28
+ # # good
29
+ # def some_method(bar = false)
30
+ # puts bar
31
+ # end
32
+ #
26
33
  class OptionalBooleanParameter < Base
34
+ include AllowedMethods
35
+
27
36
  MSG = 'Use keyword arguments when defining method with boolean argument.'
28
37
  BOOLEAN_TYPES = %i[true false].freeze
29
38
 
30
39
  def on_def(node)
40
+ return if allowed_method?(node.method_name)
41
+
31
42
  node.arguments.each do |arg|
32
43
  next unless arg.optarg_type?
33
44
 
@@ -36,6 +36,8 @@ module RuboCop
36
36
  verbose: %i[key? value?]
37
37
  }.freeze
38
38
 
39
+ RESTRICT_ON_SEND = OFFENDING_SELECTORS.values.flatten.freeze
40
+
39
41
  def on_send(node)
40
42
  return unless node.arguments.one? && offending_selector?(node.method_name)
41
43
 
@@ -41,6 +41,8 @@ module RuboCop
41
41
  COMPACT_MSG = 'Provide an exception object ' \
42
42
  'as an argument to `%<method>s`.'
43
43
 
44
+ RESTRICT_ON_SEND = %i[raise fail].freeze
45
+
44
46
  def on_send(node)
45
47
  return unless node.command?(:raise) || node.command?(:fail)
46
48
 
@@ -82,8 +84,6 @@ module RuboCop
82
84
 
83
85
  def check_compact(node)
84
86
  if node.arguments.size > 1
85
- return unless opposite_style_detected
86
-
87
87
  add_offense(node, message: format(COMPACT_MSG, method: node.method_name)) do |corrector|
88
88
  replacement = correction_exploded_to_compact(node)
89
89
 
@@ -101,7 +101,6 @@ module RuboCop
101
101
 
102
102
  return unless first_arg.send_type? && first_arg.method?(:new)
103
103
  return if acceptable_exploded_args?(first_arg.arguments)
104
- return unless opposite_style_detected
105
104
 
106
105
  add_offense(node, message: format(EXPLODED_MSG, method: node.method_name)) do |corrector|
107
106
  replacement = correction_compact_to_exploded(node)
@@ -28,6 +28,7 @@ module RuboCop
28
28
 
29
29
  MSG = 'Prefer ranges when generating random numbers instead of ' \
30
30
  'integers with offsets.'
31
+ RESTRICT_ON_SEND = %i[+ - succ pred next].freeze
31
32
 
32
33
  def_node_matcher :integer_op_rand?, <<~PATTERN
33
34
  (send
@@ -35,7 +36,7 @@ module RuboCop
35
36
  (send
36
37
  {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)}
37
38
  :rand
38
- {int irange erange}))
39
+ {int (irange int int) (erange int int)}))
39
40
  PATTERN
40
41
 
41
42
  def_node_matcher :rand_op_integer?, <<~PATTERN
@@ -43,7 +44,7 @@ module RuboCop
43
44
  (send
44
45
  {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)}
45
46
  :rand
46
- {int irange erange})
47
+ {int (irange int int) (erange int int)})
47
48
  {:+ :-}
48
49
  int)
49
50
  PATTERN
@@ -53,7 +54,7 @@ module RuboCop
53
54
  (send
54
55
  {nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)}
55
56
  :rand
56
- {int irange erange})
57
+ {int (irange int int) (erange int int)})
57
58
  {:succ :pred :next})
58
59
  PATTERN
59
60
 
@@ -94,21 +94,13 @@ module RuboCop
94
94
  add_offense(assignment) do |corrector|
95
95
  expression = assignment.children[1]
96
96
  corrector.replace(assignment, expression.source)
97
- corrector.remove(right_sibling_of(assignment))
97
+ corrector.remove(assignment.right_sibling)
98
98
  end
99
99
  else
100
100
  last_expr = node.children.last
101
101
  check_branch(last_expr)
102
102
  end
103
103
  end
104
-
105
- def right_sibling_of(node)
106
- siblings_of(node)[node.sibling_index + 1]
107
- end
108
-
109
- def siblings_of(node)
110
- node.parent.children
111
- end
112
104
  end
113
105
  end
114
106
  end
@@ -28,6 +28,14 @@ module RuboCop
28
28
  # end
29
29
  #
30
30
  # # bad
31
+ # begin
32
+ # do_something
33
+ # end
34
+ #
35
+ # # good
36
+ # do_something
37
+ #
38
+ # # bad
31
39
  # # When using Ruby 2.5 or later.
32
40
  # do_something do
33
41
  # begin
@@ -60,7 +68,9 @@ module RuboCop
60
68
  MSG = 'Redundant `begin` block detected.'
61
69
 
62
70
  def on_def(node)
63
- check(node)
71
+ return unless node.body&.kwbegin_type?
72
+
73
+ register_offense(node.body)
64
74
  end
65
75
  alias on_defs on_def
66
76
 
@@ -69,20 +79,38 @@ module RuboCop
69
79
 
70
80
  return if node.send_node.lambda_literal?
71
81
  return if node.braces?
82
+ return unless node.body&.kwbegin_type?
72
83
 
73
- check(node)
84
+ register_offense(node.body)
74
85
  end
75
86
 
76
- private
87
+ def on_kwbegin(node)
88
+ return if contain_rescue_or_ensure?(node) || valid_context_using_only_begin?(node)
77
89
 
78
- def check(node)
79
- return unless node.body&.kwbegin_type?
90
+ register_offense(node)
91
+ end
92
+
93
+ private
80
94
 
81
- add_offense(node.body.loc.begin) do |corrector|
82
- corrector.remove(node.body.loc.begin)
83
- corrector.remove(node.body.loc.end)
95
+ def register_offense(node)
96
+ add_offense(node.loc.begin) do |corrector|
97
+ corrector.remove(node.loc.begin)
98
+ corrector.remove(node.loc.end)
84
99
  end
85
100
  end
101
+
102
+ def contain_rescue_or_ensure?(node)
103
+ first_child = node.children.first
104
+
105
+ first_child.rescue_type? || first_child.ensure_type?
106
+ end
107
+
108
+ def valid_context_using_only_begin?(node)
109
+ parent = node.parent
110
+
111
+ node.each_ancestor.any?(&:assignment?) || parent&.post_condition_loop? ||
112
+ parent&.send_type? || parent&.operator_keyword?
113
+ end
86
114
  end
87
115
  end
88
116
  end
@@ -75,7 +75,7 @@ module RuboCop
75
75
  def offense?(node)
76
76
  condition, if_branch, else_branch = *node
77
77
 
78
- return false if use_if_branch?(else_branch)
78
+ return false if use_if_branch?(else_branch) || use_hash_key_assignment?(else_branch)
79
79
 
80
80
  condition == if_branch && !node.elsif? && (
81
81
  node.ternary? ||
@@ -88,6 +88,10 @@ module RuboCop
88
88
  else_branch&.if_type?
89
89
  end
90
90
 
91
+ def use_hash_key_assignment?(else_branch)
92
+ else_branch&.send_type? && else_branch&.method?(:[]=)
93
+ end
94
+
91
95
  def else_source(else_branch)
92
96
  if require_parentheses?(else_branch)
93
97
  "(#{else_branch.source})"
@@ -24,8 +24,9 @@ module RuboCop
24
24
  #
25
25
  # # good
26
26
  # x != y
27
- class RedundantConditional < Cop
27
+ class RedundantConditional < Base
28
28
  include Alignment
29
+ extend AutoCorrector
29
30
 
30
31
  operators = RuboCop::AST::Node::COMPARISON_OPERATORS.to_a
31
32
  COMPARISON_OPERATOR_MATCHER = "{:#{operators.join(' :')}}"
@@ -36,11 +37,9 @@ module RuboCop
36
37
  def on_if(node)
37
38
  return unless offense?(node)
38
39
 
39
- add_offense(node)
40
- end
40
+ message = message(node)
41
41
 
42
- def autocorrect(node)
43
- lambda do |corrector|
42
+ add_offense(node, message: message) do |corrector|
44
43
  corrector.replace(node, replacement_condition(node))
45
44
  end
46
45
  end
@@ -23,13 +23,11 @@ module RuboCop
23
23
  MSG_2 = 'Redundant `RuntimeError.new` call can be replaced with ' \
24
24
  'just the message.'
25
25
 
26
- RAISE_METHODS = %i[raise fail].freeze
26
+ RESTRICT_ON_SEND = %i[raise fail].freeze
27
27
 
28
28
  # Switch `raise RuntimeError, 'message'` to `raise 'message'`, and
29
29
  # `raise RuntimeError.new('message')` to `raise 'message'`.
30
30
  def on_send(node)
31
- return unless RAISE_METHODS.include?(node.method_name)
32
-
33
31
  fix_exploded(node) || fix_compact(node)
34
32
  end
35
33
 
@@ -28,6 +28,7 @@ module RuboCop
28
28
  extend AutoCorrector
29
29
 
30
30
  MSG = 'Redundant `.rb` file extension detected.'
31
+ RESTRICT_ON_SEND = %i[require require_relative].freeze
31
32
 
32
33
  def_node_matcher :require_call?, <<~PATTERN
33
34
  (send nil? {:require :require_relative} $str_type?)
@@ -17,9 +17,10 @@ module RuboCop
17
17
 
18
18
  MSG = 'Do not freeze immutable objects, as freezing them has no ' \
19
19
  'effect.'
20
+ RESTRICT_ON_SEND = %i[freeze].freeze
20
21
 
21
22
  def on_send(node)
22
- return unless node.receiver && node.method?(:freeze) &&
23
+ return unless node.receiver &&
23
24
  (immutable_literal?(node.receiver) ||
24
25
  operation_produces_immutable_object?(node.receiver))
25
26
 
@@ -51,7 +51,12 @@ module RuboCop
51
51
  end
52
52
 
53
53
  def single_variable_interpolation?(node)
54
- node.children.one? && variable_interpolation?(node.children.first)
54
+ return false unless node.children.one?
55
+
56
+ first_child = node.children.first
57
+
58
+ variable_interpolation?(first_child) ||
59
+ first_child.send_type? && !first_child.operator_method?
55
60
  end
56
61
 
57
62
  def interpolation?(node)
@@ -26,9 +26,7 @@ module RuboCop
26
26
  '^^(block (send _ _ equal?(%0) ...) ...)'
27
27
 
28
28
  def on_begin(node)
29
- return if !parentheses?(node) || parens_allowed?(node)
30
- return if node.parent && (node.parent.while_post_type? ||
31
- node.parent.until_post_type?)
29
+ return if !parentheses?(node) || parens_allowed?(node) || ignore_syntax?(node)
32
30
 
33
31
  check(node)
34
32
  end
@@ -42,6 +40,13 @@ module RuboCop
42
40
  allowed_expression?(node)
43
41
  end
44
42
 
43
+ def ignore_syntax?(node)
44
+ return false unless (parent = node.parent)
45
+
46
+ parent.while_post_type? || parent.until_post_type? ||
47
+ like_method_argument_parentheses?(parent)
48
+ end
49
+
45
50
  def allowed_expression?(node)
46
51
  allowed_ancestor?(node) ||
47
52
  allowed_method_call?(node) ||
@@ -68,6 +73,10 @@ module RuboCop
68
73
  !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
69
74
  end
70
75
 
76
+ def like_method_argument_parentheses?(node)
77
+ node.send_type? && node.arguments.size == 1 && !node.arithmetic_operation?
78
+ end
79
+
71
80
  def empty_parentheses?(node)
72
81
  # Don't flag `()`
73
82
  node.children.empty?
@@ -166,10 +175,9 @@ module RuboCop
166
175
  def raised_to_power_negative_numeric?(begin_node, node)
167
176
  return false unless node.numeric_type?
168
177
 
169
- siblings = begin_node.parent&.children
170
- return false if siblings.nil?
178
+ next_sibling = begin_node.right_sibling
179
+ return false unless next_sibling
171
180
 
172
- next_sibling = siblings[begin_node.sibling_index + 1]
173
181
  base_value = node.children.first
174
182
 
175
183
  base_value.negative? && next_sibling == :**