rubocop 1.20.0 → 1.22.2

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 (204) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +64 -15
  4. data/lib/rubocop/config.rb +5 -0
  5. data/lib/rubocop/config_loader.rb +3 -1
  6. data/lib/rubocop/config_validator.rb +9 -1
  7. data/lib/rubocop/cop/base.rb +3 -3
  8. data/lib/rubocop/cop/bundler/gem_comment.rb +3 -3
  9. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +45 -21
  10. data/lib/rubocop/cop/bundler/ordered_gems.rb +3 -12
  11. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
  12. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  13. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +11 -10
  14. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +3 -12
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +31 -24
  16. data/lib/rubocop/cop/generator.rb +14 -8
  17. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +60 -0
  18. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  19. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  20. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  21. data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
  22. data/lib/rubocop/cop/layout/class_structure.rb +2 -1
  23. data/lib/rubocop/cop/layout/dot_position.rb +30 -5
  24. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  25. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +7 -4
  26. data/lib/rubocop/cop/layout/end_alignment.rb +2 -3
  27. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
  28. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  30. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  31. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  32. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  33. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  34. data/lib/rubocop/cop/layout/line_length.rb +9 -7
  35. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  36. data/lib/rubocop/cop/layout/multiline_block_layout.rb +3 -3
  37. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  38. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -0
  39. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
  41. data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
  42. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
  43. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  44. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
  45. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  46. data/lib/rubocop/cop/layout/space_inside_parens.rb +74 -28
  47. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
  48. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +111 -0
  49. data/lib/rubocop/cop/lint/ambiguous_range.rb +9 -9
  50. data/lib/rubocop/cop/lint/assignment_in_condition.rb +7 -5
  51. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +18 -5
  52. data/lib/rubocop/cop/lint/boolean_symbol.rb +5 -0
  53. data/lib/rubocop/cop/lint/debugger.rb +0 -2
  54. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  55. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +24 -1
  56. data/lib/rubocop/cop/lint/else_layout.rb +10 -6
  57. data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
  58. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  59. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  60. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +12 -3
  61. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
  62. data/lib/rubocop/cop/lint/interpolation_check.rb +5 -0
  63. data/lib/rubocop/cop/lint/loop.rb +4 -3
  64. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +5 -1
  65. data/lib/rubocop/cop/lint/number_conversion.rb +12 -1
  66. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  67. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +4 -2
  68. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +17 -0
  69. data/lib/rubocop/cop/lint/percent_string_array.rb +10 -0
  70. data/lib/rubocop/cop/lint/raise_exception.rb +4 -0
  71. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +5 -4
  72. data/lib/rubocop/cop/lint/require_relative_self_path.rb +50 -0
  73. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  74. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  75. data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
  76. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +8 -3
  77. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
  78. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -2
  79. data/lib/rubocop/cop/lint/useless_setter_call.rb +7 -4
  80. data/lib/rubocop/cop/lint/useless_times.rb +4 -3
  81. data/lib/rubocop/cop/metrics/abc_size.rb +6 -0
  82. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  83. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  84. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  85. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  86. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  87. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -2
  88. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +15 -6
  89. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  90. data/lib/rubocop/cop/mixin/heredoc.rb +1 -3
  91. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
  92. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  93. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
  94. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +9 -1
  95. data/lib/rubocop/cop/mixin/percent_array.rb +6 -1
  96. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
  97. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  98. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  99. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  100. data/lib/rubocop/cop/mixin/string_literals_help.rb +5 -1
  101. data/lib/rubocop/cop/mixin/trailing_body.rb +1 -1
  102. data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
  103. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  104. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  105. data/lib/rubocop/cop/naming/inclusive_language.rb +9 -9
  106. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +5 -4
  107. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +7 -0
  108. data/lib/rubocop/cop/security/io_methods.rb +49 -0
  109. data/lib/rubocop/cop/security/json_load.rb +8 -7
  110. data/lib/rubocop/cop/security/open.rb +4 -0
  111. data/lib/rubocop/cop/security/yaml_load.rb +4 -0
  112. data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
  113. data/lib/rubocop/cop/style/and_or.rb +5 -0
  114. data/lib/rubocop/cop/style/arguments_forwarding.rb +13 -2
  115. data/lib/rubocop/cop/style/array_coercion.rb +21 -3
  116. data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
  117. data/lib/rubocop/cop/style/case_equality.rb +6 -9
  118. data/lib/rubocop/cop/style/case_like_if.rb +5 -0
  119. data/lib/rubocop/cop/style/class_and_module_children.rb +9 -0
  120. data/lib/rubocop/cop/style/collection_compact.rb +7 -5
  121. data/lib/rubocop/cop/style/collection_methods.rb +8 -6
  122. data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
  123. data/lib/rubocop/cop/style/commented_keyword.rb +9 -4
  124. data/lib/rubocop/cop/style/date_time.rb +5 -0
  125. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  126. data/lib/rubocop/cop/style/documentation.rb +23 -8
  127. data/lib/rubocop/cop/style/double_negation.rb +15 -5
  128. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  129. data/lib/rubocop/cop/style/explicit_block_argument.rb +21 -11
  130. data/lib/rubocop/cop/style/float_division.rb +10 -2
  131. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +6 -1
  132. data/lib/rubocop/cop/style/global_std_stream.rb +4 -0
  133. data/lib/rubocop/cop/style/hash_each_methods.rb +5 -0
  134. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -6
  135. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -6
  136. data/lib/rubocop/cop/style/identical_conditional_branches.rb +18 -16
  137. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +18 -4
  138. data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
  139. data/lib/rubocop/cop/style/inverse_methods.rb +9 -2
  140. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  141. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -1
  142. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
  143. data/lib/rubocop/cop/style/module_function.rb +8 -9
  144. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
  145. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
  146. data/lib/rubocop/cop/style/mutable_constant.rb +13 -8
  147. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  148. data/lib/rubocop/cop/style/negated_unless.rb +1 -1
  149. data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
  150. data/lib/rubocop/cop/style/not.rb +2 -2
  151. data/lib/rubocop/cop/style/numbered_parameters.rb +46 -0
  152. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +50 -0
  153. data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
  154. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
  155. data/lib/rubocop/cop/style/optional_arguments.rb +4 -0
  156. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +14 -4
  157. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  158. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  159. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -4
  160. data/lib/rubocop/cop/style/quoted_symbols.rb +10 -6
  161. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  162. data/lib/rubocop/cop/style/redundant_argument.rb +19 -9
  163. data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
  164. data/lib/rubocop/cop/style/redundant_fetch_block.rb +4 -0
  165. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +12 -3
  166. data/lib/rubocop/cop/style/redundant_freeze.rb +0 -1
  167. data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
  168. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
  169. data/lib/rubocop/cop/style/redundant_self.rb +10 -0
  170. data/lib/rubocop/cop/style/redundant_self_assignment.rb +4 -3
  171. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +1 -1
  172. data/lib/rubocop/cop/style/redundant_sort.rb +51 -18
  173. data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
  174. data/lib/rubocop/cop/style/return_nil.rb +2 -1
  175. data/lib/rubocop/cop/style/safe_navigation.rb +13 -2
  176. data/lib/rubocop/cop/style/select_by_regexp.rb +138 -0
  177. data/lib/rubocop/cop/style/single_argument_dig.rb +5 -0
  178. data/lib/rubocop/cop/style/slicing_with_range.rb +13 -0
  179. data/lib/rubocop/cop/style/special_global_vars.rb +4 -0
  180. data/lib/rubocop/cop/style/static_class.rb +5 -5
  181. data/lib/rubocop/cop/style/string_chars.rb +4 -2
  182. data/lib/rubocop/cop/style/string_concatenation.rb +5 -1
  183. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -0
  184. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -2
  185. data/lib/rubocop/cop/style/swap_values.rb +4 -2
  186. data/lib/rubocop/cop/style/symbol_proc.rb +26 -0
  187. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +19 -0
  188. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  189. data/lib/rubocop/cop/style/yoda_condition.rb +24 -7
  190. data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -0
  191. data/lib/rubocop/cop/util.rb +15 -4
  192. data/lib/rubocop/cops_documentation_generator.rb +17 -5
  193. data/lib/rubocop/options.rb +126 -112
  194. data/lib/rubocop/rake_task.rb +1 -1
  195. data/lib/rubocop/result_cache.rb +3 -3
  196. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  197. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  198. data/lib/rubocop/rspec/parallel_formatter.rb +90 -0
  199. data/lib/rubocop/rspec/support.rb +1 -0
  200. data/lib/rubocop/runner.rb +2 -3
  201. data/lib/rubocop/target_finder.rb +1 -1
  202. data/lib/rubocop/version.rb +1 -1
  203. data/lib/rubocop.rb +8 -1
  204. metadata +14 -5
@@ -14,7 +14,11 @@ module RuboCop
14
14
  # `Dir.glob` and `Dir[]` sort globbed results by default in Ruby 3.0.
15
15
  # So all bad cases are acceptable when Ruby 3.0 or higher are used.
16
16
  #
17
- # This cop will be deprecated and removed when supporting only Ruby 3.0 and higher.
17
+ # NOTE: This cop will be deprecated and removed when supporting only Ruby 3.0 and higher.
18
+ #
19
+ # @safety
20
+ # This cop is unsafe in the case where sorting files changes existing
21
+ # expected behaviour.
18
22
  #
19
23
  # @example
20
24
  #
@@ -18,6 +18,11 @@ module RuboCop
18
18
  # cop by default). Similarly, Rails' duration methods do not work well
19
19
  # with `Integer()` and can be ignored with `IgnoredMethods`.
20
20
  #
21
+ # @safety
22
+ # Autocorrection is unsafe because it is not guaranteed that the
23
+ # replacement `Kernel` methods are able to properly handle the
24
+ # input if it is not a standard class.
25
+ #
21
26
  # @example
22
27
  #
23
28
  # # bad
@@ -60,6 +65,7 @@ module RuboCop
60
65
  'class parsing, instead of using '\
61
66
  '`%<current>s`, use stricter '\
62
67
  '`%<corrected_method>s`.'
68
+ CONVERSION_METHODS = %i[Integer Float Complex to_i to_f to_c].freeze
63
69
  METHODS = CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
64
70
 
65
71
  # @!method to_method(node)
@@ -127,7 +133,8 @@ module RuboCop
127
133
  end
128
134
 
129
135
  def ignore_receiver?(receiver)
130
- if receiver.send_type? && ignored_method?(receiver.method_name)
136
+ if receiver.numeric_type? || (receiver.send_type? &&
137
+ (conversion_method?(receiver.method_name) || ignored_method?(receiver.method_name)))
131
138
  true
132
139
  elsif (receiver = top_receiver(receiver))
133
140
  receiver.const_type? && ignored_class?(receiver.const_name)
@@ -142,6 +149,10 @@ module RuboCop
142
149
  receiver
143
150
  end
144
151
 
152
+ def conversion_method?(method_name)
153
+ CONVERSION_METHODS.include?(method_name)
154
+ end
155
+
145
156
  def ignored_classes
146
157
  cop_config.fetch('IgnoredClasses', [])
147
158
  end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
11
11
  # -e:1: warning: `_1' is reserved for numbered parameter; consider another name
12
12
  #
13
- # Assiging to numbered parameter (from `_1` to `_9`) cause an error in Ruby 3.0.
13
+ # Assigning to a numbered parameter (from `_1` to `_9`) causes an error in Ruby 3.0.
14
14
  #
15
15
  # % ruby -ve '_1 = :value'
16
16
  # ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
@@ -9,8 +9,10 @@ module RuboCop
9
9
  # should always be the same. If constants are assigned in multiple
10
10
  # locations, the result may vary depending on the order of `require`.
11
11
  #
12
- # Also, if you already have such an implementation, auto-correction may
13
- # change the result.
12
+ # @safety
13
+ # This cop is unsafe because code that is already conditionally
14
+ # assigning a constant may have its behaviour changed by
15
+ # auto-correction.
14
16
  #
15
17
  # @example
16
18
  #
@@ -6,6 +6,23 @@ module RuboCop
6
6
  # This cops looks for references of Regexp captures that are out of range
7
7
  # and thus always returns nil.
8
8
  #
9
+ # @safety
10
+ # This cop is unsafe because it is naive in how it determines what
11
+ # references are available based on the last encountered regexp, but
12
+ # it cannot handle some cases, such as conditional regexp matches, which
13
+ # leads to false positives, such as:
14
+ #
15
+ # [source,ruby]
16
+ # ----
17
+ # foo ? /(c)(b)/ =~ str : /(b)/ =~ str
18
+ # do_something if $2
19
+ # # $2 is defined for the first condition but not the second, however
20
+ # # the cop will mark this as an offense.
21
+ # ----
22
+ #
23
+ # This might be a good indication of code that should be refactored,
24
+ # however.
25
+ #
9
26
  # @example
10
27
  #
11
28
  # /(foo)bar/ =~ 'foobar'
@@ -9,6 +9,16 @@ module RuboCop
9
9
  # example, mistranslating an array of literals to percent string notation)
10
10
  # rather than meant to be part of the resulting strings.
11
11
  #
12
+ # @safety
13
+ # The cop is unsafe because the correction changes the values in the array
14
+ # and that might have been done purposely.
15
+ #
16
+ # [source,ruby]
17
+ # ----
18
+ # %w('foo', "bar") #=> ["'foo',", '"bar"']
19
+ # %w(foo bar) #=> ['foo', 'bar']
20
+ # ----
21
+ #
12
22
  # @example
13
23
  #
14
24
  # # bad
@@ -13,6 +13,10 @@ module RuboCop
13
13
  # `Exception`. Alternatively, make `Exception` a fully qualified class
14
14
  # name with an explicit namespace.
15
15
  #
16
+ # @safety
17
+ # This cop is unsafe because it will change the exception class being
18
+ # raised, which is a change in behaviour.
19
+ #
16
20
  # @example
17
21
  # # bad
18
22
  # raise Exception, 'Error message here'
@@ -7,13 +7,14 @@ module RuboCop
7
7
  # `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
8
8
  # are checked by default. These are customizable with `AllowedMethods` option.
9
9
  #
10
- # This cop is marked as unsafe, because auto-correction can change the
11
- # return type of the expression. An offending expression that previously
12
- # could return `nil` will be auto-corrected to never return `nil`.
13
- #
14
10
  # In the example below, the safe navigation operator (`&.`) is unnecessary
15
11
  # because `NilClass` has methods like `respond_to?` and `is_a?`.
16
12
  #
13
+ # @safety
14
+ # This cop is unsafe, because auto-correction can change the return type of
15
+ # the expression. An offending expression that previously could return `nil`
16
+ # will be auto-corrected to never return `nil`.
17
+ #
17
18
  # @example
18
19
  # # bad
19
20
  # do_something if attrs&.respond_to?(:[])
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for uses a file requiring itself with `require_relative`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ #
12
+ # # foo.rb
13
+ # require_relative 'foo'
14
+ # require_relative 'bar'
15
+ #
16
+ # # good
17
+ #
18
+ # # foo.rb
19
+ # require_relative 'bar'
20
+ #
21
+ class RequireRelativeSelfPath < Base
22
+ include RangeHelp
23
+ extend AutoCorrector
24
+
25
+ MSG = 'Remove the `require_relative` that requires itself.'
26
+ RESTRICT_ON_SEND = %i[require_relative].freeze
27
+
28
+ def on_send(node)
29
+ return unless (required_feature = node.first_argument)
30
+ return unless required_feature.respond_to?(:value)
31
+ return unless same_file?(processed_source.file_path, required_feature.value)
32
+
33
+ add_offense(node) do |corrector|
34
+ corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def same_file?(file_path, required_feature)
41
+ file_path == required_feature || remove_ext(file_path) == required_feature
42
+ end
43
+
44
+ def remove_ext(file_path)
45
+ File.basename(file_path, File.extname(file_path))
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  #
11
11
  # NOTE: Shadowing of variables in block passed to `Ractor.new` is allowed
12
12
  # because `Ractor` should not access outer variables.
13
- # eg. following syle is encouraged:
13
+ # eg. following style is encouraged:
14
14
  #
15
15
  # worker_id, pipe = env
16
16
  # Ractor.new(worker_id, pipe) do |worker_id, pipe|
@@ -143,7 +143,7 @@ module RuboCop
143
143
  # Although some operators can be converted to symbols normally
144
144
  # (ie. `:==`), these are not accepted as hash keys and will
145
145
  # raise a syntax error (eg. `{ ==: ... }`). Therefore, if the
146
- # symbol does not start with an alpha-numeric or underscore, it
146
+ # symbol does not start with an alphanumeric or underscore, it
147
147
  # will be ignored.
148
148
  return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
149
149
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This cop checks for "triple quotes" (strings delimted by any odd number
6
+ # This cop checks for "triple quotes" (strings delimited by any odd number
7
7
  # of quotes greater than 1).
8
8
  #
9
9
  # Ruby allows multiple strings to be implicitly concatenated by just
@@ -13,14 +13,19 @@ module RuboCop
13
13
  # Keyword arguments (including `**kwargs`) do not get counted towards
14
14
  # this, as they are not used by the methods in question.
15
15
  #
16
- # NOTE: This cop matches for method names only and hence cannot tell apart
17
- # methods with same name in different classes.
18
- #
19
16
  # Method names and their expected arity can be configured like this:
20
17
  #
18
+ # [source,yaml]
19
+ # ----
21
20
  # Methods:
22
21
  # inject: 2
23
22
  # reduce: 2
23
+ # ----
24
+ #
25
+ # @safety
26
+ # This cop matches for method names only and hence cannot tell apart
27
+ # methods with same name in different classes, which may lead to a
28
+ # false positive.
24
29
  #
25
30
  # @example
26
31
  # # bad
@@ -87,9 +87,8 @@ module RuboCop
87
87
  end
88
88
 
89
89
  def ignored_method?(body)
90
- cop_config['IgnoreEmptyMethods'] && body.nil? ||
91
- cop_config['IgnoreNotImplementedMethods'] &&
92
- not_implemented?(body)
90
+ (cop_config['IgnoreEmptyMethods'] && body.nil?) ||
91
+ (cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
93
92
  end
94
93
 
95
94
  def message(variable)
@@ -6,8 +6,9 @@ module RuboCop
6
6
  # This cop checks for useless method definitions, specifically: empty constructors
7
7
  # and methods just delegating to `super`.
8
8
  #
9
- # This cop is marked as unsafe as it can trigger false positives for cases when
10
- # an empty constructor just overrides the parent constructor, which is bad anyway.
9
+ # @safety
10
+ # This cop is unsafe as it can register false positives for cases when an empty
11
+ # constructor just overrides the parent constructor, which is bad anyway.
11
12
  #
12
13
  # @example
13
14
  # # bad
@@ -5,11 +5,14 @@ module RuboCop
5
5
  module Lint
6
6
  # This cop checks for setter call to local variable as the final
7
7
  # expression of a function definition.
8
- # Its auto-correction is marked as unsafe because return value will be changed.
9
8
  #
10
- # NOTE: There are edge cases in which the local variable references a
11
- # value that is also accessible outside the local scope. This is not
12
- # detected by the cop, and it can yield a false positive.
9
+ # @safety
10
+ # There are edge cases in which the local variable references a
11
+ # value that is also accessible outside the local scope. This is not
12
+ # detected by the cop, and it can yield a false positive.
13
+ #
14
+ # As well, auto-correction is unsafe because the method's
15
+ # return value will be changed.
13
16
  #
14
17
  # @example
15
18
  #
@@ -7,8 +7,9 @@ module RuboCop
7
7
  # (when the integer <= 0) or that will only ever yield once
8
8
  # (`1.times`).
9
9
  #
10
- # This cop is marked as unsafe as `times` returns its receiver, which
11
- # is *usually* OK, but might change behavior.
10
+ # @safety
11
+ # This cop is unsafe as `times` returns its receiver, which is
12
+ # *usually* OK, but might change behavior.
12
13
  #
13
14
  # @example
14
15
  # # bad
@@ -65,7 +66,7 @@ module RuboCop
65
66
  private
66
67
 
67
68
  def never_process?(count, node)
68
- count < 1 || node.block_type? && node.body.nil?
69
+ count < 1 || (node.block_type? && node.body.nil?)
69
70
  end
70
71
 
71
72
  def remove_node(corrector, node)
@@ -8,6 +8,12 @@ module RuboCop
8
8
  # (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
9
9
  # and https://en.wikipedia.org/wiki/ABC_Software_Metric.
10
10
  #
11
+ # Interpreting ABC size:
12
+ #
13
+ # * <= 17 satisfactory
14
+ # * 18..30 unsatisfactory
15
+ # * > 30 dangerous
16
+ #
11
17
  # You can have repeated "attributes" calls count as a single "branch".
12
18
  # For this purpose, attributes are any method with no argument; no attempt
13
19
  # is meant to distinguish actual `attr_reader` from other methods.
@@ -9,6 +9,9 @@ module RuboCop
9
9
  # Keyword arguments can optionally be excluded from the total count,
10
10
  # as they add less complexity than positional or optional parameters.
11
11
  #
12
+ # NOTE: Explicit block argument `&block` is not counted to prevent
13
+ # erroneous change that is avoided by making block argument implicit.
14
+ #
12
15
  # @example Max: 3
13
16
  # # good
14
17
  # def foo(a, b, c = 1)
@@ -94,9 +97,9 @@ module RuboCop
94
97
 
95
98
  def args_count(node)
96
99
  if count_keyword_args?
97
- node.children.size
100
+ node.children.count { |a| !a.blockarg_type? }
98
101
  else
99
- node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
102
+ node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) && !a.blockarg_type? }
100
103
  end
101
104
  end
102
105
 
@@ -45,7 +45,7 @@ module RuboCop
45
45
  else
46
46
  # Otherwise, the case node gets 0.8 complexity points and each
47
47
  # when gets 0.2.
48
- (0.8 + 0.2 * nb_branches).round
48
+ (0.8 + (0.2 * nb_branches)).round
49
49
  end
50
50
  when :if
51
51
  node.else? && !node.elsif? ? 2 : 1
@@ -46,7 +46,7 @@ module RuboCop
46
46
  visit_depth_last(@node) { |child| calculate_node(child) }
47
47
 
48
48
  [
49
- Math.sqrt(@assignment**2 + @branch**2 + @condition**2).round(2),
49
+ Math.sqrt((@assignment**2) + (@branch**2) + (@condition**2)).round(2),
50
50
  "<#{@assignment}, #{@branch}, #{@condition}>"
51
51
  ]
52
52
  end
@@ -147,7 +147,7 @@ module RuboCop
147
147
 
148
148
  # Returns true for lines that shall not be included in the count.
149
149
  def irrelevant_line?(source_line)
150
- source_line.blank? || !count_comments? && comment_line?(source_line)
150
+ source_line.blank? || (!count_comments? && comment_line?(source_line))
151
151
  end
152
152
 
153
153
  def count_comments?
@@ -43,7 +43,7 @@ module RuboCop
43
43
 
44
44
  # Returns true for lines that shall not be included in the count.
45
45
  def irrelevant_line(source_line)
46
- source_line.blank? || !count_comments? && comment_line?(source_line)
46
+ source_line.blank? || (!count_comments? && comment_line?(source_line))
47
47
  end
48
48
 
49
49
  def build_code_length_calculator(node)
@@ -34,8 +34,7 @@ module RuboCop
34
34
 
35
35
  def matching_ranges(end_loc, align_ranges)
36
36
  align_ranges.select do |_, range|
37
- range.line == end_loc.line ||
38
- column_offset_between(range, end_loc).zero?
37
+ same_line?(range, end_loc) || column_offset_between(range, end_loc).zero?
39
38
  end
40
39
  end
41
40
 
@@ -9,9 +9,8 @@ module RuboCop
9
9
  FROZEN_STRING_LITERAL = '# frozen_string_literal:'
10
10
  FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
11
11
  FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
12
- FROZEN_STRING_LITERAL_TYPES_RUBY30 = %i[str].freeze
13
12
 
14
- private_constant :FROZEN_STRING_LITERAL_TYPES_RUBY27, :FROZEN_STRING_LITERAL_TYPES_RUBY30
13
+ private_constant :FROZEN_STRING_LITERAL_TYPES_RUBY27
15
14
 
16
15
  def frozen_string_literal_comment_exists?
17
16
  leading_comment_lines.any? { |line| MagicComment.parse(line).valid_literal_value? }
@@ -20,13 +19,23 @@ module RuboCop
20
19
  private
21
20
 
22
21
  def frozen_string_literal?(node)
23
- literal_types = if target_ruby_version >= 3.0
24
- FROZEN_STRING_LITERAL_TYPES_RUBY30
22
+ frozen_string = if target_ruby_version >= 3.0
23
+ uninterpolated_string?(node) || frozen_heredoc?(node)
25
24
  else
26
- FROZEN_STRING_LITERAL_TYPES_RUBY27
25
+ FROZEN_STRING_LITERAL_TYPES_RUBY27.include?(node.type)
27
26
  end
28
27
 
29
- literal_types.include?(node.type) && frozen_string_literals_enabled?
28
+ frozen_string && frozen_string_literals_enabled?
29
+ end
30
+
31
+ def uninterpolated_string?(node)
32
+ node.str_type? || (node.dstr_type? && node.each_descendant(:begin).none?)
33
+ end
34
+
35
+ def frozen_heredoc?(node)
36
+ return false unless node.dstr_type? && node.heredoc?
37
+
38
+ node.children.all?(&:str_type?)
30
39
  end
31
40
 
32
41
  def frozen_string_literals_enabled?
@@ -139,9 +139,9 @@ module RuboCop
139
139
  end
140
140
 
141
141
  def self.from_map_to_h(node, match)
142
- strip_trailing_chars = 0
143
-
144
- unless node.parent&.block_type?
142
+ if node.parent&.block_type? && node.parent.send_node == node
143
+ strip_trailing_chars = 0
144
+ else
145
145
  map_range = node.children.first.source_range
146
146
  node_range = node.source_range
147
147
  strip_trailing_chars = node_range.end_pos - map_range.end_pos
@@ -21,9 +21,7 @@ module RuboCop
21
21
  private
22
22
 
23
23
  def indent_level(str)
24
- indentations = str.lines
25
- .map { |line| line[/^\s*/] }
26
- .reject { |line| line.end_with?("\n") }
24
+ indentations = str.lines.map { |line| line[/^\s*/] }.reject { |line| line.end_with?("\n") }
27
25
  indentations.empty? ? 0 : indentations.min_by(&:size).size
28
26
  end
29
27
 
@@ -15,7 +15,7 @@ module RuboCop
15
15
  node.arguments.each do |arg|
16
16
  on_node(type, arg, :send) do |type_node|
17
17
  left_brace = type_node.loc.begin
18
- if left_brace && left_brace.line == left_parenthesis.line
18
+ if left_brace && same_line?(left_brace, left_parenthesis)
19
19
  yield type_node, left_parenthesis
20
20
  ignore_node(type_node)
21
21
  end
@@ -134,7 +134,7 @@ module RuboCop
134
134
 
135
135
  next if a.setter_method?
136
136
  next unless kind == :with_or_without_parentheses ||
137
- kind == :with_parentheses && parentheses?(a)
137
+ (kind == :with_parentheses && parentheses?(a))
138
138
 
139
139
  a.arguments.any? { |arg| within_node?(node, arg) }
140
140
  end
@@ -156,7 +156,7 @@ module RuboCop
156
156
 
157
157
  def disqualified_rhs?(candidate, ancestor)
158
158
  UNALIGNED_RHS_TYPES.include?(ancestor.type) ||
159
- ancestor.block_type? && part_of_block_body?(candidate, ancestor)
159
+ (ancestor.block_type? && part_of_block_body?(candidate, ancestor))
160
160
  end
161
161
 
162
162
  def valid_rhs?(candidate, ancestor)
@@ -90,7 +90,7 @@ module RuboCop
90
90
  # This method depends on the fact that we have guarded
91
91
  # against implicit and empty literals.
92
92
  def opening_brace_on_same_line?(node)
93
- node.loc.begin.line == children(node).first.first_line
93
+ same_line?(node.loc.begin, children(node).first)
94
94
  end
95
95
 
96
96
  # This method depends on the fact that we have guarded
@@ -35,7 +35,15 @@ module RuboCop
35
35
  previous: gem_name(current),
36
36
  current: gem_name(previous)
37
37
  )
38
- add_offense(current, message: message)
38
+
39
+ add_offense(current, message: message) do |corrector|
40
+ OrderedGemCorrector.correct(
41
+ processed_source,
42
+ current,
43
+ previous_declaration(current),
44
+ treat_comments_as_separators
45
+ ).call(corrector)
46
+ end
39
47
  end
40
48
 
41
49
  def gem_name(declaration_node)
@@ -36,7 +36,12 @@ module RuboCop
36
36
  def check_percent_array(node)
37
37
  array_style_detected(:percent, node.values.size)
38
38
 
39
- return unless style == :brackets || invalid_percent_array_contents?(node)
39
+ brackets_required = invalid_percent_array_contents?(node)
40
+ return unless style == :brackets || brackets_required
41
+
42
+ # If in percent style but brackets are required due to
43
+ # string content, the file should be excluded in auto-gen-config
44
+ no_acceptable_style! if brackets_required
40
45
 
41
46
  bracketed_array = build_bracketed_array(node)
42
47
  message = format(self.class::ARRAY_MSG, prefer: bracketed_array)
@@ -93,7 +93,15 @@ module RuboCop
93
93
  end
94
94
 
95
95
  def aligned_assignment?(range, line)
96
- range.source[-1] == '=' && line[range.last_column - 1] == '='
96
+ (range.source[-1] == '=' && line[range.last_column - 1] == '=') ||
97
+ aligned_with_append_operator?(range, line)
98
+ end
99
+
100
+ def aligned_with_append_operator?(range, line)
101
+ last_column = range.last_column
102
+
103
+ (range.source == '<<' && line[last_column - 1] == '=') ||
104
+ (range.source[-1] == '=' && line[(last_column - 2)..(last_column - 1)] == '<<')
97
105
  end
98
106
 
99
107
  def aligned_identical?(range, line)
@@ -28,7 +28,7 @@ module RuboCop
28
28
  end
29
29
 
30
30
  def space_missing?(token1, token2)
31
- token1.line == token2.line && token2.column == token1.column + offset
31
+ same_line?(token1, token2) && token2.column == token1.column + offset
32
32
  end
33
33
 
34
34
  def space_required_before?(token)
@@ -32,7 +32,7 @@ module RuboCop
32
32
  end
33
33
 
34
34
  def space_missing?(token1, token2)
35
- token1.line == token2.line && token2.begin_pos > token1.end_pos
35
+ same_line?(token1, token2) && token2.begin_pos > token1.end_pos
36
36
  end
37
37
 
38
38
  def space_required_after?(token)
@@ -54,7 +54,7 @@ module RuboCop
54
54
  end
55
55
 
56
56
  def first_line_comment(node)
57
- comment = processed_source.find_comment { |c| c.loc.line == node.loc.line }
57
+ comment = processed_source.find_comment { |c| same_line?(c, node) }
58
58
  return unless comment
59
59
 
60
60
  comment_source = comment.loc.expression.source
@@ -13,7 +13,11 @@ module RuboCop
13
13
  if style == :single_quotes
14
14
  !double_quotes_required?(src)
15
15
  else
16
- !/" | \\[^'\\] | \#[@{$]/x.match?(src)
16
+ # The string needs single quotes if:
17
+ # 1. It contains a double quote
18
+ # 2. It contains text that would become an escape sequence with double quotes
19
+ # 3. It contains text that would become an interpolation with double quotes
20
+ !/" | (?<!\\)\\[abcefMnrtuUx0-7] | \#[@{$]/x.match?(src)
17
21
  end
18
22
  end
19
23
  end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  end
11
11
 
12
12
  def body_on_first_line?(node, body)
13
- node.source_range.first_line == body.source_range.first_line
13
+ same_line?(node, body)
14
14
  end
15
15
 
16
16
  def first_part_of(body)
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Style/AsciiComments
4
-
5
3
  module RuboCop
6
4
  module Cop
7
5
  module Naming
@@ -90,4 +88,3 @@ module RuboCop
90
88
  end
91
89
  end
92
90
  end
93
- # rubocop:enable Style/AsciiComments
@@ -24,7 +24,7 @@ module RuboCop
24
24
  # # With `AllowNamesEndingInNumbers` set to false
25
25
  # foo { |num1, num2| num1 * num2 }
26
26
  #
27
- # # With `MinParamNameLength` set to number greater than 1
27
+ # # With `MinNameLength` set to number greater than 1
28
28
  # baz { |a, b, c| do_stuff(a, b, c) }
29
29
  #
30
30
  # # good