rubocop 1.92.1 → 1.92.3

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 (160) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -4
  6. data/lib/rubocop/config_obsoletion.rb +9 -0
  7. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  8. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  10. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  11. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  12. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  13. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  14. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  15. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  16. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  17. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +15 -7
  18. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  19. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  25. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  26. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_length.rb +23 -5
  28. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  29. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  30. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  31. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  33. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  34. data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
  35. data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
  39. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  40. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  41. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  43. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  44. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  45. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
  47. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  48. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  49. data/lib/rubocop/cop/lint/nested_percent_literal.rb +3 -0
  50. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  51. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  52. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
  53. data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
  54. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  56. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  57. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  58. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +11 -4
  59. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  60. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  61. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  62. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  63. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  64. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  65. data/lib/rubocop/cop/lint/void.rb +20 -9
  66. data/lib/rubocop/cop/message_annotator.rb +6 -8
  67. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  68. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  69. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  70. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  71. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +17 -1
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  73. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  74. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  75. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  76. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  77. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  78. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -1
  79. data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
  80. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  81. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  82. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  83. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  84. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  85. data/lib/rubocop/cop/style/and_or.rb +41 -11
  86. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  87. data/lib/rubocop/cop/style/case_equality.rb +14 -3
  88. data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
  89. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  90. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  91. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  92. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  93. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  94. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  95. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  96. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  97. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  98. data/lib/rubocop/cop/style/endless_method.rb +64 -16
  99. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  100. data/lib/rubocop/cop/style/float_division.rb +16 -1
  101. data/lib/rubocop/cop/style/for.rb +59 -4
  102. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  103. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  104. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  105. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  106. data/lib/rubocop/cop/style/hash_lookup_method.rb +21 -2
  107. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  108. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  109. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  110. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  111. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  112. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  113. data/lib/rubocop/cop/style/lambda.rb +13 -5
  114. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  115. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  116. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  117. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  118. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +12 -1
  119. data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  121. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  122. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  123. data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
  124. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  125. data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -10
  126. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  127. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  128. data/lib/rubocop/cop/style/quoted_symbols.rb +5 -7
  129. data/lib/rubocop/cop/style/raise_args.rb +66 -7
  130. data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
  131. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  132. data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
  133. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  134. data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
  135. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  136. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  137. data/lib/rubocop/cop/style/redundant_self.rb +9 -1
  138. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  139. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  140. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  141. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  142. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  143. data/lib/rubocop/cop/style/single_line_methods.rb +26 -2
  144. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  145. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  146. data/lib/rubocop/cop/style/string_concatenation.rb +28 -8
  147. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  148. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  149. data/lib/rubocop/cop/style/word_array.rb +1 -3
  150. data/lib/rubocop/cop/util.rb +2 -0
  151. data/lib/rubocop/cop/variable_force.rb +14 -1
  152. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  153. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  154. data/lib/rubocop/plugin/configuration_integrator.rb +24 -3
  155. data/lib/rubocop/result_cache.rb +9 -3
  156. data/lib/rubocop/runner.rb +6 -1
  157. data/lib/rubocop/server/cache.rb +24 -5
  158. data/lib/rubocop/todo_audit.rb +61 -15
  159. data/lib/rubocop/version.rb +1 -1
  160. metadata +4 -3
@@ -38,6 +38,10 @@ module RuboCop
38
38
  RESTRICT_ON_SEND = %i[sort].freeze
39
39
  GLOB_METHODS = %i[glob []].freeze
40
40
 
41
+ def self.autocorrect_incompatible_with
42
+ [Layout::DotPosition]
43
+ end
44
+
41
45
  def on_send(node)
42
46
  return unless dir_glob?(node.receiver)
43
47
  # `sort` with a comparator block or block-pass changes the order, so it is
@@ -75,9 +79,23 @@ module RuboCop
75
79
  corrector.remove(dot)
76
80
  corrector.remove(selector)
77
81
  else
78
- corrector.remove(range_with_surrounding_space(range, side: :left))
82
+ corrector.remove(removal_range(range))
79
83
  end
80
84
  end
85
+
86
+ def removal_range(range)
87
+ range_with_space = range_with_surrounding_space(range, side: :left)
88
+ return range if comment_line_joined_with_code?(range_with_space, range)
89
+
90
+ range_with_space
91
+ end
92
+
93
+ def comment_line_joined_with_code?(range_with_space, range)
94
+ comment = processed_source.comment_at_line(range_with_space.line)
95
+ return false if comment&.source_range&.end_pos != range_with_space.begin_pos
96
+
97
+ !processed_source.lines[range.last_line - 1][range.last_column..].strip.empty?
98
+ end
81
99
  end
82
100
  end
83
101
  end
@@ -33,11 +33,15 @@ module RuboCop
33
33
  # require 'unloaded_feature'
34
34
  class RedundantRequireStatement < Base
35
35
  include RangeHelp
36
+ include RescueNode
36
37
  extend AutoCorrector
37
38
 
38
39
  MSG = 'Remove unnecessary `require` statement.'
39
40
  RESTRICT_ON_SEND = %i[require].freeze
40
41
  RUBY_22_LOADED_FEATURES = %w[rational complex].freeze
42
+ BODY_PARENT_TYPES = %i[
43
+ class module sclass any_def any_block when in_pattern for resbody
44
+ ].freeze
41
45
 
42
46
  # @!method redundant_require_statement?(node)
43
47
  def_node_matcher :redundant_require_statement?, <<~PATTERN
@@ -47,6 +51,7 @@ module RuboCop
47
51
 
48
52
  def on_send(node)
49
53
  return unless redundant_require_statement?(node)
54
+ return unless statement?(node)
50
55
 
51
56
  add_offense(node) do |corrector|
52
57
  if node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
@@ -54,7 +59,7 @@ module RuboCop
54
59
 
55
60
  range = range_with_surrounding_space(node.source_range, side: :right)
56
61
  else
57
- range = range_by_whole_lines(node.source_range, include_final_newline: true)
62
+ range = range_for_statement_removal(node.source_range)
58
63
  end
59
64
 
60
65
  corrector.remove(range)
@@ -73,6 +78,27 @@ module RuboCop
73
78
  (target_ruby_version >= 3.2 && feature_name == 'set') ||
74
79
  (target_ruby_version >= 4.0 && feature_name == 'pathname')
75
80
  end
81
+
82
+ def statement?(node)
83
+ parent = node.parent
84
+ return true unless parent
85
+ return false if value_only_form?(parent)
86
+ return true if parent.type?(:begin, :kwbegin, :rescue, :ensure)
87
+ return !parent.condition.equal?(node) if parent.type?(:if, :while, :until)
88
+ return parent.else_branch.equal?(node) if parent.type?(:case, :case_match)
89
+
90
+ parent.type?(*BODY_PARENT_TYPES) && parent.body.equal?(node)
91
+ end
92
+
93
+ def value_only_form?(node)
94
+ case node.type
95
+ when :if then node.ternary?
96
+ when :rescue then rescue_modifier?(node.resbody_branches.first)
97
+ when :resbody then rescue_modifier?(node)
98
+ when :def, :defs then node.endless?
99
+ else false
100
+ end
101
+ end
76
102
  end
77
103
  end
78
104
  end
@@ -118,10 +118,7 @@ module RuboCop
118
118
  def redundant_splat_expansion(node)
119
119
  literal_expansion(node) do |expanded_item|
120
120
  if expanded_item.send_type?
121
- return if array_new_inside_array_literal?(expanded_item)
122
-
123
- grandparent = node.parent.parent
124
- return if grandparent && !ASSIGNMENT_TYPES.include?(grandparent.type)
121
+ return unless redundant_array_new_splat?(node, expanded_item)
125
122
  # An empty array/percent literal (`*[]`, `*%w()`, ...) expands to nothing, so
126
123
  # removing the splat would produce invalid or semantically different code.
127
124
  elsif expanded_item.array_type? && expanded_item.children.empty?
@@ -132,6 +129,16 @@ module RuboCop
132
129
  end
133
130
  end
134
131
 
132
+ # A splatted `Array.new` passed as a method argument provides the arguments,
133
+ # so the splat is only redundant directly inside an array.
134
+ def redundant_array_new_splat?(node, array_new_node)
135
+ return false unless node.parent.array_type?
136
+ return false if array_new_inside_array_literal?(array_new_node)
137
+
138
+ grandparent = node.parent.parent
139
+ !grandparent || ASSIGNMENT_TYPES.include?(grandparent.type)
140
+ end
141
+
135
142
  def array_new_inside_array_literal?(array_new_node)
136
143
  return false unless array_new?(array_new_node)
137
144
 
@@ -28,6 +28,7 @@ module RuboCop
28
28
  # x&.foo || bar
29
29
  class SafeNavigationChain < Base
30
30
  include NilMethods
31
+ include RescueNode
31
32
  extend AutoCorrector
32
33
  extend TargetRubyVersion
33
34
 
@@ -35,6 +36,9 @@ module RuboCop
35
36
 
36
37
  MSG = 'Do not chain ordinary method call after safe navigation operator.'
37
38
  PLUS_MINUS_METHODS = %i[+@ -@].freeze
39
+ BODY_PARENT_TYPES = %i[
40
+ block numblock itblock when in_pattern while until class module sclass
41
+ ].to_set.freeze
38
42
 
39
43
  # @!method bad_method?(node)
40
44
  def_node_matcher :bad_method?, <<~PATTERN
@@ -121,18 +125,67 @@ module RuboCop
121
125
  end
122
126
 
123
127
  def require_parentheses?(send_node)
124
- return true if operator_inside_collection_literal?(send_node)
125
- return false unless send_node.comparison_method?
126
- return false unless (node = send_node.parent)
128
+ return false if send_node.loc.dot || brackets?(send_node)
127
129
 
128
- (node.respond_to?(:logical_operator?) && node.logical_operator?) ||
129
- (node.respond_to?(:comparison_method?) && node.comparison_method?)
130
+ !command_call_allowed?(send_node)
130
131
  end
131
132
 
132
- def operator_inside_collection_literal?(send_node)
133
- # If an operator call (without a dot) is inside an array or a hash, it needs
134
- # to be parenthesized when converted to safe navigation.
135
- send_node.parent&.type?(:array, :pair) && !send_node.loc.dot
133
+ def command_call_allowed?(node)
134
+ return true unless (parent = node.parent)
135
+ return sole_argument?(node, parent) if argument_taking?(parent)
136
+ return true if condition?(node, parent)
137
+
138
+ statement?(node, parent)
139
+ end
140
+
141
+ def argument_taking?(node)
142
+ return node.type?(:yield, :super, :return, :break, :next) unless node.call_type?
143
+
144
+ !node.assignment? && !node.operator_method?
145
+ end
146
+
147
+ def sole_argument?(node, parent)
148
+ return false unless parent.arguments.one?
149
+ return false unless node.equal?(parent.first_argument)
150
+ return false if parent.parenthesized? && parent.multiline?
151
+
152
+ !processed_source.buffer.source.match?(/\G\s*,/, node.source_range.end_pos)
153
+ end
154
+
155
+ def condition?(node, parent)
156
+ case parent.type
157
+ when :and, :or then parent.semantic_operator?
158
+ when :if then !parent.ternary? && node.equal?(parent.condition)
159
+ when :while, :until, :while_post, :until_post, :case, :case_match
160
+ node.equal?(parent.condition)
161
+ else false
162
+ end
163
+ end
164
+
165
+ def statement?(node, parent)
166
+ return true if parent.type?(:begin, :kwbegin, :rescue, :ensure)
167
+ return node.equal?(parent.body) if BODY_PARENT_TYPES.include?(parent.type)
168
+ return branch?(node, parent) if parent.type?(:if, :case, :case_match)
169
+
170
+ case parent.type
171
+ when :def, :defs then !parent.endless?
172
+ when :resbody then node.equal?(parent.body) && !rescue_modifier?(parent)
173
+ else assigned_value_in_statement?(node, parent)
174
+ end
175
+ end
176
+
177
+ def branch?(node, parent)
178
+ return false if parent.if_type? && parent.ternary?
179
+
180
+ !node.equal?(parent.condition)
181
+ end
182
+
183
+ def assigned_value_in_statement?(node, parent)
184
+ return false unless parent.assignment?
185
+ return false unless node.equal?(parent.children.last)
186
+ return true unless (grandparent = parent.parent)
187
+
188
+ statement?(parent, grandparent)
136
189
  end
137
190
  end
138
191
  end
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  bad_location = bad_location(node)
59
59
  add_offense(bad_location, message: message) do |corrector|
60
- corrector.replace(bad_location, "#{method} #{module_names_source}")
60
+ corrector.replace(bad_location, replacement(node, method, module_names_source))
61
61
  end
62
62
  end
63
63
  end
@@ -74,6 +74,26 @@ module RuboCop
74
74
  format(MSG, method: method, module_name: module_name, bad_method: bad_method)
75
75
  end
76
76
 
77
+ def replacement(node, method, module_names_source)
78
+ if statement?(node)
79
+ "#{method} #{module_names_source}"
80
+ else
81
+ "#{method}(#{module_names_source})"
82
+ end
83
+ end
84
+
85
+ def statement?(node)
86
+ parent = node.parent
87
+ return true if parent.nil? || parent.type?(:begin, :kwbegin)
88
+ return body?(parent, node) if parent.type?(:class, :module, :sclass, :any_def, :any_block)
89
+
90
+ parent.if_type? && !parent.ternary? && !parent.condition.equal?(node)
91
+ end
92
+
93
+ def body?(parent, node)
94
+ parent.body.equal?(node) && !(parent.any_def_type? && parent.endless?)
95
+ end
96
+
77
97
  def mixin_method?(node)
78
98
  MIXIN_METHODS.include?(node.to_sym)
79
99
  end
@@ -34,6 +34,10 @@ module RuboCop
34
34
  MSG = 'Avoid leaving a trailing comma in attribute declarations.'
35
35
  RESTRICT_ON_SEND = %i[attr_reader attr_writer attr_accessor attr].freeze
36
36
 
37
+ def self.autocorrect_incompatible_with
38
+ [Style::MethodCallWithArgsParentheses]
39
+ end
40
+
37
41
  def on_send(node)
38
42
  return unless node.attribute_accessor? && node.last_argument.any_def_type?
39
43
  # A lone `def` argument (e.g. `attr_reader def foo; end`) has no preceding
@@ -44,6 +44,10 @@ module RuboCop
44
44
 
45
45
  MSG = 'Delimiting a string with multiple quotes has no effect, use a single quote instead.'
46
46
 
47
+ def self.autocorrect_incompatible_with
48
+ [Lint::ImplicitStringConcatenation]
49
+ end
50
+
47
51
  def on_dstr(node)
48
52
  return if (empty_str_nodes = empty_str_nodes(node)).none?
49
53
 
@@ -150,6 +150,7 @@ module RuboCop
150
150
  # TODO: More precise handling (rescue, ensure, nested begin, etc.)
151
151
  def return_value_node_of_scope(scope)
152
152
  body_node = scope.body_node
153
+ return if body_node.nil?
153
154
 
154
155
  if body_node.begin_type?
155
156
  body_node.children.last
@@ -202,6 +203,31 @@ module RuboCop
202
203
 
203
204
  def remove_trailing_character_from_operator(corrector, node)
204
205
  corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
206
+
207
+ value = node.parent.expression
208
+ corrector.wrap(value, '(', ')') if operand_requires_parentheses?(value)
209
+ end
210
+
211
+ def operand_requires_parentheses?(node)
212
+ if node.any_block_type?
213
+ return true unless node.braces?
214
+
215
+ node = node.send_node
216
+ end
217
+ return call_requires_parentheses?(node) if node.type?(:call, :yield, :super)
218
+
219
+ return node.op_asgn_type? if node.assignment?
220
+
221
+ !node.basic_literal? && !node.variable? &&
222
+ !node.type?(:const, :self, :begin, :kwbegin, :zsuper, :array, :hash, :dstr, :dsym,
223
+ :xstr, :regexp)
224
+ end
225
+
226
+ def call_requires_parentheses?(node)
227
+ return true if node.prefix_not?
228
+ return false if node.call_type? && node.assignment_method?
229
+
230
+ node.arguments? && !node.parenthesized? && !node.method?(:[])
205
231
  end
206
232
 
207
233
  def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
@@ -40,7 +40,7 @@ module RuboCop
40
40
 
41
41
  # @!method block_reassigns_arg?(node)
42
42
  def_node_search :block_reassigns_arg?, <<~PATTERN
43
- (lvasgn %)
43
+ {(lvasgn % ...) (match_var %) (match_with_lvasgn #named_capture?(%) _)}
44
44
  PATTERN
45
45
 
46
46
  def on_send(node)
@@ -74,9 +74,17 @@ module RuboCop
74
74
  end
75
75
 
76
76
  def remove_node(corrector, node)
77
+ return if code_after?(node)
78
+
77
79
  corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
78
80
  end
79
81
 
82
+ def code_after?(node)
83
+ last_line = processed_source.buffer.source_line(node.last_line)
84
+
85
+ last_line[node.loc.last_column..].match?(/\A\s*[^\s#]/)
86
+ end
87
+
80
88
  def autocorrect_block_pass(corrector, node, proc_name)
81
89
  corrector.replace(node, proc_name)
82
90
  end
@@ -112,6 +120,10 @@ module RuboCop
112
120
  end
113
121
  end
114
122
 
123
+ def named_capture?(regexp, name)
124
+ regexp.to_regexp.names.include?(name.to_s)
125
+ end
126
+
115
127
  def fix_indentation(source, range)
116
128
  # Cleanup indentation in a multiline block
117
129
  source_lines = source.split("\n")
@@ -75,6 +75,7 @@ module RuboCop
75
75
  BINARY_OPERATORS = %i[* / % + - == === != < > <= >= <=>].freeze
76
76
  UNARY_OPERATORS = %i[+@ -@ ~ !].freeze
77
77
  OPERATORS = (BINARY_OPERATORS + UNARY_OPERATORS).freeze
78
+ NON_VALUE_ARGUMENT_TYPES = %i[splat block_pass forwarded_args forwarded_restarg].freeze
78
79
  NONMUTATING_METHODS_WITH_BANG_VERSION = %i[capitalize chomp chop compact
79
80
  delete_prefix delete_suffix downcase
80
81
  encode flatten gsub lstrip merge next
@@ -153,13 +154,11 @@ module RuboCop
153
154
  check_expression(case_node.else_branch) if case_node.else_branch
154
155
  end
155
156
 
156
- # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
157
+ # rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
157
158
  def check_void_op(node, &block)
158
159
  node = node.children.first while node&.begin_type?
159
160
  return unless node&.call_type? && OPERATORS.include?(node.method_name)
160
- if !UNARY_OPERATORS.include?(node.method_name) && node.loc.dot && node.arguments.none?
161
- return
162
- end
161
+ return unless operation?(node)
163
162
 
164
163
  return if block && yield(node)
165
164
 
@@ -169,6 +168,19 @@ module RuboCop
169
168
  end
170
169
  end
171
170
 
171
+ def operation?(node)
172
+ return UNARY_OPERATORS.include?(node.method_name) || !node.loc.dot if node.arguments.none?
173
+ return false if !node.arguments.one? || trailing_comma?(node)
174
+
175
+ argument = node.first_argument
176
+
177
+ argument.hash_type? ? argument.braces? : !argument.type?(*NON_VALUE_ARGUMENT_TYPES)
178
+ end
179
+
180
+ def trailing_comma?(node)
181
+ node.last_argument.source_range.end.join(node.source_range.end).source.include?(',')
182
+ end
183
+
172
184
  def check_var(node)
173
185
  return unless node.variable? || node.const_type?
174
186
 
@@ -193,6 +205,8 @@ module RuboCop
193
205
  end
194
206
 
195
207
  add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
208
+ next if node.each_descendant(:begin).any?
209
+
196
210
  autocorrect_void_expression(corrector, node)
197
211
  end
198
212
  end
@@ -282,11 +296,8 @@ module RuboCop
282
296
  end
283
297
 
284
298
  def autocorrect_nonmutating_send(corrector, node, suggestion)
285
- send_node = if node.call_type?
286
- node
287
- else
288
- node.send_node
289
- end
299
+ send_node = node.call_type? ? node : node.send_node
300
+
290
301
  corrector.replace(send_node.loc.selector, suggestion)
291
302
  end
292
303
 
@@ -75,14 +75,12 @@ module RuboCop
75
75
  url = cop_config['StyleGuide']
76
76
  return nil if url.nil? || url.empty?
77
77
 
78
- self.class.style_guide_urls[url] ||= begin
79
- base_url = style_guide_base_url
80
- if base_url.nil? || base_url.empty?
81
- url
82
- else
83
- URI.join(base_url, url).to_s
84
- end
85
- end
78
+ base_url = style_guide_base_url
79
+ return url if base_url.nil? || base_url.empty?
80
+
81
+ # Departments can have style guides of their own, so the same guideline
82
+ # can resolve against different bases.
83
+ self.class.style_guide_urls[[base_url, url]] ||= URI.join(base_url, url).to_s
86
84
  end
87
85
 
88
86
  # Returns the base style guide URL from AllCops or the specific department
@@ -66,7 +66,7 @@ module RuboCop
66
66
  # the line at which the parent node ends.
67
67
  # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
68
68
  def find_end_line(node)
69
- if node.if_type?
69
+ if node.if_type? && !node.modifier_form?
70
70
  if node.else?
71
71
  node.loc.else.line
72
72
  elsif node.ternary?
@@ -87,7 +87,7 @@ module RuboCop
87
87
  else
88
88
  parent.loc.line
89
89
  end
90
- end || node.loc.end.line
90
+ end || node.source_range.last_line
91
91
  end
92
92
  end
93
93
  end
@@ -21,9 +21,16 @@ module RuboCop
21
21
  # @!method constant_definition?(node)
22
22
  def_node_matcher :constant_definition?, '{class module}'
23
23
 
24
+ # @!method access_modifier_send?(node)
25
+ def_node_matcher :access_modifier_send?, '(send nil? {:private :protected :public})'
26
+
24
27
  # @!method empty_line_required?(node)
25
- def_node_matcher :empty_line_required?,
26
- '{any_def class module (send nil? {:private :protected :public})}'
28
+ def_node_matcher :empty_line_required?, <<~PATTERN
29
+ {
30
+ any_def class module #access_modifier_send?
31
+ [{(if _ #access_modifier_send? nil?) (if _ nil? #access_modifier_send?)} modifier_form?]
32
+ }
33
+ PATTERN
27
34
 
28
35
  def check(node, body, adjusted_first_line: nil)
29
36
  return if valid_body_style?(body)
@@ -39,15 +46,15 @@ module RuboCop
39
46
  when :empty_lines_special
40
47
  check_empty_lines_special(body, first_line, last_line)
41
48
  else
42
- check_both(style, first_line, last_line)
49
+ check_both(style, body, first_line, last_line)
43
50
  end
44
51
  end
45
52
 
46
53
  def check_empty_lines_except_namespace(body, first_line, last_line)
47
54
  if namespace?(body, with_one_child: true)
48
- check_both(:no_empty_lines, first_line, last_line)
55
+ check_both(:no_empty_lines, body, first_line, last_line)
49
56
  else
50
- check_both(:empty_lines, first_line, last_line)
57
+ check_both(:empty_lines, body, first_line, last_line)
51
58
  end
52
59
  end
53
60
 
@@ -55,37 +62,41 @@ module RuboCop
55
62
  return unless body
56
63
 
57
64
  if namespace?(body, with_one_child: true)
58
- check_both(:no_empty_lines, first_line, last_line)
65
+ check_both(:no_empty_lines, body, first_line, last_line)
59
66
  else
60
67
  if first_child_requires_empty_line?(body)
61
- check_beginning(:empty_lines, first_line)
68
+ check_beginning(:empty_lines, body, first_line)
62
69
  else
63
- check_beginning(:no_empty_lines, first_line)
70
+ check_beginning(:no_empty_lines, body, first_line)
64
71
  check_deferred_empty_line(body)
65
72
  end
66
- check_ending(:empty_lines, last_line)
73
+ check_ending(:empty_lines, body, last_line)
67
74
  end
68
75
  end
69
76
 
70
- def check_both(style, first_line, last_line)
77
+ def check_both(style, body, first_line, last_line)
71
78
  case style
72
79
  when :beginning_only
73
- check_beginning(:empty_lines, first_line)
74
- check_ending(:no_empty_lines, last_line)
80
+ check_beginning(:empty_lines, body, first_line)
81
+ check_ending(:no_empty_lines, body, last_line)
75
82
  when :ending_only
76
- check_beginning(:no_empty_lines, first_line)
77
- check_ending(:empty_lines, last_line)
83
+ check_beginning(:no_empty_lines, body, first_line)
84
+ check_ending(:empty_lines, body, last_line)
78
85
  else
79
- check_beginning(style, first_line)
80
- check_ending(style, last_line)
86
+ check_beginning(style, body, first_line)
87
+ check_ending(style, body, last_line)
81
88
  end
82
89
  end
83
90
 
84
- def check_beginning(style, first_line)
91
+ def check_beginning(style, body, first_line)
92
+ return if style == :empty_lines && body&.first_line == first_line
93
+
85
94
  check_source(style, first_line, 'beginning')
86
95
  end
87
96
 
88
- def check_ending(style, last_line)
97
+ def check_ending(style, body, last_line)
98
+ return if style == :empty_lines && body&.last_line == last_line
99
+
89
100
  check_source(style, last_line - 2, 'end')
90
101
  end
91
102
 
@@ -119,6 +130,7 @@ module RuboCop
119
130
  def check_deferred_empty_line(body)
120
131
  node = first_empty_line_required_child(body)
121
132
  return unless node
133
+ return if same_line_as_previous_statement?(node)
122
134
 
123
135
  line = previous_line_ignoring_comments(node.first_line)
124
136
  return if processed_source[line].empty?
@@ -156,6 +168,10 @@ module RuboCop
156
168
  end
157
169
  end
158
170
 
171
+ def same_line_as_previous_statement?(node)
172
+ node.left_sibling.last_line == node.first_line
173
+ end
174
+
159
175
  def previous_line_ignoring_comments(send_line)
160
176
  (send_line - 2).downto(0) do |line|
161
177
  return line unless comment_line?(processed_source[line])
@@ -5,6 +5,10 @@ module RuboCop
5
5
  # Common functionality for checking whether a method body can become the
6
6
  # body of an endless method definition without changing its meaning.
7
7
  module EndlessMethodBodyHelp
8
+ STATEMENT_BODY_PARENT_TYPES = %i[
9
+ resbody when in_pattern while until for class module sclass any_block
10
+ ].freeze
11
+
8
12
  private
9
13
 
10
14
  def endless_method_body?(body)
@@ -45,7 +49,7 @@ module RuboCop
45
49
 
46
50
  def command_disallowed_in_endless_body?(body)
47
51
  return true if body.assignment? && command_call?(assigned_value(body))
48
- return true if body_disallowed_in_argument?(body)
52
+ return true if body_disallowed_outside_statement?(body)
49
53
  return true if negated_command_call?(body)
50
54
 
51
55
  body.each_node(:any_block).any? do |block|
@@ -53,20 +57,53 @@ module RuboCop
53
57
  end
54
58
  end
55
59
 
56
- def body_disallowed_in_argument?(body)
57
- command_body_in_argument?(body) || do_end_block_body_in_command_argument?(body)
60
+ def body_disallowed_outside_statement?(body)
61
+ return true if endless_method_with_command_body?(body)
62
+
63
+ command_body_outside_statement?(body) || do_end_block_body_in_command_argument?(body)
64
+ end
65
+
66
+ def endless_method_with_command_body?(node)
67
+ node.any_def_type? && node.endless? && command_call?(node.body)
58
68
  end
59
69
 
60
- # `private def foo = bar baz` parses only on Ruby 4.0+, and only as the sole argument.
61
- def command_body_in_argument?(body)
70
+ def command_body_outside_statement?(body)
62
71
  return false unless command_call?(body)
72
+ return false if command_position?(body.parent)
63
73
 
64
74
  call = argument_receiving_call(body.parent)
65
- return false if call.nil?
75
+ return true if call.nil?
66
76
 
67
77
  target_ruby_version < 4.0 || !call.arguments.one?
68
78
  end
69
79
 
80
+ def command_position?(def_node)
81
+ parent = def_node.parent
82
+ return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
83
+ return false if parent.masgn_type?
84
+ return true if parent.assignment?
85
+ return parent.last_argument.equal?(def_node) if setter_call?(parent)
86
+
87
+ statement_body?(def_node, parent)
88
+ end
89
+
90
+ def setter_call?(node)
91
+ node.call_type? && node.assignment_method?
92
+ end
93
+
94
+ def statement_body?(def_node, parent)
95
+ return def_node.equal?(parent.body) if parent.type?(*STATEMENT_BODY_PARENT_TYPES)
96
+
97
+ case parent.type
98
+ when :if
99
+ !parent.ternary? && !def_node.equal?(parent.condition)
100
+ when :case, :case_match
101
+ def_node.equal?(parent.else_branch)
102
+ else
103
+ false
104
+ end
105
+ end
106
+
70
107
  def argument_receiving_call(def_node)
71
108
  call = def_node.parent
72
109
  return unless call&.call_type?
@@ -90,8 +127,10 @@ module RuboCop
90
127
  node
91
128
  end
92
129
 
130
+ # `return foo` is a command too, even as `return(foo)`.
93
131
  def command_call?(node)
94
132
  node = node.send_node if node.any_block_type?
133
+ return node.arguments? if node.return_type?
95
134
  return false unless node.type?(:call, :super, :yield)
96
135
 
97
136
  node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
@@ -17,7 +17,7 @@ module RuboCop
17
17
  private
18
18
 
19
19
  def receiver(node)
20
- node.receiver ? "#{node.receiver.source}#{node.loc.operator.source}" : ''
20
+ node.receiver ? node.loc.keyword.end.join(node.loc.name.begin).source.lstrip : ''
21
21
  end
22
22
 
23
23
  def arguments(node, missing = '')