rubocop 1.6.0 → 1.9.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 (109) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +3 -2
  4. data/config/default.yml +142 -19
  5. data/lib/rubocop.rb +15 -1
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  7. data/lib/rubocop/comment_config.rb +6 -6
  8. data/lib/rubocop/config.rb +10 -7
  9. data/lib/rubocop/config_loader.rb +11 -14
  10. data/lib/rubocop/config_loader_resolver.rb +21 -4
  11. data/lib/rubocop/config_obsoletion.rb +5 -3
  12. data/lib/rubocop/config_obsoletion/extracted_cop.rb +6 -6
  13. data/lib/rubocop/config_store.rb +12 -1
  14. data/lib/rubocop/cop/base.rb +1 -1
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -2
  16. data/lib/rubocop/cop/generator.rb +1 -3
  17. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  18. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  19. data/lib/rubocop/cop/internal_affairs/example_description.rb +89 -0
  20. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +61 -0
  21. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +64 -0
  22. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +145 -0
  23. data/lib/rubocop/cop/layout/class_structure.rb +7 -2
  24. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +19 -3
  25. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +14 -0
  26. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
  27. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -10
  28. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +1 -0
  29. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
  30. data/lib/rubocop/cop/layout/space_before_brackets.rb +62 -0
  31. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +13 -10
  32. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  33. data/lib/rubocop/cop/lint/ambiguous_assignment.rb +59 -0
  34. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +7 -2
  35. data/lib/rubocop/cop/lint/deprecated_constants.rb +75 -0
  36. data/lib/rubocop/cop/lint/duplicate_branch.rb +64 -2
  37. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +44 -0
  38. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +10 -6
  39. data/lib/rubocop/cop/lint/number_conversion.rb +41 -6
  40. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  41. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  42. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +2 -1
  43. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +50 -0
  44. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +50 -17
  45. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -11
  46. data/lib/rubocop/cop/lint/symbol_conversion.rb +102 -0
  47. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  48. data/lib/rubocop/cop/lint/unreachable_loop.rb +17 -0
  49. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  50. data/lib/rubocop/cop/mixin/allowed_identifiers.rb +18 -0
  51. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  52. data/lib/rubocop/cop/mixin/comments_help.rb +1 -11
  53. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  54. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  55. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +59 -5
  56. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  57. data/lib/rubocop/cop/naming/variable_name.rb +2 -0
  58. data/lib/rubocop/cop/naming/variable_number.rb +2 -9
  59. data/lib/rubocop/cop/registry.rb +10 -0
  60. data/lib/rubocop/cop/severity.rb +3 -3
  61. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -1
  62. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  63. data/lib/rubocop/cop/style/collection_methods.rb +14 -1
  64. data/lib/rubocop/cop/style/commented_keyword.rb +22 -5
  65. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  66. data/lib/rubocop/cop/style/empty_literal.rb +6 -2
  67. data/lib/rubocop/cop/style/endless_method.rb +102 -0
  68. data/lib/rubocop/cop/style/eval_with_location.rb +63 -34
  69. data/lib/rubocop/cop/style/explicit_block_argument.rb +10 -0
  70. data/lib/rubocop/cop/style/float_division.rb +3 -0
  71. data/lib/rubocop/cop/style/for.rb +2 -0
  72. data/lib/rubocop/cop/style/format_string_token.rb +18 -2
  73. data/lib/rubocop/cop/style/hash_except.rb +95 -0
  74. data/lib/rubocop/cop/style/hash_like_case.rb +2 -1
  75. data/lib/rubocop/cop/style/if_inside_else.rb +22 -10
  76. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +96 -0
  77. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -2
  78. data/lib/rubocop/cop/style/lambda_call.rb +2 -1
  79. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -0
  80. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +16 -6
  81. data/lib/rubocop/cop/style/method_def_parentheses.rb +7 -0
  82. data/lib/rubocop/cop/style/multiline_method_signature.rb +26 -1
  83. data/lib/rubocop/cop/style/multiline_when_then.rb +3 -1
  84. data/lib/rubocop/cop/style/mutable_constant.rb +13 -3
  85. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -0
  86. data/lib/rubocop/cop/style/nil_comparison.rb +1 -0
  87. data/lib/rubocop/cop/style/non_nil_check.rb +23 -13
  88. data/lib/rubocop/cop/style/raise_args.rb +5 -2
  89. data/lib/rubocop/cop/style/redundant_argument.rb +7 -1
  90. data/lib/rubocop/cop/style/redundant_freeze.rb +8 -4
  91. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  92. data/lib/rubocop/cop/style/single_line_methods.rb +34 -2
  93. data/lib/rubocop/cop/style/sole_nested_conditional.rb +29 -5
  94. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  95. data/lib/rubocop/cop/style/symbol_proc.rb +5 -4
  96. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  97. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  98. data/lib/rubocop/cop/util.rb +3 -1
  99. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  100. data/lib/rubocop/magic_comment.rb +30 -1
  101. data/lib/rubocop/options.rb +10 -10
  102. data/lib/rubocop/rspec/cop_helper.rb +0 -4
  103. data/lib/rubocop/rspec/expect_offense.rb +37 -22
  104. data/lib/rubocop/runner.rb +17 -1
  105. data/lib/rubocop/target_finder.rb +4 -2
  106. data/lib/rubocop/target_ruby.rb +47 -11
  107. data/lib/rubocop/util.rb +16 -0
  108. data/lib/rubocop/version.rb +8 -2
  109. metadata +26 -7
@@ -7,6 +7,9 @@ module RuboCop
7
7
  # It is recommended to either always use `fdiv` or coerce one side only.
8
8
  # This cop also provides other options for code consistency.
9
9
  #
10
+ # This cop is marked as unsafe, because if operand variable is a string object
11
+ # then `.to_f` will be removed and an error will occur.
12
+ #
10
13
  # @example EnforcedStyle: single_coerce (default)
11
14
  # # bad
12
15
  # a.to_f / b.to_f
@@ -51,6 +51,7 @@ module RuboCop
51
51
  if style == :each
52
52
  add_offense(node, message: PREFER_EACH) do |corrector|
53
53
  ForToEachCorrector.new(node).call(corrector)
54
+ opposite_style_detected
54
55
  end
55
56
  else
56
57
  correct_style_detected
@@ -63,6 +64,7 @@ module RuboCop
63
64
  if style == :for
64
65
  add_offense(node, message: PREFER_FOR) do |corrector|
65
66
  EachToForCorrector.new(node).call(corrector)
67
+ opposite_style_detected
66
68
  end
67
69
  else
68
70
  correct_style_detected
@@ -11,6 +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 ignored methods with `IgnoredMethods`.
15
+ #
14
16
  # @example EnforcedStyle: annotated (default)
15
17
  #
16
18
  # # bad
@@ -58,12 +60,18 @@ module RuboCop
58
60
  #
59
61
  # # good
60
62
  # format('%06d', 10)
63
+ #
64
+ # @example IgnoredMethods: [redirect]
65
+ #
66
+ # # good
67
+ # redirect('foo/%{bar_id}')
68
+ #
61
69
  class FormatStringToken < Base
62
70
  include ConfigurableEnforcedStyle
71
+ include IgnoredMethods
63
72
 
64
73
  def on_str(node)
65
- return unless node.value.include?('%')
66
- return if node.each_ancestor(:xstr, :regexp).any?
74
+ return if format_string_token?(node) || use_ignored_method?(node)
67
75
 
68
76
  detections = collect_detections(node)
69
77
  return if detections.empty?
@@ -88,6 +96,14 @@ module RuboCop
88
96
  }
89
97
  PATTERN
90
98
 
99
+ def format_string_token?(node)
100
+ !node.value.include?('%') || node.each_ancestor(:xstr, :regexp).any?
101
+ end
102
+
103
+ def use_ignored_method?(node)
104
+ (parent = node.parent) && parent.send_type? && ignored_method?(parent.method_name)
105
+ end
106
+
91
107
  def unannotated_format?(node, detected_style)
92
108
  detected_style == :unannotated && !format_string_in_typical_context?(node)
93
109
  end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for usages of `Hash#reject`, `Hash#select`, and `Hash#filter` methods
7
+ # that can be replaced with `Hash#except` method.
8
+ #
9
+ # This cop should only be enabled on Ruby version 3.0 or higher.
10
+ # (`Hash#except` was added in Ruby 3.0.)
11
+ #
12
+ # For safe detection, it is limited to commonly used string and symbol comparisons
13
+ # when used `==`.
14
+ # And do not check `Hash#delete_if` and `Hash#keep_if` to change receiver object.
15
+ #
16
+ # @example
17
+ #
18
+ # # bad
19
+ # {foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }
20
+ # {foo: 1, bar: 2, baz: 3}.select {|k, v| k != :bar }
21
+ # {foo: 1, bar: 2, baz: 3}.filter {|k, v| k != :bar }
22
+ #
23
+ # # good
24
+ # {foo: 1, bar: 2, baz: 3}.except(:bar)
25
+ #
26
+ class HashExcept < Base
27
+ include RangeHelp
28
+ extend TargetRubyVersion
29
+ extend AutoCorrector
30
+
31
+ minimum_target_ruby_version 3.0
32
+
33
+ MSG = 'Use `%<prefer>s` instead.'
34
+ RESTRICT_ON_SEND = %i[reject select filter].freeze
35
+
36
+ def_node_matcher :bad_method?, <<~PATTERN
37
+ (block
38
+ (send _ _)
39
+ (args
40
+ (arg _)
41
+ (arg _))
42
+ (send
43
+ _ {:== :!= :eql?} _))
44
+ PATTERN
45
+
46
+ def on_send(node)
47
+ block = node.parent
48
+ return unless bad_method?(block) && semantically_except_method?(node, block)
49
+
50
+ except_key = except_key(block)
51
+ return unless safe_to_register_offense?(block, except_key)
52
+
53
+ range = offense_range(node)
54
+ preferred_method = "except(#{except_key.source})"
55
+
56
+ add_offense(range, message: format(MSG, prefer: preferred_method)) do |corrector|
57
+ corrector.replace(range, preferred_method)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def semantically_except_method?(send, block)
64
+ body = block.body
65
+
66
+ case send.method_name
67
+ when :reject
68
+ body.method?('==') || body.method?('eql?')
69
+ when :select, :filter
70
+ body.method?('!=')
71
+ else
72
+ false
73
+ end
74
+ end
75
+
76
+ def safe_to_register_offense?(block, except_key)
77
+ return true if block.body.method?('eql?')
78
+
79
+ except_key.sym_type? || except_key.str_type?
80
+ end
81
+
82
+ def except_key(node)
83
+ key_argument = node.argument_list.first
84
+ lhs, _method_name, rhs = *node.body
85
+
86
+ [lhs, rhs].find { |operand| operand.source != key_argument.source }
87
+ end
88
+
89
+ def offense_range(node)
90
+ range_between(node.loc.selector.begin_pos, node.parent.loc.end.end_pos)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -68,7 +68,8 @@ module RuboCop
68
68
  length = cop_config['MinBranchesCount'] || 3
69
69
  return length if length.is_a?(Integer) && length.positive?
70
70
 
71
- raise 'MinBranchesCount needs to be a positive integer!'
71
+ warn Rainbow('`MinBranchesCount` needs to be a positive integer!').red
72
+ exit!
72
73
  end
73
74
  end
74
75
  end
@@ -82,15 +82,14 @@ module RuboCop
82
82
  def autocorrect(corrector, node)
83
83
  if node.modifier_form?
84
84
  correct_to_elsif_from_modifier_form(corrector, node)
85
- end_range = node.parent.loc.end
86
85
  else
87
86
  correct_to_elsif_from_if_inside_else_form(corrector, node, node.condition)
88
- end_range = node.loc.end
89
87
  end
90
- corrector.remove(range_by_whole_lines(end_range, include_final_newline: true))
91
- corrector.remove(
92
- range_by_whole_lines(node.if_branch.source_range, include_final_newline: true)
93
- )
88
+ corrector.remove(range_by_whole_lines(find_end_range(node), include_final_newline: true))
89
+ return unless (if_branch = node.if_branch)
90
+
91
+ range = range_by_whole_lines(if_branch.source_range, include_final_newline: true)
92
+ corrector.remove(range)
94
93
  end
95
94
 
96
95
  def correct_to_elsif_from_modifier_form(corrector, node)
@@ -103,13 +102,26 @@ module RuboCop
103
102
 
104
103
  def correct_to_elsif_from_if_inside_else_form(corrector, node, condition)
105
104
  corrector.replace(node.parent.loc.else, "elsif #{condition.source}")
106
- if_condition_range = range_between(
107
- node.loc.keyword.begin_pos, condition.source_range.end_pos
108
- )
109
- corrector.replace(if_condition_range, node.if_branch.source)
105
+ if_condition_range = if_condition_range(node, condition)
106
+ if (if_branch = node.if_branch)
107
+ corrector.replace(if_condition_range, if_branch.source)
108
+ else
109
+ corrector.remove(range_by_whole_lines(if_condition_range, include_final_newline: true))
110
+ end
110
111
  corrector.remove(condition)
111
112
  end
112
113
 
114
+ def find_end_range(node)
115
+ end_range = node.loc.end
116
+ return end_range if end_range
117
+
118
+ find_end_range(node.parent)
119
+ end
120
+
121
+ def if_condition_range(node, condition)
122
+ range_between(node.loc.keyword.begin_pos, condition.source_range.end_pos)
123
+ end
124
+
113
125
  def allow_if_modifier_in_else_branch?(else_branch)
114
126
  allow_if_modifier? && else_branch&.modifier_form?
115
127
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for redundant `if` with boolean literal branches.
7
+ # It checks only conditions to return boolean value (`true` or `false`) for safe detection.
8
+ # The conditions to be checked are comparison methods, predicate methods, and double negative.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # if foo == bar
13
+ # true
14
+ # else
15
+ # false
16
+ # end
17
+ #
18
+ # # bad
19
+ # foo == bar ? true : false
20
+ #
21
+ # # good
22
+ # foo == bar
23
+ #
24
+ class IfWithBooleanLiteralBranches < Base
25
+ extend AutoCorrector
26
+
27
+ MSG = 'Remove redundant %<keyword>s with boolean literal branches.'
28
+
29
+ def_node_matcher :if_with_boolean_literal_branches?, <<~PATTERN
30
+ (if #return_boolean_value? {(true) (false) | (false) (true)})
31
+ PATTERN
32
+ def_node_matcher :double_negative?, '(send (send _ :!) :!)'
33
+
34
+ def on_if(node)
35
+ return unless if_with_boolean_literal_branches?(node)
36
+
37
+ condition = node.condition
38
+ range, keyword = if node.ternary?
39
+ range = condition.source_range.end.join(node.source_range.end)
40
+
41
+ [range, 'ternary operator']
42
+ else
43
+ keyword = node.loc.keyword
44
+
45
+ [keyword, "`#{keyword.source}`"]
46
+ end
47
+
48
+ add_offense(range, message: format(MSG, keyword: keyword)) do |corrector|
49
+ replacement = replacement_condition(node, condition)
50
+
51
+ corrector.replace(node, replacement)
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def return_boolean_value?(condition)
58
+ if condition.begin_type?
59
+ return_boolean_value?(condition.children.first)
60
+ elsif condition.or_type?
61
+ return_boolean_value?(condition.lhs) && return_boolean_value?(condition.rhs)
62
+ elsif condition.and_type?
63
+ return_boolean_value?(condition.rhs)
64
+ else
65
+ assume_boolean_value?(condition)
66
+ end
67
+ end
68
+
69
+ def assume_boolean_value?(condition)
70
+ return false unless condition.send_type?
71
+
72
+ condition.comparison_method? || condition.predicate_method? || double_negative?(condition)
73
+ end
74
+
75
+ def replacement_condition(node, condition)
76
+ bang = '!' if opposite_condition?(node)
77
+
78
+ if bang && require_parentheses?(condition)
79
+ "#{bang}(#{condition.source})"
80
+ else
81
+ "#{bang}#{condition.source}"
82
+ end
83
+ end
84
+
85
+ def opposite_condition?(node)
86
+ !node.unless? && node.if_branch.false_type? || node.unless? && node.if_branch.true_type?
87
+ end
88
+
89
+ def require_parentheses?(condition)
90
+ condition.and_type? || condition.or_type? ||
91
+ condition.send_type? && condition.comparison_method?
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -21,6 +21,16 @@ module RuboCop
21
21
  # # body omitted
22
22
  # end
23
23
  #
24
+ # # bad
25
+ # do_something do |first: false, second:, third: 10|
26
+ # # body omitted
27
+ # end
28
+ #
29
+ # # good
30
+ # do_something do |second:, first: false, third: 10|
31
+ # # body omitted
32
+ # end
33
+ #
24
34
  class KeywordParametersOrder < Base
25
35
  include RangeHelp
26
36
  extend AutoCorrector
@@ -35,7 +45,7 @@ module RuboCop
35
45
  if node.parent.find(&:kwoptarg_type?) == node
36
46
  corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
37
47
 
38
- arguments = node.each_ancestor(:def, :defs).first.arguments
48
+ arguments = node.each_ancestor(:def, :defs, :block).first.arguments
39
49
  append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
40
50
 
41
51
  remove_kwargs(kwarg_nodes, corrector)
@@ -50,7 +60,7 @@ module RuboCop
50
60
  return if last_argument.kwrestarg_type? || last_argument.blockarg_type?
51
61
 
52
62
  last_kwoptarg = arguments.reverse.find(&:kwoptarg_type?)
53
- corrector.insert_after(last_kwoptarg, "\n")
63
+ corrector.insert_after(last_kwoptarg, "\n") unless arguments.parent.block_type?
54
64
  end
55
65
 
56
66
  def remove_kwargs(kwarg_nodes, corrector)
@@ -27,8 +27,9 @@ module RuboCop
27
27
  def on_send(node)
28
28
  return unless node.receiver
29
29
 
30
- if offense?(node) && opposite_style_detected
30
+ if offense?(node)
31
31
  add_offense(node) do |corrector|
32
+ opposite_style_detected
32
33
  autocorrect(corrector, node)
33
34
  end
34
35
  else
@@ -40,6 +40,9 @@ module RuboCop
40
40
  # to `true` allows the presence of parentheses in such a method call
41
41
  # even with arguments.
42
42
  #
43
+ # NOTE: Parens are required around a method with arguments when inside an
44
+ # endless method definition (>= Ruby 3.0).
45
+ #
43
46
  # @example EnforcedStyle: require_parentheses (default)
44
47
  #
45
48
  # # bad
@@ -154,6 +157,10 @@ module RuboCop
154
157
  include OmitParentheses
155
158
  extend AutoCorrector
156
159
 
160
+ def self.autocorrect_incompatible_with
161
+ [Style::NestedParenthesizedCalls]
162
+ end
163
+
157
164
  def on_send(node)
158
165
  send(style, node) # call require_parentheses or omit_parentheses
159
166
  end
@@ -14,25 +14,35 @@ module RuboCop
14
14
 
15
15
  def omit_parentheses(node)
16
16
  return unless node.parenthesized?
17
+ return if inside_endless_method_def?(node)
17
18
  return if node.implicit_call?
18
19
  return if super_call_without_arguments?(node)
19
20
  return if allowed_camel_case_method_call?(node)
20
21
  return if legitimate_call_with_parentheses?(node)
21
22
 
22
23
  add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
23
- if parentheses_at_the_end_of_multiline_call?(node)
24
- corrector.replace(args_begin(node), ' \\')
25
- else
26
- corrector.replace(args_begin(node), ' ')
27
- end
28
- corrector.remove(node.loc.end)
24
+ auto_correct(corrector, node)
25
+ end
26
+ end
27
+
28
+ def auto_correct(corrector, node)
29
+ if parentheses_at_the_end_of_multiline_call?(node)
30
+ corrector.replace(args_begin(node), ' \\')
31
+ else
32
+ corrector.replace(args_begin(node), ' ')
29
33
  end
34
+ corrector.remove(node.loc.end)
30
35
  end
31
36
 
32
37
  def offense_range(node)
33
38
  node.loc.begin.join(node.loc.end)
34
39
  end
35
40
 
41
+ def inside_endless_method_def?(node)
42
+ # parens are required around arguments inside an endless method
43
+ node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
44
+ end
45
+
36
46
  def super_call_without_arguments?(node)
37
47
  node.super_type? && node.arguments.none?
38
48
  end
@@ -6,6 +6,9 @@ module RuboCop
6
6
  # This cop checks for parentheses around the arguments in method
7
7
  # definitions. Both instance and class/singleton methods are checked.
8
8
  #
9
+ # This cop does not consider endless methods, since parentheses are
10
+ # always required for them.
11
+ #
9
12
  # @example EnforcedStyle: require_parentheses (default)
10
13
  # # The `require_parentheses` style requires method definitions
11
14
  # # to always use parentheses
@@ -94,6 +97,8 @@ module RuboCop
94
97
  'parameters.'
95
98
 
96
99
  def on_def(node)
100
+ return if node.endless?
101
+
97
102
  args = node.arguments
98
103
 
99
104
  if require_parentheses?(args)
@@ -142,6 +147,7 @@ module RuboCop
142
147
 
143
148
  add_offense(location, message: MSG_MISSING) do |corrector|
144
149
  correct_definition(node, corrector)
150
+ unexpected_style_detected 'require_no_parentheses'
145
151
  end
146
152
  end
147
153
 
@@ -149,6 +155,7 @@ module RuboCop
149
155
  add_offense(args, message: MSG_PRESENT) do |corrector|
150
156
  # offense is registered on args node when parentheses are unwanted
151
157
  correct_arguments(args, corrector)
158
+ unexpected_style_detected 'require_parentheses'
152
159
  end
153
160
  end
154
161
  end