rubocop 0.90.0 → 0.91.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +35 -0
  4. data/lib/rubocop.rb +10 -1
  5. data/lib/rubocop/cli/command/execute_runner.rb +8 -0
  6. data/lib/rubocop/config_loader.rb +3 -3
  7. data/lib/rubocop/config_store.rb +3 -3
  8. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -1
  9. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  10. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -0
  11. data/lib/rubocop/cop/generator.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  13. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  14. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -0
  15. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -0
  17. data/lib/rubocop/cop/layout/begin_end_alignment.rb +77 -0
  18. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  19. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +6 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +5 -10
  21. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +17 -4
  22. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -3
  23. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -6
  24. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -0
  25. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -2
  26. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +54 -0
  27. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  28. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -3
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -4
  30. data/lib/rubocop/cop/lint/duplicate_require.rb +7 -2
  31. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  32. data/lib/rubocop/cop/lint/empty_file.rb +1 -4
  33. data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -0
  34. data/lib/rubocop/cop/lint/float_comparison.rb +2 -2
  35. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  36. data/lib/rubocop/cop/lint/identity_comparison.rb +49 -0
  37. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  38. data/lib/rubocop/cop/lint/multiple_comparison.rb +3 -1
  39. data/lib/rubocop/cop/lint/number_conversion.rb +1 -0
  40. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -2
  41. data/lib/rubocop/cop/lint/raise_exception.rb +1 -0
  42. data/lib/rubocop/cop/lint/rand_one.rb +2 -1
  43. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  44. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +3 -1
  45. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  46. data/lib/rubocop/cop/lint/to_json.rb +16 -5
  47. data/lib/rubocop/cop/lint/unreachable_loop.rb +2 -1
  48. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +3 -1
  49. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -1
  50. data/lib/rubocop/cop/lint/useless_method_definition.rb +20 -27
  51. data/lib/rubocop/cop/lint/useless_times.rb +97 -0
  52. data/lib/rubocop/cop/mixin/comments_help.rb +3 -9
  53. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  54. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +9 -0
  55. data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -1
  56. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  57. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  58. data/lib/rubocop/cop/security/eval.rb +1 -0
  59. data/lib/rubocop/cop/security/json_load.rb +1 -0
  60. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  61. data/lib/rubocop/cop/security/open.rb +1 -0
  62. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  63. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -11
  64. data/lib/rubocop/cop/style/alias.rb +2 -0
  65. data/lib/rubocop/cop/style/array_join.rb +1 -0
  66. data/lib/rubocop/cop/style/attr.rb +1 -0
  67. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -0
  68. data/lib/rubocop/cop/style/case_equality.rb +3 -0
  69. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -0
  70. data/lib/rubocop/cop/style/class_check.rb +6 -9
  71. data/lib/rubocop/cop/style/class_methods_definitions.rb +42 -16
  72. data/lib/rubocop/cop/style/class_vars.rb +1 -2
  73. data/lib/rubocop/cop/style/conditional_assignment.rb +49 -60
  74. data/lib/rubocop/cop/style/dir.rb +1 -0
  75. data/lib/rubocop/cop/style/double_negation.rb +1 -0
  76. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  77. data/lib/rubocop/cop/style/eval_with_location.rb +1 -3
  78. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  79. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  80. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  81. data/lib/rubocop/cop/style/float_division.rb +2 -0
  82. data/lib/rubocop/cop/style/format_string.rb +1 -4
  83. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +12 -2
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +5 -11
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +5 -11
  86. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  87. data/lib/rubocop/cop/style/lambda_call.rb +3 -1
  88. data/lib/rubocop/cop/style/mixin_usage.rb +1 -0
  89. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +14 -1
  90. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  91. data/lib/rubocop/cop/style/non_nil_check.rb +2 -0
  92. data/lib/rubocop/cop/style/not.rb +1 -0
  93. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -3
  94. data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -0
  95. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  96. data/lib/rubocop/cop/style/random_with_offset.rb +1 -0
  97. data/lib/rubocop/cop/style/redundant_exception.rb +1 -3
  98. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  99. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -1
  100. data/lib/rubocop/cop/style/redundant_parentheses.rb +12 -3
  101. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  102. data/lib/rubocop/cop/style/redundant_sort.rb +1 -7
  103. data/lib/rubocop/cop/style/safe_navigation.rb +5 -0
  104. data/lib/rubocop/cop/style/sample.rb +2 -1
  105. data/lib/rubocop/cop/style/send.rb +2 -3
  106. data/lib/rubocop/cop/style/signal_exception.rb +2 -0
  107. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  108. data/lib/rubocop/cop/style/slicing_with_range.rb +2 -1
  109. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  110. data/lib/rubocop/cop/style/string_concatenation.rb +16 -2
  111. data/lib/rubocop/cop/style/strip.rb +1 -0
  112. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  113. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -5
  114. data/lib/rubocop/core_ext/string.rb +1 -1
  115. data/lib/rubocop/ext/regexp_node.rb +4 -4
  116. data/lib/rubocop/options.rb +19 -1
  117. data/lib/rubocop/result_cache.rb +30 -13
  118. data/lib/rubocop/runner.rb +29 -14
  119. data/lib/rubocop/version.rb +1 -1
  120. metadata +8 -4
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # end
24
24
  class RescueEnsureAlignment < Base
25
25
  include RangeHelp
26
+ include EndKeywordAlignment
26
27
  extend AutoCorrector
27
28
 
28
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
@@ -59,7 +60,7 @@ module RuboCop
59
60
  alignment_node = alignment_node(node)
60
61
  return if alignment_node.nil?
61
62
 
62
- alignment_loc = alignment_node.loc.expression
63
+ alignment_loc = alignment_location(alignment_node)
63
64
  kw_loc = node.loc.keyword
64
65
 
65
66
  return if alignment_loc.column == kw_loc.column || alignment_loc.line == kw_loc.line
@@ -67,16 +68,16 @@ module RuboCop
67
68
  add_offense(
68
69
  kw_loc, message: format_message(alignment_node, alignment_loc, kw_loc)
69
70
  ) do |corrector|
70
- autocorrect(corrector, node, alignment_node)
71
+ autocorrect(corrector, node, alignment_loc)
71
72
  end
72
73
  end
73
74
 
74
- def autocorrect(corrector, node, alignment_node)
75
+ def autocorrect(corrector, node, alignment_location)
75
76
  whitespace = whitespace_range(node)
76
77
  # Some inline node is sitting before current node.
77
78
  return nil unless whitespace.source.strip.empty?
78
79
 
79
- new_column = alignment_node.loc.column
80
+ new_column = alignment_location.column
80
81
 
81
82
  corrector.replace(whitespace, ' ' * new_column)
82
83
  end
@@ -180,6 +181,18 @@ module RuboCop
180
181
 
181
182
  false
182
183
  end
184
+
185
+ def alignment_location(alignment_node)
186
+ if begin_end_alignment_style == 'start_of_line'
187
+ start_line_range(alignment_node)
188
+ else
189
+ alignment_node.loc.expression
190
+ end
191
+ end
192
+
193
+ def begin_end_alignment_style
194
+ config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
195
+ end
183
196
  end
184
197
  end
185
198
  end
@@ -126,12 +126,11 @@ module RuboCop
126
126
 
127
127
  def incorrect_style_detected(token1, token2,
128
128
  expect_space, is_empty_braces)
129
+ return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)
130
+
129
131
  brace = (token1.text == '{' ? token1 : token2).pos
130
132
  range = expect_space ? brace : space_range(brace)
131
133
 
132
- style = expect_space ? :no_space : :space
133
- return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)
134
-
135
134
  add_offense(range, message: message(brace, is_empty_braces, expect_space)) do |corrector|
136
135
  autocorrect(corrector, range)
137
136
  end
@@ -61,11 +61,10 @@ module RuboCop
61
61
  MSG = '%<command>s space inside reference brackets.'
62
62
  EMPTY_MSG = '%<command>s space inside empty reference brackets.'
63
63
 
64
- BRACKET_METHODS = %i[[] []=].freeze
64
+ RESTRICT_ON_SEND = %i[[] []=].freeze
65
65
 
66
66
  def on_send(node)
67
67
  return if node.multiline?
68
- return unless bracket_method?(node)
69
68
 
70
69
  tokens = tokens(node)
71
70
  left_token = left_ref_bracket(node, tokens)
@@ -104,10 +103,6 @@ module RuboCop
104
103
  [left, closing_bracket(tokens, left)]
105
104
  end
106
105
 
107
- def bracket_method?(node)
108
- BRACKET_METHODS.include?(node.method_name)
109
- end
110
-
111
106
  def left_ref_bracket(node, tokens)
112
107
  current_token = tokens.reverse.find(&:left_ref_bracket?)
113
108
  previous_token = previous_token(current_token)
@@ -43,6 +43,8 @@ module RuboCop
43
43
  next unless diagnostic.reason == :ambiguous_prefix
44
44
 
45
45
  offense_node = find_offense_node_by(diagnostic)
46
+ next unless offense_node
47
+
46
48
  message = message(diagnostic)
47
49
 
48
50
  add_offense(
@@ -19,6 +19,7 @@ module RuboCop
19
19
 
20
20
  MSG = '`%<double_colon>sBigDecimal.new()` is deprecated. ' \
21
21
  'Use `%<double_colon>sBigDecimal()` instead.'
22
+ RESTRICT_ON_SEND = %i[new].freeze
22
23
 
23
24
  def_node_matcher :big_decimal_new, <<~PATTERN
24
25
  (send
@@ -26,8 +27,6 @@ module RuboCop
26
27
  PATTERN
27
28
 
28
29
  def on_send(node)
29
- return unless node.method?(:new)
30
-
31
30
  big_decimal_new(node) do |captured_value|
32
31
  double_colon = captured_value ? '::' : ''
33
32
  message = format(MSG, double_colon: double_colon)
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Do not define constants within a block, since the block's scope does not
7
+ # isolate or namespace the constant in any way.
8
+ #
9
+ # Define the constant outside of the block instead, or use a variable or
10
+ # method if defining the constant in the outer scope would be problematic.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # task :lint do
15
+ # FILES_TO_LINT = Dir['lib/*.rb']
16
+ # end
17
+ #
18
+ # # bad
19
+ # describe 'making a request' do
20
+ # class TestRequest; end
21
+ # end
22
+ #
23
+ # # good
24
+ # task :lint do
25
+ # files_to_lint = Dir['lib/*.rb']
26
+ # end
27
+ #
28
+ # # good
29
+ # describe 'making a request' do
30
+ # let(:test_request) { Class.new }
31
+ # end
32
+ class ConstantDefinitionInBlock < Base
33
+ MSG = 'Do not define constants within a block.'
34
+
35
+ def_node_matcher :constant_assigned_in_block?, <<~PATTERN
36
+ ({^block_type? [^begin_type? ^^block_type?]} nil? ...)
37
+ PATTERN
38
+
39
+ def_node_matcher :module_defined_in_block?, <<~PATTERN
40
+ ({^block_type? [^begin_type? ^^block_type?]} ...)
41
+ PATTERN
42
+
43
+ def on_casgn(node)
44
+ add_offense(node) if constant_assigned_in_block?(node)
45
+ end
46
+
47
+ def on_class(node)
48
+ add_offense(node) if module_defined_in_block?(node)
49
+ end
50
+ alias on_module on_class
51
+ end
52
+ end
53
+ end
54
+ end
@@ -35,10 +35,10 @@ module RuboCop
35
35
  class Debugger < Base
36
36
  MSG = 'Remove debugger entry point `%<source>s`.'
37
37
 
38
- DEBUGGER_METHODS = %i[
38
+ RESTRICT_ON_SEND = %i[
39
39
  debugger byebug remote_byebug pry remote_pry pry_remote console rescue
40
40
  save_and_open_page save_and_open_screenshot save_screenshot irb
41
- ].to_set.freeze
41
+ ].freeze
42
42
 
43
43
  def_node_matcher :kernel?, <<~PATTERN
44
44
  {
@@ -62,7 +62,6 @@ module RuboCop
62
62
  PATTERN
63
63
 
64
64
  def on_send(node)
65
- return unless DEBUGGER_METHODS.include?(node.method_name)
66
65
  return unless debugger_call?(node) || binding_irb?(node)
67
66
 
68
67
  add_offense(node)
@@ -62,11 +62,9 @@ module RuboCop
62
62
  replacement: :block_given?)
63
63
  ].freeze
64
64
 
65
- DEPRECATED_METHODS = DEPRECATED_METHODS_OBJECT.map(&:deprecated_method).freeze
65
+ RESTRICT_ON_SEND = DEPRECATED_METHODS_OBJECT.map(&:deprecated_method).freeze
66
66
 
67
67
  def on_send(node)
68
- return unless DEPRECATED_METHODS.include?(node.method_name)
69
-
70
68
  check(node) do |data|
71
69
  message = format(MSG, current: deprecated_method(data),
72
70
  prefer: replacement_method(data))
@@ -53,8 +53,8 @@ module RuboCop
53
53
  MSG = 'Method `%<method>s` is defined at both %<defined>s and ' \
54
54
  '%<current>s.'
55
55
 
56
- METHOD_DEF_METHODS = %i[alias_method attr_reader attr_writer
57
- attr_accessor attr].to_set.freeze
56
+ RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer
57
+ attr_accessor attr].freeze
58
58
 
59
59
  def initialize(config = nil, options = nil)
60
60
  super
@@ -100,8 +100,6 @@ module RuboCop
100
100
 
101
101
  def_node_matcher :sym_name, '(sym $_name)'
102
102
  def on_send(node)
103
- return unless METHOD_DEF_METHODS.include?(node.method_name)
104
-
105
103
  if (name = alias_method?(node))
106
104
  return if node.ancestors.any?(&:if_type?)
107
105
  return if possible_dsl?(node)
@@ -15,9 +15,14 @@ module RuboCop
15
15
  # require 'foo'
16
16
  # require 'bar'
17
17
  #
18
+ # # good
19
+ # require 'foo'
20
+ # require_relative 'foo'
21
+ #
18
22
  class DuplicateRequire < Base
19
23
  MSG = 'Duplicate `%<method>s` detected.'
20
24
  REQUIRE_METHODS = %i[require require_relative].freeze
25
+ RESTRICT_ON_SEND = REQUIRE_METHODS
21
26
 
22
27
  def_node_matcher :require_call?, <<~PATTERN
23
28
  (send {nil? (const _ :Kernel)} {:#{REQUIRE_METHODS.join(' :')}} _)
@@ -30,8 +35,8 @@ module RuboCop
30
35
  end
31
36
 
32
37
  def on_send(node)
33
- return unless REQUIRE_METHODS.include?(node.method_name) && require_call?(node)
34
- return if @required[node.parent].add?(node.first_argument)
38
+ return unless require_call?(node)
39
+ return if @required[node.parent].add?("#{node.method_name}#{node.first_argument}")
35
40
 
36
41
  add_offense(node, message: format(MSG, method: node.method_name))
37
42
  end
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # sum = numbers.each_with_object(num) { |e, a| a += e }
24
24
  class EachWithObjectArgument < Base
25
25
  MSG = 'The argument to each_with_object cannot be immutable.'
26
+ RESTRICT_ON_SEND = %i[each_with_object].freeze
26
27
 
27
28
  def_node_matcher :each_with_object?, <<~PATTERN
28
29
  ({send csend} _ :each_with_object $_)
@@ -26,10 +26,7 @@ module RuboCop
26
26
  MSG = 'Empty file detected.'
27
27
 
28
28
  def on_new_investigation
29
- return unless offending?
30
-
31
- range = source_range(processed_source.buffer, 1, 0)
32
- add_offense(range)
29
+ add_global_offense(MSG) if offending?
33
30
  end
34
31
 
35
32
  private
@@ -78,6 +78,8 @@ module RuboCop
78
78
  '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
79
79
  ].freeze
80
80
 
81
+ RESTRICT_ON_SEND = %i[new].freeze
82
+
81
83
  def_node_matcher :erb_new_with_non_keyword_arguments, <<~PATTERN
82
84
  (send
83
85
  (const {nil? cbase} :ERB) :new $...)
@@ -35,9 +35,9 @@ module RuboCop
35
35
  FLOAT_RETURNING_METHODS = %i[to_f Float fdiv].freeze
36
36
  FLOAT_INSTANCE_METHODS = %i[@- abs magnitude modulo next_float prev_float quo].to_set.freeze
37
37
 
38
- def on_send(node)
39
- return unless EQUALITY_METHODS.include?(node.method_name)
38
+ RESTRICT_ON_SEND = EQUALITY_METHODS
40
39
 
40
+ def on_send(node)
41
41
  lhs, _method, rhs = *node
42
42
  add_offense(node) if float?(lhs) || float?(rhs)
43
43
  end
@@ -44,10 +44,10 @@ module RuboCop
44
44
  KERNEL = 'Kernel'
45
45
  SHOVEL = '<<'
46
46
  STRING_TYPES = %i[str dstr].freeze
47
- FORMAT_METHODS = %i[format sprintf %].freeze
47
+ RESTRICT_ON_SEND = %i[format sprintf %].freeze
48
48
 
49
49
  def on_send(node)
50
- return unless FORMAT_METHODS.include?(node.method_name) && format_string?(node)
50
+ return unless format_string?(node)
51
51
 
52
52
  if invalid_format_string?(node)
53
53
  add_offense(node.loc.selector, message: MSG_INVALID)
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ #
7
+ # Prefer `equal?` over `==` when comparing `object_id`.
8
+ #
9
+ # `Object#equal?` is provided to compare objects for identity, and in contrast
10
+ # `Object#==` is provided for the purpose of doing value comparison.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # foo.object_id == bar.object_id
15
+ #
16
+ # # good
17
+ # foo.equal?(bar)
18
+ #
19
+ class IdentityComparison < Base
20
+ extend AutoCorrector
21
+
22
+ MSG = 'Use `equal?` instead `==` when comparing `object_id`.'
23
+ RESTRICT_ON_SEND = %i[==].freeze
24
+
25
+ def on_send(node)
26
+ return unless compare_between_object_id_by_double_equal?(node)
27
+
28
+ add_offense(node) do |corrector|
29
+ receiver = node.receiver.receiver.source
30
+ argument = node.first_argument.receiver.source
31
+ replacement = "#{receiver}.equal?(#{argument})"
32
+
33
+ corrector.replace(node, replacement)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def compare_between_object_id_by_double_equal?(node)
40
+ object_id_method?(node.receiver) && object_id_method?(node.first_argument)
41
+ end
42
+
43
+ def object_id_method?(node)
44
+ node.send_type? && node.method?(:object_id)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -56,6 +56,8 @@ module RuboCop
56
56
  SystemExit
57
57
  ].freeze
58
58
 
59
+ RESTRICT_ON_SEND = %i[new].freeze
60
+
59
61
  def_node_matcher :class_new_call?, <<~PATTERN
60
62
  (send
61
63
  (const {cbase nil?} :Class) :new
@@ -74,8 +76,6 @@ module RuboCop
74
76
  end
75
77
 
76
78
  def on_send(node)
77
- return unless node.method?(:new)
78
-
79
79
  constant = class_new_call?(node)
80
80
  return unless constant && illegal_class_name?(constant)
81
81
 
@@ -25,9 +25,11 @@ module RuboCop
25
25
  extend AutoCorrector
26
26
 
27
27
  MSG = 'Use the `&&` operator to compare multiple values.'
28
+ COMPARISON_METHODS = %i[< > <= >=].freeze
29
+ RESTRICT_ON_SEND = COMPARISON_METHODS
28
30
 
29
31
  def_node_matcher :multiple_compare?, <<~PATTERN
30
- (send (send _ {:< :> :<= :>=} $_) {:< :> :<= :>=} _)
32
+ (send (send _ {:< :> :<= :>=} $_) {:#{COMPARISON_METHODS.join(' :')}} _)
31
33
  PATTERN
32
34
 
33
35
  def on_send(node)
@@ -32,6 +32,7 @@ module RuboCop
32
32
  'class parsing, instead of using '\
33
33
  '%<number_object>s.%<to_method>s, use stricter '\
34
34
  '%<corrected_method>s.'
35
+ RESTRICT_ON_SEND = CONVERSION_METHOD_CLASS_MAPPING.keys.freeze
35
36
 
36
37
  def_node_matcher :to_method, <<~PATTERN
37
38
  (send $_ ${:to_i :to_f :to_c})
@@ -25,6 +25,7 @@ module RuboCop
25
25
  REGEXP_ARGUMENT_METHODS = %i[=~ match grep gsub gsub! sub sub! [] slice slice! index rindex
26
26
  scan partition rpartition start_with? end_with?].to_set.freeze
27
27
  REGEXP_CAPTURE_METHODS = (REGEXP_RECEIVER_METHODS + REGEXP_ARGUMENT_METHODS).freeze
28
+ RESTRICT_ON_SEND = REGEXP_CAPTURE_METHODS
28
29
 
29
30
  def on_new_investigation
30
31
  @valid_ref = 0
@@ -35,8 +36,6 @@ module RuboCop
35
36
  end
36
37
 
37
38
  def on_send(node)
38
- return unless REGEXP_CAPTURE_METHODS.include?(node.method_name)
39
-
40
39
  @valid_ref = nil
41
40
 
42
41
  if node.receiver&.regexp_type?
@@ -31,6 +31,7 @@ module RuboCop
31
31
  extend AutoCorrector
32
32
 
33
33
  MSG = 'Use `StandardError` over `Exception`.'
34
+ RESTRICT_ON_SEND = %i[raise fail].freeze
34
35
 
35
36
  def_node_matcher :exception?, <<~PATTERN
36
37
  (send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
@@ -23,13 +23,14 @@ module RuboCop
23
23
  class RandOne < Base
24
24
  MSG = '`%<method>s` always returns `0`. ' \
25
25
  'Perhaps you meant `rand(2)` or `rand`?'
26
+ RESTRICT_ON_SEND = %i[rand].freeze
26
27
 
27
28
  def_node_matcher :rand_one?, <<~PATTERN
28
29
  (send {(const {nil? cbase} :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
29
30
  PATTERN
30
31
 
31
32
  def on_send(node)
32
- return unless node.method?(:rand) && rand_one?(node)
33
+ return unless rand_one?(node)
33
34
 
34
35
  add_offense(node)
35
36
  end