rbhint 0.85.1.rc1 → 0.85.1.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -2
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +13 -1
  5. data/lib/rbhint.rb +8 -0
  6. data/lib/rbhint/version.rb +1 -1
  7. data/lib/rubocop.rb +2 -0
  8. data/lib/rubocop/config.rb +1 -1
  9. data/lib/rubocop/config_loader_resolver.rb +1 -1
  10. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  11. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  12. data/lib/rubocop/cop/layout/assignment_indentation.rb +2 -2
  13. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  14. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -1
  15. data/lib/rubocop/cop/layout/case_indentation.rb +2 -2
  16. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  17. data/lib/rubocop/cop/layout/comment_indentation.rb +4 -4
  18. data/lib/rubocop/cop/layout/condition_position.rb +1 -1
  19. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  21. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  22. data/lib/rubocop/cop/layout/empty_comment.rb +2 -2
  23. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  24. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
  25. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  26. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
  27. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
  28. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
  29. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -1
  30. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  31. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -2
  32. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +1 -1
  34. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  35. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  36. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -6
  37. data/lib/rubocop/cop/layout/first_argument_indentation.rb +3 -3
  38. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  39. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +2 -2
  40. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  42. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -1
  43. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +1 -1
  44. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  45. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  46. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  47. data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -3
  48. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
  49. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  50. data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
  51. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  52. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  53. data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
  54. data/lib/rubocop/cop/layout/line_length.rb +6 -6
  55. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
  56. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +1 -1
  57. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  58. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
  60. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  62. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  63. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  64. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  65. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
  66. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  67. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  68. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  69. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  70. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +6 -6
  71. data/lib/rubocop/cop/layout/space_around_operators.rb +2 -2
  72. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  73. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  74. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  75. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
  76. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  77. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  78. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -1
  79. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
  80. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  81. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  82. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  83. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -1
  84. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  85. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  86. data/lib/rubocop/cop/lint/raise_exception.rb +4 -4
  87. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
  88. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  89. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  90. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  91. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  92. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  93. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  94. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  95. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  96. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  97. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  98. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  101. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
  102. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  103. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  104. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  105. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
  106. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  107. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  108. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  109. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  110. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  112. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  113. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  114. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  115. data/lib/rubocop/cop/style/copyright.rb +3 -3
  116. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  117. data/lib/rubocop/cop/style/documentation.rb +2 -2
  118. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  119. data/lib/rubocop/cop/style/encoding.rb +1 -1
  120. data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
  121. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  122. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  123. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  124. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  125. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  126. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  127. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  128. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  129. data/lib/rubocop/cop/style/next.rb +2 -2
  130. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  131. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  132. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  133. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  134. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  135. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  136. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  137. data/lib/rubocop/cop/style/sample.rb +1 -1
  138. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  139. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  140. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  141. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  142. data/lib/rubocop/cop/style/word_array.rb +1 -1
  143. data/lib/rubocop/cop/util.rb +2 -2
  144. data/lib/rubocop/cop/utils/format_string.rb +1 -1
  145. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  146. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  147. data/lib/rubocop/name_similarity.rb +6 -0
  148. data/lib/rubocop/path_util.rb +2 -2
  149. data/lib/rubocop/platform.rb +1 -1
  150. data/lib/rubocop/rspec/expect_offense.rb +12 -2
  151. data/lib/rubocop/target_finder.rb +1 -1
  152. data/lib/rubocop/target_ruby.rb +1 -1
  153. metadata +10 -2
@@ -148,7 +148,7 @@ module RuboCop
148
148
  end
149
149
 
150
150
  next_token = processed_source.tokens[token_number]
151
- token = next_token if next_token && next_token.text =~ Encoding::ENCODING_PATTERN
151
+ token = next_token if Encoding::ENCODING_PATTERN.match?(next_token&.text)
152
152
 
153
153
  token
154
154
  end
@@ -81,7 +81,7 @@ module RuboCop
81
81
 
82
82
  private
83
83
 
84
- def check_branches(branches)
84
+ def check_branches(branches) # rubocop:todo Metrics/CyclomaticComplexity
85
85
  # return if any branch is empty. An empty branch can be an `if`
86
86
  # without an `else` or a branch that contains only comments.
87
87
  return if branches.any?(&:nil?)
@@ -61,7 +61,7 @@ module RuboCop
61
61
  class IfInsideElse < Cop
62
62
  MSG = 'Convert `if` nested inside `else` to `elsif`.'
63
63
 
64
- def on_if(node)
64
+ def on_if(node) # rubocop:todo Metrics/CyclomaticComplexity
65
65
  return if node.ternary? || node.unless?
66
66
 
67
67
  else_branch = node.else_branch
@@ -178,7 +178,7 @@ module RuboCop
178
178
  end
179
179
 
180
180
  def camel_case_constant?(node)
181
- node.const_type? && node.source =~ CAMEL_CASE
181
+ node.const_type? && CAMEL_CASE.match?(node.source)
182
182
  end
183
183
 
184
184
  def dot_range(loc)
@@ -34,7 +34,7 @@ module RuboCop
34
34
  # shortcut out if the string does not look like an IP address
35
35
  return false unless could_be_ip?(contents)
36
36
 
37
- contents =~ ::Resolv::IPv4::Regex || contents =~ ::Resolv::IPv6::Regex
37
+ ::Resolv::IPv4::Regex.match?(contents) || ::Resolv::IPv6::Regex.match?(contents)
38
38
  end
39
39
 
40
40
  # Dummy implementation of method in ConfigurableEnforcedStyle that is
@@ -42,7 +42,7 @@ module RuboCop
42
42
  private
43
43
 
44
44
  def non_modifier_then?(node)
45
- node.loc.begin && node.loc.begin.source_line =~ NON_MODIFIER_THEN
45
+ NON_MODIFIER_THEN.match?(node.loc.begin&.source_line)
46
46
  end
47
47
  end
48
48
  end
@@ -17,12 +17,11 @@ module RuboCop
17
17
  #
18
18
  # # good
19
19
  # a = cond ? b : c
20
- # a =
21
- # if cond
22
- # b
23
- # else
24
- # c
25
- # end
20
+ # a = if cond
21
+ # b
22
+ # else
23
+ # c
24
+ # end
26
25
  class MultilineTernaryOperator < Cop
27
26
  MSG = 'Avoid multi-line ternary operators, ' \
28
27
  'use `if` or `unless` instead.'
@@ -32,6 +31,18 @@ module RuboCop
32
31
 
33
32
  add_offense(node)
34
33
  end
34
+
35
+ def autocorrect(node)
36
+ lambda do |corrector|
37
+ corrector.replace(node, <<~RUBY.chop)
38
+ if #{node.condition.source}
39
+ #{node.if_branch.source}
40
+ else
41
+ #{node.else_branch.source}
42
+ end
43
+ RUBY
44
+ end
45
+ end
35
46
  end
36
47
  end
37
48
  end
@@ -26,6 +26,33 @@ module RuboCop
26
26
  add_offense(nested_ternary)
27
27
  end
28
28
  end
29
+
30
+ def autocorrect(node)
31
+ if_node = if_node(node)
32
+
33
+ lambda do |corrector|
34
+ corrector.replace(if_node, <<~RUBY.chop)
35
+ if #{if_node.condition.source}
36
+ #{remove_parentheses(if_node.if_branch.source)}
37
+ else
38
+ #{if_node.else_branch.source}
39
+ end
40
+ RUBY
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def if_node(node)
47
+ node = node.parent
48
+ return node if node.if_type?
49
+
50
+ if_node(node)
51
+ end
52
+
53
+ def remove_parentheses(source)
54
+ source.gsub(/\A\(/, '').gsub(/\)\z/, '')
55
+ end
29
56
  end
30
57
  end
31
58
  end
@@ -191,7 +191,7 @@ module RuboCop
191
191
  end
192
192
 
193
193
  def end_followed_by_whitespace_only?(source_buffer, end_pos)
194
- source_buffer.source[end_pos..-1] =~ /\A\s*$/
194
+ /\A\s*$/.match?(source_buffer.source[end_pos..-1])
195
195
  end
196
196
 
197
197
  def reindentable_lines(node)
@@ -201,7 +201,7 @@ module RuboCop
201
201
  lines = (node.source_range.line + 1)...node.loc.end.line
202
202
  lines = lines.to_a - heredoc_lines(node)
203
203
  # Skip blank lines
204
- lines.reject { |lineno| buffer.source_line(lineno) =~ /\A\s*\z/ }
204
+ lines.reject { |lineno| /\A\s*\z/.match?(buffer.source_line(lineno)) }
205
205
  end
206
206
 
207
207
  # Adjust indentation of `lines` to match `node`
@@ -77,9 +77,9 @@ module RuboCop
77
77
  end
78
78
 
79
79
  def octal_literal_type(literal)
80
- if literal =~ OCTAL_ZERO_ONLY_REGEX && octal_zero_only?
80
+ if OCTAL_ZERO_ONLY_REGEX.match?(literal) && octal_zero_only?
81
81
  :octal_zero_only
82
- elsif literal =~ OCTAL_REGEX && !octal_zero_only?
82
+ elsif OCTAL_REGEX.match?(literal) && !octal_zero_only?
83
83
  :octal
84
84
  end
85
85
  end
@@ -102,7 +102,7 @@ module RuboCop
102
102
  delimiters_regexp = Regexp.union(delimiters)
103
103
  node
104
104
  .children.map { |n| string_source(n) }.compact
105
- .any? { |s| delimiters_regexp =~ s }
105
+ .any? { |s| delimiters_regexp.match?(s) }
106
106
  end
107
107
 
108
108
  def string_source(node)
@@ -156,7 +156,7 @@ module RuboCop
156
156
  source_buffer = node.source_range.source_buffer
157
157
  line_range = source_buffer.line_range(node.loc.end.line)
158
158
 
159
- line_range.source =~ /^\s*\)\s*,/
159
+ /^\s*\)\s*,/.match?(line_range.source)
160
160
  end
161
161
 
162
162
  def disallowed_literal?(begin_node, node)
@@ -97,13 +97,13 @@ module RuboCop
97
97
 
98
98
  return true if STRING_INTERPOLATION_REGEXP.match?(src)
99
99
 
100
- src.scan(/\\./).any? { |s| s =~ ESCAPED_NON_BACKSLASH }
100
+ src.scan(/\\./).any? { |s| ESCAPED_NON_BACKSLASH.match?(s) }
101
101
  end
102
102
 
103
103
  def acceptable_capital_q?(node)
104
104
  src = node.source
105
105
  src.include?(QUOTE) &&
106
- (src =~ STRING_INTERPOLATION_REGEXP ||
106
+ (STRING_INTERPOLATION_REGEXP.match?(src) ||
107
107
  (node.str_type? && double_quotes_required?(src)))
108
108
  end
109
109
  end
@@ -67,8 +67,8 @@ module RuboCop
67
67
  end
68
68
 
69
69
  def each_redundant_character_class(node)
70
- each_match_range(node.loc.expression, PATTERN) do |loc|
71
- yield loc
70
+ pattern_source(node).scan(PATTERN) do
71
+ yield match_range(node.loc.begin.end, Regexp.last_match)
72
72
  end
73
73
  end
74
74
 
@@ -18,6 +18,9 @@ module RuboCop
18
18
  # # good
19
19
  # %r/foo\/bar/
20
20
  #
21
+ # # good
22
+ # %r!foo\!bar!
23
+ #
21
24
  # # bad
22
25
  # /a\-b/
23
26
  #
@@ -63,27 +66,30 @@ module RuboCop
63
66
 
64
67
  private
65
68
 
66
- def slash_literal?(node)
67
- ['/', '%r/'].include?(node.loc.begin.source)
68
- end
69
-
70
69
  def allowed_escape?(node, char, within_character_class)
71
70
  # Strictly speaking a few single-letter metachars are currently
72
71
  # unnecessary to "escape", e.g. g, i, E, F, but enumerating them is
73
72
  # rather difficult, and their behaviour could change over time with
74
73
  # different versions of Ruby so that e.g. /\g/ != /g/
75
74
  return true if /[[:alnum:]]/.match?(char)
76
- return true if ALLOWED_ALWAYS_ESCAPES.include?(char)
75
+ return true if ALLOWED_ALWAYS_ESCAPES.include?(char) || delimiter?(node, char)
77
76
 
78
- if char == '/'
79
- slash_literal?(node)
80
- elsif within_character_class
77
+ if within_character_class
81
78
  ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
82
79
  else
83
80
  ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
84
81
  end
85
82
  end
86
83
 
84
+ def delimiter?(node, char)
85
+ delimiters = [
86
+ node.loc.begin.source.chars.last,
87
+ node.loc.end.source.chars.first
88
+ ]
89
+
90
+ delimiters.include?(char)
91
+ end
92
+
87
93
  def each_escape(node)
88
94
  pattern_source(node).each_char.with_index.reduce(
89
95
  [nil, false]
@@ -109,21 +115,6 @@ module RuboCop
109
115
 
110
116
  range_between(start, start + 2)
111
117
  end
112
-
113
- def pattern_source(node)
114
- freespace_mode = freespace_mode_regexp?(node)
115
-
116
- node.children.reject(&:regopt_type?).map do |child|
117
- source = child.source
118
-
119
- if freespace_mode
120
- # Remove comments to avoid misleading results
121
- source.sub(/(?<!\\)#.*/, '')
122
- else
123
- source
124
- end
125
- end.join
126
- end
127
118
  end
128
119
  end
129
120
  end
@@ -44,6 +44,11 @@ module RuboCop
44
44
  class RedundantSelf < Cop
45
45
  MSG = 'Redundant `self` detected.'
46
46
  KERNEL_METHODS = Kernel.methods(false)
47
+ KEYWORDS = %i[alias and begin break case class def defined? do
48
+ else elsif end ensure false for if in module
49
+ next nil not or redo rescue retry return self
50
+ super then true undef unless until when while
51
+ yield __FILE__ __LINE__ __ENCODING__].freeze
47
52
 
48
53
  def self.autocorrect_incompatible_with
49
54
  [ColonMethodCall]
@@ -131,7 +136,7 @@ module RuboCop
131
136
 
132
137
  def regular_method_call?(node)
133
138
  !(node.operator_method? ||
134
- keyword?(node.method_name) ||
139
+ KEYWORDS.include?(node.method_name) ||
135
140
  node.camel_case_method? ||
136
141
  node.setter_method? ||
137
142
  node.implicit_call?)
@@ -142,14 +147,6 @@ module RuboCop
142
147
  @local_variables_scopes[node] << name
143
148
  end
144
149
 
145
- def keyword?(method_name)
146
- %i[alias and begin break case class def defined? do
147
- else elsif end ensure false for if in module
148
- next nil not or redo rescue retry return self
149
- super then true undef unless until when while
150
- yield __FILE__ __LINE__ __ENCODING__].include?(method_name)
151
- end
152
-
153
150
  def allow_self(node)
154
151
  return unless node.send_type? && node.self_receiver?
155
152
 
@@ -92,7 +92,7 @@ module RuboCop
92
92
  second.int_type? ? second.to_a.first : :unknown
93
93
  end
94
94
 
95
- def range_size(range_node)
95
+ def range_size(range_node) # rubocop:todo Metrics/CyclomaticComplexity
96
96
  vals = range_node.to_a
97
97
  return :unknown unless vals.all?(&:int_type?)
98
98
 
@@ -39,7 +39,7 @@ module RuboCop
39
39
  check_for_line_terminator_or_opener
40
40
  end
41
41
 
42
- def on_begin(node)
42
+ def on_begin(node) # rubocop:todo Metrics/CyclomaticComplexity
43
43
  return if cop_config['AllowAsExpressionSeparator']
44
44
 
45
45
  exprs = node.children
@@ -20,6 +20,8 @@ module RuboCop
20
20
  # end
21
21
  # end
22
22
  class StructInheritance < Cop
23
+ include RangeHelp
24
+
23
25
  MSG = "Don't extend an instance initialized by `Struct.new`. " \
24
26
  'Use a block to customize the struct.'
25
27
 
@@ -29,10 +31,29 @@ module RuboCop
29
31
  add_offense(node, location: node.parent_class.source_range)
30
32
  end
31
33
 
34
+ def autocorrect(node)
35
+ lambda do |corrector|
36
+ corrector.remove(range_with_surrounding_space(range: node.loc.keyword))
37
+ corrector.replace(node.loc.operator, '=')
38
+
39
+ correct_parent(node.parent_class, corrector)
40
+ end
41
+ end
42
+
32
43
  def_node_matcher :struct_constructor?, <<~PATTERN
33
44
  {(send (const nil? :Struct) :new ...)
34
45
  (block (send (const nil? :Struct) :new ...) ...)}
35
46
  PATTERN
47
+
48
+ private
49
+
50
+ def correct_parent(parent, corrector)
51
+ if parent.block_type?
52
+ corrector.remove(range_with_surrounding_space(range: parent.loc.end, newlines: false))
53
+ else
54
+ corrector.insert_after(parent.loc.expression, ' do')
55
+ end
56
+ end
36
57
  end
37
58
  end
38
59
  end
@@ -65,7 +65,7 @@ module RuboCop
65
65
  def symbols_contain_spaces?(node)
66
66
  node.children.any? do |sym|
67
67
  content, = *sym
68
- content =~ / /
68
+ / /.match?(content)
69
69
  end
70
70
  end
71
71
 
@@ -104,12 +104,12 @@ module RuboCop
104
104
  )
105
105
 
106
106
  # method name
107
- string =~ /\A[a-zA-Z_]\w*[!?]?\z/ ||
107
+ /\A[a-zA-Z_]\w*[!?]?\z/.match?(string) ||
108
108
  # instance / class variable
109
- string =~ /\A@@?[a-zA-Z_]\w*\z/ ||
109
+ /\A@@?[a-zA-Z_]\w*\z/.match?(string) ||
110
110
  # global variable
111
- string =~ /\A\$[1-9]\d*\z/ ||
112
- string =~ /\A\$[a-zA-Z_]\w*\z/ ||
111
+ /\A\$[1-9]\d*\z/.match?(string) ||
112
+ /\A\$[a-zA-Z_]\w*\z/.match?(string) ||
113
113
  special_gvars.include?(string) ||
114
114
  redefinable_operators.include?(string)
115
115
  end
@@ -174,7 +174,7 @@ module RuboCop
174
174
  end
175
175
 
176
176
  def unparenthesized_method_call?(child)
177
- method_name(child) =~ /^[a-z]/i && !child.parenthesized?
177
+ /^[a-z]/i.match?(method_name(child)) && !child.parenthesized?
178
178
  end
179
179
 
180
180
  def below_ternary_precedence?(child)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  strings.any? do |s|
74
74
  string = s.str_content.dup.force_encoding(::Encoding::UTF_8)
75
75
  !string.valid_encoding? ||
76
- string !~ word_regex || string =~ / /
76
+ !word_regex.match?(string) || / /.match?(string)
77
77
  end
78
78
  end
79
79
 
@@ -14,7 +14,7 @@ module RuboCop
14
14
  module_function
15
15
 
16
16
  def comment_line?(line_source)
17
- line_source =~ /^\s*#/
17
+ /^\s*#/.match?(line_source)
18
18
  end
19
19
 
20
20
  def comment_lines?(node)
@@ -88,7 +88,7 @@ module RuboCop
88
88
 
89
89
  # Regex matches IF there is a ' or there is a \\ in the string that is
90
90
  # not preceded/followed by another \\ (e.g. "\\x34") but not "\\\\".
91
- string =~ /'|(?<! \\) \\{2}* \\ (?![\\"])/x
91
+ /'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string)
92
92
  end
93
93
 
94
94
  def needs_escaping?(string)
@@ -120,7 +120,7 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def mixed_formats?
123
- formats = format_sequences.map do |seq|
123
+ formats = format_sequences.reject(&:percent?).map do |seq|
124
124
  if seq.name
125
125
  :named
126
126
  elsif seq.max_digit_dollar_num