rubocop 1.32.0 → 1.37.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 (189) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +104 -16
  4. data/config/obsoletion.yml +23 -1
  5. data/lib/rubocop/arguments_env.rb +17 -0
  6. data/lib/rubocop/arguments_file.rb +17 -0
  7. data/lib/rubocop/cache_config.rb +29 -0
  8. data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +2 -2
  9. data/lib/rubocop/cli/command/execute_runner.rb +7 -7
  10. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  11. data/lib/rubocop/cli/command/suggest_extensions.rb +53 -15
  12. data/lib/rubocop/config.rb +1 -1
  13. data/lib/rubocop/config_finder.rb +68 -0
  14. data/lib/rubocop/config_loader.rb +12 -40
  15. data/lib/rubocop/config_loader_resolver.rb +1 -5
  16. data/lib/rubocop/config_obsoletion/changed_parameter.rb +5 -0
  17. data/lib/rubocop/config_obsoletion/parameter_rule.rb +4 -0
  18. data/lib/rubocop/config_obsoletion.rb +7 -2
  19. data/lib/rubocop/cop/cop.rb +1 -1
  20. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +58 -0
  21. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  22. data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
  23. data/lib/rubocop/cop/generator.rb +1 -2
  24. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
  25. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +62 -0
  26. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  27. data/lib/rubocop/cop/layout/block_alignment.rb +16 -12
  28. data/lib/rubocop/cop/layout/block_end_newline.rb +35 -5
  29. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
  30. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
  31. data/lib/rubocop/cop/layout/end_of_line.rb +4 -4
  32. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -1
  33. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  34. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  35. data/lib/rubocop/cop/layout/indentation_width.rb +6 -2
  36. data/lib/rubocop/cop/layout/line_length.rb +4 -1
  37. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  38. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  39. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  41. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
  43. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +13 -9
  44. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +25 -9
  45. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +28 -3
  46. data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
  47. data/lib/rubocop/cop/legacy/corrector.rb +1 -1
  48. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +21 -8
  49. data/lib/rubocop/cop/lint/debugger.rb +26 -16
  50. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  51. data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
  52. data/lib/rubocop/cop/lint/duplicate_methods.rb +11 -1
  53. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +25 -6
  54. data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
  55. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  56. data/lib/rubocop/cop/lint/empty_class.rb +3 -1
  57. data/lib/rubocop/cop/lint/empty_conditional_body.rb +107 -1
  58. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
  59. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +4 -0
  60. data/lib/rubocop/cop/lint/nested_method_definition.rb +50 -1
  61. data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
  62. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +6 -6
  63. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
  64. data/lib/rubocop/cop/lint/number_conversion.rb +24 -8
  65. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
  66. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
  67. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +12 -1
  68. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
  69. data/lib/rubocop/cop/lint/redundant_require_statement.rb +29 -9
  70. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +9 -3
  71. data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
  72. data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
  73. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  74. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -2
  75. data/lib/rubocop/cop/lint/shadowed_exception.rb +15 -10
  76. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +27 -3
  77. data/lib/rubocop/cop/lint/unreachable_loop.rb +9 -3
  78. data/lib/rubocop/cop/lint/unused_method_argument.rb +4 -0
  79. data/lib/rubocop/cop/lint/useless_access_modifier.rb +8 -6
  80. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
  81. data/lib/rubocop/cop/lint/void.rb +2 -0
  82. data/lib/rubocop/cop/metrics/abc_size.rb +3 -1
  83. data/lib/rubocop/cop/metrics/block_length.rb +6 -7
  84. data/lib/rubocop/cop/metrics/method_length.rb +8 -8
  85. data/lib/rubocop/cop/mixin/allowed_methods.rb +20 -1
  86. data/lib/rubocop/cop/mixin/allowed_pattern.rb +17 -1
  87. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  88. data/lib/rubocop/cop/mixin/comments_help.rb +17 -1
  89. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -1
  90. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
  91. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +82 -4
  92. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
  93. data/lib/rubocop/cop/mixin/method_complexity.rb +8 -13
  94. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
  95. data/lib/rubocop/cop/mixin/range_help.rb +4 -5
  96. data/lib/rubocop/cop/mixin/rescue_node.rb +3 -1
  97. data/lib/rubocop/cop/mixin/surrounding_space.rb +6 -5
  98. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  99. data/lib/rubocop/cop/naming/constant_name.rb +2 -2
  100. data/lib/rubocop/cop/naming/inclusive_language.rb +1 -1
  101. data/lib/rubocop/cop/naming/predicate_name.rb +24 -3
  102. data/lib/rubocop/cop/style/access_modifier_declarations.rb +97 -1
  103. data/lib/rubocop/cop/style/accessor_grouping.rb +7 -3
  104. data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
  105. data/lib/rubocop/cop/style/block_delimiters.rb +26 -7
  106. data/lib/rubocop/cop/style/case_equality.rb +40 -10
  107. data/lib/rubocop/cop/style/class_and_module_children.rb +4 -4
  108. data/lib/rubocop/cop/style/class_equality_comparison.rb +32 -7
  109. data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
  110. data/lib/rubocop/cop/style/collection_compact.rb +6 -1
  111. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  112. data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
  113. data/lib/rubocop/cop/style/double_negation.rb +2 -0
  114. data/lib/rubocop/cop/style/each_for_simple_loop.rb +41 -6
  115. data/lib/rubocop/cop/style/each_with_object.rb +39 -8
  116. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  117. data/lib/rubocop/cop/style/empty_heredoc.rb +15 -1
  118. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  119. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  120. data/lib/rubocop/cop/style/endless_method.rb +1 -1
  121. data/lib/rubocop/cop/style/explicit_block_argument.rb +4 -0
  122. data/lib/rubocop/cop/style/for.rb +2 -0
  123. data/lib/rubocop/cop/style/format_string_token.rb +21 -8
  124. data/lib/rubocop/cop/style/guard_clause.rb +27 -16
  125. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
  126. data/lib/rubocop/cop/style/hash_except.rb +0 -4
  127. data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
  128. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  129. data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
  130. data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +15 -4
  132. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -1
  133. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -7
  134. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -6
  135. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  136. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
  137. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
  138. data/lib/rubocop/cop/style/negated_if_else_condition.rb +7 -1
  139. data/lib/rubocop/cop/style/next.rb +3 -5
  140. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  141. data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
  142. data/lib/rubocop/cop/style/numeric_predicate.rb +28 -8
  143. data/lib/rubocop/cop/style/object_then.rb +2 -0
  144. data/lib/rubocop/cop/style/operator_method_call.rb +39 -0
  145. data/lib/rubocop/cop/style/perl_backrefs.rb +22 -1
  146. data/lib/rubocop/cop/style/proc.rb +4 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +3 -0
  148. data/lib/rubocop/cop/style/redundant_condition.rb +24 -6
  149. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
  151. data/lib/rubocop/cop/style/redundant_parentheses.rb +19 -22
  152. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +8 -1
  153. data/lib/rubocop/cop/style/redundant_self.rb +2 -0
  154. data/lib/rubocop/cop/style/redundant_sort.rb +21 -6
  155. data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
  156. data/lib/rubocop/cop/style/redundant_string_escape.rb +173 -0
  157. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  158. data/lib/rubocop/cop/style/safe_navigation.rb +4 -2
  159. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  160. data/lib/rubocop/cop/style/sole_nested_conditional.rb +14 -5
  161. data/lib/rubocop/cop/style/static_class.rb +32 -1
  162. data/lib/rubocop/cop/style/symbol_array.rb +3 -1
  163. data/lib/rubocop/cop/style/symbol_proc.rb +38 -12
  164. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -13
  165. data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
  166. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  167. data/lib/rubocop/cop/style/word_array.rb +3 -1
  168. data/lib/rubocop/cop/util.rb +1 -1
  169. data/lib/rubocop/ext/range.rb +15 -0
  170. data/lib/rubocop/feature_loader.rb +94 -0
  171. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  172. data/lib/rubocop/formatter/disabled_config_formatter.rb +9 -3
  173. data/lib/rubocop/formatter/html_formatter.rb +3 -3
  174. data/lib/rubocop/formatter/markdown_formatter.rb +1 -1
  175. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  176. data/lib/rubocop/options.rb +13 -13
  177. data/lib/rubocop/result_cache.rb +22 -20
  178. data/lib/rubocop/rspec/shared_contexts.rb +13 -1
  179. data/lib/rubocop/runner.rb +4 -0
  180. data/lib/rubocop/server/cache.rb +41 -2
  181. data/lib/rubocop/server/cli.rb +26 -2
  182. data/lib/rubocop/server/client_command/exec.rb +5 -0
  183. data/lib/rubocop/server/core.rb +2 -1
  184. data/lib/rubocop/server/socket_reader.rb +5 -1
  185. data/lib/rubocop/server.rb +1 -1
  186. data/lib/rubocop/version.rb +8 -2
  187. data/lib/rubocop.rb +8 -3
  188. metadata +20 -9
  189. data/lib/rubocop/cop/mixin/ignored_methods.rb +0 -52
@@ -8,14 +8,14 @@ module RuboCop
8
8
  # These can be replaced by their respective predicate methods.
9
9
  # This cop can also be configured to do the reverse.
10
10
  #
11
- # This cop can be customized ignored methods with `IgnoredMethods`.
12
- # By default, there are no methods to ignored.
11
+ # This cop can be customized allowed methods with `AllowedMethods`.
12
+ # By default, there are no methods to allowed.
13
13
  #
14
14
  # This cop disregards `#nonzero?` as its value is truthy or falsey,
15
15
  # but not `true` and `false`, and thus not always interchangeable with
16
16
  # `!= 0`.
17
17
  #
18
- # This cop ignores comparisons to global variables, since they are often
18
+ # This cop allows comparisons to global variables, since they are often
19
19
  # populated with objects which can be compared with integers, but are
20
20
  # not themselves `Integer` polymorphic.
21
21
  #
@@ -46,13 +46,13 @@ module RuboCop
46
46
  # 0 > foo
47
47
  # bar.baz > 0
48
48
  #
49
- # @example IgnoredMethods: [] (default) with EnforcedStyle: predicate
49
+ # @example AllowedMethods: [] (default) with EnforcedStyle: predicate
50
50
  # # bad
51
51
  # foo == 0
52
52
  # 0 > foo
53
53
  # bar.baz > 0
54
54
  #
55
- # @example IgnoredMethods: [==] with EnforcedStyle: predicate
55
+ # @example AllowedMethods: [==] with EnforcedStyle: predicate
56
56
  # # good
57
57
  # foo == 0
58
58
  #
@@ -60,9 +60,25 @@ module RuboCop
60
60
  # 0 > foo
61
61
  # bar.baz > 0
62
62
  #
63
+ # @example AllowedPatterns: [] (default) with EnforcedStyle: comparison
64
+ # # bad
65
+ # foo.zero?
66
+ # foo.negative?
67
+ # bar.baz.positive?
68
+ #
69
+ # @example AllowedPatterns: ['zero'] with EnforcedStyle: predicate
70
+ # # good
71
+ # # bad
72
+ # foo.zero?
73
+ #
74
+ # # bad
75
+ # foo.negative?
76
+ # bar.baz.positive?
77
+ #
63
78
  class NumericPredicate < Base
64
79
  include ConfigurableEnforcedStyle
65
- include IgnoredMethods
80
+ include AllowedMethods
81
+ include AllowedPattern
66
82
  extend AutoCorrector
67
83
 
68
84
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
@@ -75,9 +91,9 @@ module RuboCop
75
91
  numeric, replacement = check(node)
76
92
  return unless numeric
77
93
 
78
- return if ignored_method?(node.method_name) ||
94
+ return if allowed_method_name?(node.method_name) ||
79
95
  node.each_ancestor(:send, :block).any? do |ancestor|
80
- ignored_method?(ancestor.method_name)
96
+ allowed_method_name?(ancestor.method_name)
81
97
  end
82
98
 
83
99
  message = format(MSG, prefer: replacement, current: node.source)
@@ -88,6 +104,10 @@ module RuboCop
88
104
 
89
105
  private
90
106
 
107
+ def allowed_method_name?(name)
108
+ allowed_method?(name) || matches_allowed_pattern?(name)
109
+ end
110
+
91
111
  def check(node)
92
112
  numeric, operator =
93
113
  if style == :predicate
@@ -32,6 +32,8 @@ module RuboCop
32
32
  check_method_node(node.send_node)
33
33
  end
34
34
 
35
+ alias on_numblock on_block
36
+
35
37
  def on_send(node)
36
38
  return unless node.arguments.one? && node.first_argument.block_pass_type?
37
39
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for redundant dot before operator method call.
7
+ # The target operator methods are `|`, `^`, `&`, `<=>`, `==`, `===`, `=~`, `>`, `>=`, `<`,
8
+ # `<=`, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `~`, `!`, `!=`, and `!~`.
9
+ #
10
+ # @example
11
+ #
12
+ # # bad
13
+ # foo.+ bar
14
+ # foo.& bar
15
+ #
16
+ # # good
17
+ # foo + bar
18
+ # foo & bar
19
+ #
20
+ class OperatorMethodCall < Base
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Redundant dot detected.'
24
+ RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ ! != !~].freeze
25
+
26
+ def on_send(node)
27
+ return unless (dot = node.loc.dot)
28
+
29
+ _lhs, _op, rhs = *node
30
+ return if rhs.children.first
31
+
32
+ add_offense(dot) do |corrector|
33
+ corrector.replace(dot, ' ')
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -83,10 +83,31 @@ module RuboCop
83
83
  end
84
84
  end
85
85
 
86
+ # @private
87
+ # @param [RuboCop::AST::Node] node
88
+ # @return [String, nil]
89
+ def preferred_expression_to_node_with_constant_prefix(node)
90
+ expression = preferred_expression_to(node)
91
+ return unless expression
92
+
93
+ "#{constant_prefix(node)}#{expression}"
94
+ end
95
+
96
+ # @private
97
+ # @param [RuboCop::AST::Node] node
98
+ # @return [String]
99
+ def constant_prefix(node)
100
+ if node.each_ancestor(:class, :module).any?
101
+ '::'
102
+ else
103
+ ''
104
+ end
105
+ end
106
+
86
107
  # @private
87
108
  # @param [RuboCop::AST::Node] node
88
109
  def on_back_ref_or_gvar_or_nth_ref(node)
89
- preferred_expression = preferred_expression_to(node)
110
+ preferred_expression = preferred_expression_to_node_with_constant_prefix(node)
90
111
  return unless preferred_expression
91
112
 
92
113
  add_offense(
@@ -19,7 +19,8 @@ module RuboCop
19
19
  MSG = 'Use `proc` instead of `Proc.new`.'
20
20
 
21
21
  # @!method proc_new?(node)
22
- def_node_matcher :proc_new?, '(block $(send (const {nil? cbase} :Proc) :new) ...)'
22
+ def_node_matcher :proc_new?,
23
+ '({block numblock} $(send (const {nil? cbase} :Proc) :new) ...)'
23
24
 
24
25
  def on_block(node)
25
26
  proc_new?(node) do |block_method|
@@ -28,6 +29,8 @@ module RuboCop
28
29
  end
29
30
  end
30
31
  end
32
+
33
+ alias on_numblock on_block
31
34
  end
32
35
  end
33
36
  end
@@ -75,6 +75,7 @@ module RuboCop
75
75
 
76
76
  def on_def(node)
77
77
  return unless node.body&.kwbegin_type?
78
+ return if node.endless? && !node.body.children.one?
78
79
 
79
80
  register_offense(node.body)
80
81
  end
@@ -89,6 +90,8 @@ module RuboCop
89
90
  register_offense(node.body)
90
91
  end
91
92
 
93
+ alias on_numblock on_block
94
+
92
95
  def on_kwbegin(node)
93
96
  return unless (target_node = offensive_kwbegins(node).to_a.last)
94
97
 
@@ -145,25 +145,34 @@ module RuboCop
145
145
 
146
146
  return false unless if_branch && else_branch
147
147
 
148
- if_branch.send_type? && if_branch.arguments.count == 1 &&
149
- else_branch.send_type? && else_branch.arguments.count == 1 &&
148
+ single_argument_method?(if_branch) && single_argument_method?(else_branch) &&
150
149
  same_method?(if_branch, else_branch)
151
150
  end
152
151
 
152
+ def single_argument_method?(node)
153
+ node.send_type? && !node.method?(:[]) && node.arguments.one?
154
+ end
155
+
153
156
  def same_method?(if_branch, else_branch)
154
157
  if_branch.method?(else_branch.method_name) && if_branch.receiver == else_branch.receiver
155
158
  end
156
159
 
157
- def if_source(if_branch)
160
+ def if_source(if_branch, arithmetic_operation)
158
161
  if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
159
162
  if_branch.source.delete_suffix(')')
163
+ elsif arithmetic_operation
164
+ argument_source = if_branch.first_argument.source
165
+
166
+ "#{if_branch.receiver.source} #{if_branch.method_name} (#{argument_source}"
160
167
  else
161
168
  if_branch.source
162
169
  end
163
170
  end
164
171
 
165
- def else_source(else_branch)
166
- if branches_have_method?(else_branch.parent)
172
+ def else_source(else_branch, arithmetic_operation) # rubocop:disable Metrics/AbcSize
173
+ if arithmetic_operation
174
+ "#{else_branch.first_argument.source})"
175
+ elsif branches_have_method?(else_branch.parent)
167
176
  else_source_if_has_method(else_branch)
168
177
  elsif require_parentheses?(else_branch)
169
178
  "(#{else_branch.source})"
@@ -198,7 +207,12 @@ module RuboCop
198
207
 
199
208
  def make_ternary_form(node)
200
209
  _condition, if_branch, else_branch = *node
201
- ternary_form = [if_source(if_branch), else_source(else_branch)].join(' || ')
210
+ arithmetic_operation = use_arithmetic_operation?(if_branch)
211
+
212
+ ternary_form = [
213
+ if_source(if_branch, arithmetic_operation),
214
+ else_source(else_branch, arithmetic_operation)
215
+ ].join(' || ')
202
216
  ternary_form += ')' if branches_have_method?(node) && if_branch.parenthesized?
203
217
 
204
218
  if node.parent&.send_type?
@@ -227,6 +241,10 @@ module RuboCop
227
241
  node.hash_type? && !node.braces?
228
242
  end
229
243
 
244
+ def use_arithmetic_operation?(node)
245
+ node.respond_to?(:arithmetic_operation?) && node.arithmetic_operation?
246
+ end
247
+
230
248
  def without_argument_parentheses_method?(node)
231
249
  node.send_type? && !node.arguments.empty? &&
232
250
  !node.parenthesized? && !node.operator_method? && !node.assignment_method?
@@ -50,7 +50,7 @@ module RuboCop
50
50
  ${nil? #basic_literal? #const_type?})
51
51
  PATTERN
52
52
 
53
- def on_block(node)
53
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
54
54
  redundant_fetch_block_candidate?(node) do |send, body|
55
55
  return if should_not_check?(send, body)
56
56
 
@@ -140,7 +140,9 @@ module RuboCop
140
140
  end
141
141
 
142
142
  def allow_comments?(node)
143
- cop_config['AllowComments'] && contains_comments?(node)
143
+ return false unless cop_config['AllowComments']
144
+
145
+ contains_comments?(node) && !comments_contain_disables?(node, name)
144
146
  end
145
147
 
146
148
  def same_args?(super_node, args)
@@ -29,6 +29,9 @@ module RuboCop
29
29
  # @!method rescue?(node)
30
30
  def_node_matcher :rescue?, '{^resbody ^^resbody}'
31
31
 
32
+ # @!method allowed_pin_operator?(node)
33
+ def_node_matcher :allowed_pin_operator?, '^(pin (begin !{lvar ivar cvar gvar}))'
34
+
32
35
  # @!method arg_in_call_with_block?(node)
33
36
  def_node_matcher :arg_in_call_with_block?, '^^(block (send _ _ equal?(%0) ...) ...)'
34
37
 
@@ -44,6 +47,7 @@ module RuboCop
44
47
  empty_parentheses?(node) ||
45
48
  first_arg_begins_with_hash_literal?(node) ||
46
49
  rescue?(node) ||
50
+ allowed_pin_operator?(node) ||
47
51
  allowed_expression?(node)
48
52
  end
49
53
 
@@ -57,8 +61,8 @@ module RuboCop
57
61
  def allowed_expression?(node)
58
62
  allowed_ancestor?(node) ||
59
63
  allowed_method_call?(node) ||
60
- allowed_array_or_hash_element?(node) ||
61
- allowed_multiple_expression?(node)
64
+ allowed_multiple_expression?(node) ||
65
+ allowed_ternary?(node)
62
66
  end
63
67
 
64
68
  def allowed_ancestor?(node)
@@ -80,6 +84,19 @@ module RuboCop
80
84
  !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
81
85
  end
82
86
 
87
+ def allowed_ternary?(node)
88
+ return unless node&.parent&.if_type?
89
+
90
+ node.parent.ternary? && ternary_parentheses_required?
91
+ end
92
+
93
+ def ternary_parentheses_required?
94
+ config = @config.for_cop('Style/TernaryParentheses')
95
+ allowed_styles = %w[require_parentheses require_parentheses_when_complex]
96
+
97
+ config.fetch('Enabled') && allowed_styles.include?(config['EnforcedStyle'])
98
+ end
99
+
83
100
  def like_method_argument_parentheses?(node)
84
101
  node.send_type? && node.arguments.one? &&
85
102
  !node.arithmetic_operation? && node.first_argument.begin_type?
@@ -153,26 +170,6 @@ module RuboCop
153
170
  node.parent&.keyword?
154
171
  end
155
172
 
156
- def allowed_array_or_hash_element?(node)
157
- # Don't flag
158
- # ```
159
- # { a: (1
160
- # ), }
161
- # ```
162
- hash_or_array_element?(node) && only_closing_paren_before_comma?(node)
163
- end
164
-
165
- def hash_or_array_element?(node)
166
- node.each_ancestor(:array, :hash).any?
167
- end
168
-
169
- def only_closing_paren_before_comma?(node)
170
- source_buffer = node.source_range.source_buffer
171
- line_range = source_buffer.line_range(node.loc.end.line)
172
-
173
- /^\s*\)\s*,/.match?(line_range.source)
174
- end
175
-
176
173
  def disallowed_literal?(begin_node, node)
177
174
  node.literal? && !node.range_type? && !raised_to_power_negative_numeric?(begin_node, node)
178
175
  end
@@ -74,7 +74,7 @@ module RuboCop
74
74
 
75
75
  non_redundant =
76
76
  whitespace_in_free_space_mode?(node, class_elem) ||
77
- backslash_b?(class_elem) ||
77
+ backslash_b?(class_elem) || backslash_zero?(class_elem) ||
78
78
  requires_escape_outside_char_class?(class_elem)
79
79
 
80
80
  !non_redundant
@@ -104,6 +104,13 @@ module RuboCop
104
104
  elem == '\b'
105
105
  end
106
106
 
107
+ def backslash_zero?(elem)
108
+ # See https://github.com/rubocop/rubocop/issues/11067 for details - in short "\0" != "0" -
109
+ # the former means an Unicode code point `"\u0000"`, the latter a number character `"0"`.
110
+ # Similarly "\032" means "\u001A". Other numbers starting with "\0" can also be mentioned.
111
+ elem == '\0'
112
+ end
113
+
107
114
  def requires_escape_outside_char_class?(elem)
108
115
  REQUIRES_ESCAPE_OUTSIDE_CHAR_CLASS_CHARS.include?(elem)
109
116
  end
@@ -120,6 +120,8 @@ module RuboCop
120
120
  add_scope(node, @local_variables_scopes[node])
121
121
  end
122
122
 
123
+ alias on_numblock on_block
124
+
123
125
  def on_if(node)
124
126
  # Allow conditional nodes to use `self` in the condition if that variable
125
127
  # name is used in an `lvasgn` or `masgn` within the `if`.
@@ -123,13 +123,8 @@ module RuboCop
123
123
 
124
124
  def register_offense(node, sort_node, sorter, accessor)
125
125
  message = message(node, sorter, accessor)
126
-
127
126
  add_offense(offense_range(sort_node, node), message: message) do |corrector|
128
- # Remove accessor, e.g. `first` or `[-1]`.
129
- corrector.remove(range_between(accessor_start(node), node.loc.expression.end_pos))
130
-
131
- # Replace "sort" or "sort_by" with the appropriate min/max method.
132
- corrector.replace(sort_node.loc.selector, suggestion(sorter, accessor, arg_value(node)))
127
+ autocorrect(corrector, node, sort_node, sorter, accessor)
133
128
  end
134
129
  end
135
130
 
@@ -149,6 +144,20 @@ module RuboCop
149
144
  accessor_source: accessor_source)
150
145
  end
151
146
 
147
+ def autocorrect(corrector, node, sort_node, sorter, accessor)
148
+ # Remove accessor, e.g. `first` or `[-1]`.
149
+ corrector.remove(range_between(accessor_start(node), node.loc.expression.end_pos))
150
+ # Replace "sort" or "sort_by" with the appropriate min/max method.
151
+ corrector.replace(sort_node.loc.selector, suggestion(sorter, accessor, arg_value(node)))
152
+ # Replace to avoid syntax errors when followed by a logical operator.
153
+ replace_with_logical_operator(corrector, node) if with_logical_operator?(node)
154
+ end
155
+
156
+ def replace_with_logical_operator(corrector, node)
157
+ corrector.insert_after(node.child_nodes.first, " #{node.parent.loc.operator.source}")
158
+ corrector.remove(node.parent.loc.operator)
159
+ end
160
+
152
161
  def suggestion(sorter, accessor, arg)
153
162
  base(accessor, arg) + suffix(sorter)
154
163
  end
@@ -187,6 +196,12 @@ module RuboCop
187
196
  node.loc.selector.begin_pos
188
197
  end
189
198
  end
199
+
200
+ def with_logical_operator?(node)
201
+ return unless (parent = node.parent)
202
+
203
+ parent.or_type? || parent.and_type?
204
+ end
190
205
  end
191
206
  end
192
207
  end
@@ -19,18 +19,24 @@ module RuboCop
19
19
  include RangeHelp
20
20
  extend AutoCorrector
21
21
 
22
- MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
23
-
24
- # @!method redundant_sort_by(node)
25
- def_node_matcher :redundant_sort_by, <<~PATTERN
26
- (block $(send _ :sort_by) (args (arg $_x)) (lvar _x))
27
- PATTERN
22
+ MSG_BLOCK = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
23
+ MSG_NUMBLOCK = 'Use `sort` instead of `sort_by { _1 }`.'
28
24
 
29
25
  def on_block(node)
30
- redundant_sort_by(node) do |send, var_name|
26
+ redundant_sort_by_block(node) do |send, var_name|
31
27
  range = sort_by_range(send, node)
32
28
 
33
- add_offense(range, message: format(MSG, var: var_name)) do |corrector|
29
+ add_offense(range, message: format(MSG_BLOCK, var: var_name)) do |corrector|
30
+ corrector.replace(range, 'sort')
31
+ end
32
+ end
33
+ end
34
+
35
+ def on_numblock(node)
36
+ redundant_sort_by_numblock(node) do |send|
37
+ range = sort_by_range(send, node)
38
+
39
+ add_offense(range, message: format(MSG_NUMBLOCK)) do |corrector|
34
40
  corrector.replace(range, 'sort')
35
41
  end
36
42
  end
@@ -38,6 +44,16 @@ module RuboCop
38
44
 
39
45
  private
40
46
 
47
+ # @!method redundant_sort_by_block(node)
48
+ def_node_matcher :redundant_sort_by_block, <<~PATTERN
49
+ (block $(send _ :sort_by) (args (arg $_x)) (lvar _x))
50
+ PATTERN
51
+
52
+ # @!method redundant_sort_by_numblock(node)
53
+ def_node_matcher :redundant_sort_by_numblock, <<~PATTERN
54
+ (numblock $(send _ :sort_by) 1 (lvar :_1))
55
+ PATTERN
56
+
41
57
  def sort_by_range(send, node)
42
58
  range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
43
59
  end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for redundant escapes in string literals.
7
+ #
8
+ # @example
9
+ # # bad - no need to escape # without following {/$/@
10
+ # "\#foo"
11
+ #
12
+ # # bad - no need to escape single quotes inside double quoted string
13
+ # "\'foo\'"
14
+ #
15
+ # # bad - heredocs are also checked for unnecessary escapes
16
+ # <<~STR
17
+ # \#foo \"foo\"
18
+ # STR
19
+ #
20
+ # # good
21
+ # "#foo"
22
+ #
23
+ # # good
24
+ # "\#{no_interpolation}"
25
+ #
26
+ # # good
27
+ # "'foo'"
28
+ #
29
+ # # good
30
+ # "foo\
31
+ # bar"
32
+ #
33
+ # # good
34
+ # <<~STR
35
+ # #foo "foo"
36
+ # STR
37
+ class RedundantStringEscape < Base
38
+ include MatchRange
39
+ include RangeHelp
40
+ extend AutoCorrector
41
+
42
+ MSG = 'Redundant escape of %<char>s inside string literal.'
43
+
44
+ def on_str(node)
45
+ return if node.parent&.regexp_type? || node.parent&.xstr_type?
46
+
47
+ str_contents_range = str_contents_range(node)
48
+ return unless str_contents_range.source.include?('\\')
49
+
50
+ each_match_range(str_contents_range, /(\\.)/) do |range|
51
+ next if allowed_escape?(node, range.resize(3))
52
+
53
+ add_offense(range) do |corrector|
54
+ corrector.remove_leading(range, 1)
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def message(range)
62
+ format(MSG, char: range.source.chars.last)
63
+ end
64
+
65
+ def str_contents_range(node)
66
+ if heredoc?(node)
67
+ node.loc.heredoc_body
68
+ elsif begin_loc_present?(node)
69
+ contents_range(node)
70
+ else
71
+ node.loc.expression
72
+ end
73
+ end
74
+
75
+ def begin_loc_present?(node)
76
+ # e.g. a __FILE__ literal has no begin loc so we can't query if it's nil
77
+ node.loc.to_hash.key?(:begin) && !node.loc.begin.nil?
78
+ end
79
+
80
+ def allowed_escape?(node, range)
81
+ escaped = range.source[(1..-1)]
82
+
83
+ # Inside a single-quoted string, escapes (except \\ and \') do not have special meaning,
84
+ # and so are not redundant, as they are a literal backslash.
85
+ return true if interpolation_not_enabled?(node)
86
+
87
+ # Strictly speaking a few single-letter chars are currently unnecessary to "escape", e.g.
88
+ # d, but enumerating them is rather difficult, and their behavior could change over time
89
+ # with different versions of Ruby so that e.g. /\d/ != /d/
90
+ return true if /[\n\\[[:alnum:]]]/.match?(escaped[0])
91
+
92
+ return true if escaped[0] == ' ' && percent_array_literal?(node)
93
+
94
+ # Allow #{foo}, #$foo, #@foo, and #@@foo for escaping local, global, instance and class
95
+ # variable interpolations inside
96
+ return true if /\A#[{$@]/.match?(escaped)
97
+ return true if delimiter?(node, escaped[0])
98
+
99
+ false
100
+ end
101
+
102
+ def interpolation_not_enabled?(node)
103
+ single_quoted?(node) ||
104
+ percent_w_literal?(node) ||
105
+ percent_q_literal?(node) ||
106
+ heredoc_with_disabled_interpolation?(node)
107
+ end
108
+
109
+ def single_quoted?(node)
110
+ delimiter?(node, "'")
111
+ end
112
+
113
+ def percent_q_literal?(node)
114
+ if literal_in_interpolated_or_multiline_string?(node)
115
+ percent_q_literal?(node.parent)
116
+ else
117
+ node.source.start_with?('%q')
118
+ end
119
+ end
120
+
121
+ def array_literal?(node, prefix)
122
+ if literal_in_interpolated_or_multiline_string?(node)
123
+ array_literal?(node.parent, prefix)
124
+ else
125
+ node.parent&.array_type? && node.parent.source.start_with?(prefix)
126
+ end
127
+ end
128
+
129
+ def percent_w_literal?(node)
130
+ array_literal?(node, '%w')
131
+ end
132
+
133
+ def percent_w_upper_literal?(node)
134
+ array_literal?(node, '%W')
135
+ end
136
+
137
+ def percent_array_literal?(node)
138
+ (percent_w_literal?(node) || percent_w_upper_literal?(node))
139
+ end
140
+
141
+ def heredoc_with_disabled_interpolation?(node)
142
+ if heredoc?(node)
143
+ node.loc.expression.source.end_with?("'")
144
+ elsif node.parent&.dstr_type?
145
+ heredoc_with_disabled_interpolation?(node.parent)
146
+ else
147
+ false
148
+ end
149
+ end
150
+
151
+ def heredoc?(node)
152
+ (node.str_type? || node.dstr_type?) && node.heredoc?
153
+ end
154
+
155
+ def delimiter?(node, char)
156
+ return false if heredoc?(node)
157
+
158
+ if literal_in_interpolated_or_multiline_string?(node) || percent_array_literal?(node)
159
+ return delimiter?(node.parent, char)
160
+ end
161
+
162
+ delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
163
+
164
+ delimiters.include?(char)
165
+ end
166
+
167
+ def literal_in_interpolated_or_multiline_string?(node)
168
+ node.str_type? && !begin_loc_present?(node) && node.parent&.dstr_type?
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # reasons:
10
10
  #
11
11
  # * The syntax of modifier form `rescue` can be misleading because it
12
- # might led us to believe that `rescue` handles the given exception
12
+ # might lead us to believe that `rescue` handles the given exception
13
13
  # but it actually rescue all exceptions to return the given rescue
14
14
  # block. In this case, value returned by handle_error or
15
15
  # SomeException.