rubocop 1.75.4 → 1.75.8

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +2 -0
  3. data/lib/rubocop/cop/autocorrect_logic.rb +18 -10
  4. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +49 -5
  5. data/lib/rubocop/cop/layout/class_structure.rb +35 -0
  6. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +6 -2
  7. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  8. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  9. data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
  10. data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
  11. data/lib/rubocop/cop/layout/space_before_brackets.rb +6 -32
  12. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +5 -1
  13. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
  14. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
  15. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  16. data/lib/rubocop/cop/lint/duplicate_methods.rb +84 -2
  17. data/lib/rubocop/cop/lint/float_comparison.rb +27 -0
  18. data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
  19. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -0
  20. data/lib/rubocop/cop/lint/void.rb +2 -2
  21. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  22. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
  23. data/lib/rubocop/cop/mixin/trailing_comma.rb +6 -2
  24. data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -10
  25. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  26. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  27. data/lib/rubocop/cop/style/comparable_between.rb +3 -0
  28. data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
  29. data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
  30. data/lib/rubocop/cop/style/identical_conditional_branches.rb +3 -3
  31. data/lib/rubocop/cop/style/if_unless_modifier.rb +20 -0
  32. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
  33. data/lib/rubocop/cop/style/map_to_hash.rb +11 -0
  34. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -0
  35. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
  36. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  37. data/lib/rubocop/cop/style/redundant_format.rb +6 -1
  38. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  39. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  40. data/lib/rubocop/cop/style/string_concatenation.rb +1 -2
  41. data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
  42. data/lib/rubocop/cop/team.rb +1 -1
  43. data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
  44. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -0
  45. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  46. data/lib/rubocop/version.rb +1 -1
  47. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fc4cebf1565ee3ced4efc79eb1012c0750f797f0ba74f94620ff50ff1d7be8e
4
- data.tar.gz: b1e53688b07611ebec3ebc028022f71314960573cc421cfdd1b6c6b831008abf
3
+ metadata.gz: 31a9b16f521cb4dd8d22db58d5473cd527162b65934d8e210993f7c435a85d26
4
+ data.tar.gz: 57af5692e8b85aca41224e621a0ba6b54001944a6625edef169a78278066a532
5
5
  SHA512:
6
- metadata.gz: b124c47ddee3e947bbcda187086ae1a8bf45317702d6ab0bc43f7888b90c8d560c12f61ae6fc4890bf5fec29016d88678cbc08db84e9dad5ee3ec242c8e03884
7
- data.tar.gz: fcc14e85af8aac9186971fa21371743f29221c8e694c7cf17b44762d628c7246d720b3e241f006286a43f3523e6ab9e587e0c2f0aecb5d34fe849375ca7ad183
6
+ metadata.gz: ef0ab376b3993e9ad1961560911ed5b5e2abf069a2b9acd0187e3d31c03f853a520646f2c421813bc3c03aedd74004dcf057cbadecad0ea86acafbd6670c8e46
7
+ data.tar.gz: b76bd8b4d7492577429bab8abd02ebcf7f2d9d2f83066e81c48b060abcaad68a2660b064c1f5b0b9c42bc80cc78ce15cd6e10ace393cd636310055515af925db
data/config/default.yml CHANGED
@@ -3702,7 +3702,9 @@ Style/CommentedKeyword:
3702
3702
  Style/ComparableBetween:
3703
3703
  Description: 'Enforces the use of `Comparable#between?` instead of logical comparison.'
3704
3704
  Enabled: pending
3705
+ Safe: false
3705
3706
  VersionAdded: '1.74'
3707
+ VersionChanged: '1.75'
3706
3708
  StyleGuide: '#ranges-or-between'
3707
3709
 
3708
3710
  Style/ComparableClamp:
@@ -50,7 +50,7 @@ module RuboCop
50
50
 
51
51
  def disable_offense(offense_range)
52
52
  unbreakable_range = multiline_ranges(offense_range)&.find do |range|
53
- range_overlaps_offense?(offense_range, range)
53
+ eol_comment_would_be_inside_literal?(offense_range, range)
54
54
  end
55
55
 
56
56
  if unbreakable_range
@@ -75,18 +75,22 @@ module RuboCop
75
75
  end
76
76
 
77
77
  def disable_offense_with_eol_or_surround_comment(range)
78
- eol_comment = " # rubocop:todo #{cop_name}"
79
- needed_line_length = (range.source_line + eol_comment).length
80
-
81
- if needed_line_length <= max_line_length
82
- disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
83
- else
78
+ if line_with_eol_comment_too_long?(range)
84
79
  disable_offense_before_and_after(range_by_lines(range))
80
+ else
81
+ disable_offense_at_end_of_line(range_of_first_line(range))
85
82
  end
86
83
  end
87
84
 
88
- def range_overlaps_offense?(offense_range, range)
89
- offense_range.begin_pos > range.begin_pos && range.overlaps?(offense_range)
85
+ def eol_comment_would_be_inside_literal?(offense_range, literal_range)
86
+ return true if line_with_eol_comment_too_long?(offense_range)
87
+
88
+ offense_line = offense_range.line
89
+ offense_line >= literal_range.first_line && offense_line < literal_range.last_line
90
+ end
91
+
92
+ def line_with_eol_comment_too_long?(range)
93
+ (range.source_line + eol_comment).length > max_line_length
90
94
  end
91
95
 
92
96
  def surrounding_heredoc?(node)
@@ -132,10 +136,14 @@ module RuboCop
132
136
  config.for_cop('Layout/LineLength')['Max'] || 120
133
137
  end
134
138
 
135
- def disable_offense_at_end_of_line(range, eol_comment)
139
+ def disable_offense_at_end_of_line(range)
136
140
  Corrector.new(range).insert_after(range, eol_comment)
137
141
  end
138
142
 
143
+ def eol_comment
144
+ " # rubocop:todo #{cop_name}"
145
+ end
146
+
139
147
  def disable_offense_before_and_after(range_by_lines)
140
148
  range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
141
149
  leading_whitespace = range_by_lines.source_line[/^\s*/]
@@ -6,10 +6,11 @@ module RuboCop
6
6
  # An attribute assignment method calls should be listed only once
7
7
  # in a gemspec.
8
8
  #
9
- # Assigning to an attribute with the same name using `spec.foo =` will be
10
- # an unintended usage. On the other hand, duplication of methods such
11
- # as `spec.requirements`, `spec.add_runtime_dependency`, and others are
12
- # permitted because it is the intended use of appending values.
9
+ # Assigning to an attribute with the same name using `spec.foo =` or
10
+ # `spec.attribute#[]=` will be an unintended usage. On the other hand,
11
+ # duplication of methods such # as `spec.requirements`,
12
+ # `spec.add_runtime_dependency`, and others are permitted because it is
13
+ # the intended use of appending values.
13
14
  #
14
15
  # @example
15
16
  # # bad
@@ -34,6 +35,18 @@ module RuboCop
34
35
  # spec.add_dependency('parallel', '~> 1.10')
35
36
  # spec.add_dependency('parser', '>= 2.3.3.1', '< 3.0')
36
37
  # end
38
+ #
39
+ # # bad
40
+ # Gem::Specification.new do |spec|
41
+ # spec.metadata["key"] = "value"
42
+ # spec.metadata["key"] = "value"
43
+ # end
44
+ #
45
+ # # good
46
+ # Gem::Specification.new do |spec|
47
+ # spec.metadata["key"] = "value"
48
+ # end
49
+ #
37
50
  class DuplicatedAssignment < Base
38
51
  include RangeHelp
39
52
  include GemspecHelp
@@ -47,9 +60,26 @@ module RuboCop
47
60
  (lvar #match_block_variable_name?) _ ...)
48
61
  PATTERN
49
62
 
63
+ # @!method indexed_assignment_method_declarations(node)
64
+ def_node_search :indexed_assignment_method_declarations, <<~PATTERN
65
+ (send
66
+ (send (lvar #match_block_variable_name?) _)
67
+ :[]=
68
+ literal?
69
+ _
70
+ )
71
+ PATTERN
72
+
50
73
  def on_new_investigation
51
74
  return if processed_source.blank?
52
75
 
76
+ process_assignment_method_nodes
77
+ process_indexed_assignment_method_nodes
78
+ end
79
+
80
+ private
81
+
82
+ def process_assignment_method_nodes
53
83
  duplicated_assignment_method_nodes.each do |nodes|
54
84
  nodes[1..].each do |node|
55
85
  register_offense(node, node.method_name, nodes.first.first_line)
@@ -57,7 +87,14 @@ module RuboCop
57
87
  end
58
88
  end
59
89
 
60
- private
90
+ def process_indexed_assignment_method_nodes
91
+ duplicated_indexed_assignment_method_nodes.each do |nodes|
92
+ nodes[1..].each do |node|
93
+ assignment = "#{node.children.first.method_name}[#{node.first_argument.source}]="
94
+ register_offense(node, assignment, nodes.first.first_line)
95
+ end
96
+ end
97
+ end
61
98
 
62
99
  def match_block_variable_name?(receiver_name)
63
100
  gem_specification(processed_source.ast) do |block_variable_name|
@@ -73,6 +110,13 @@ module RuboCop
73
110
  .select { |nodes| nodes.size > 1 }
74
111
  end
75
112
 
113
+ def duplicated_indexed_assignment_method_nodes
114
+ indexed_assignment_method_declarations(processed_source.ast)
115
+ .group_by { |node| [node.children.first.method_name, node.first_argument] }
116
+ .values
117
+ .select { |nodes| nodes.size > 1 }
118
+ end
119
+
76
120
  def register_offense(node, assignment, line_of_first_occurrence)
77
121
  line_range = node.loc.column...node.loc.last_column
78
122
  offense_location = source_range(processed_source.buffer, node.first_line, line_range)
@@ -22,6 +22,11 @@ module RuboCop
22
22
  # * Private attribute macros (`attr_accessor`, `attr_writer`, `attr_reader`)
23
23
  # * Private instance methods
24
24
  #
25
+ # NOTE: Simply enabling the cop with `Enabled: true` will not use
26
+ # the example order shown below.
27
+ # To enforce the order of macros like `attr_reader`,
28
+ # you must define both `ExpectedOrder` *and* `Categories`.
29
+ #
25
30
  # You can configure the following order:
26
31
  #
27
32
  # [source,yaml]
@@ -68,6 +73,36 @@ module RuboCop
68
73
  # - extend
69
74
  # ----
70
75
  #
76
+ # If you only set `ExpectedOrder`
77
+ # without defining `Categories`,
78
+ # macros such as `attr_reader` or `has_many`
79
+ # will not be recognized as part of a category, and their order will not be validated.
80
+ # For example, the following will NOT raise any offenses, even if the order is incorrect:
81
+ #
82
+ # [source,yaml]
83
+ # ----
84
+ # Layout/ClassStructure:
85
+ # Enabled: true
86
+ # ExpectedOrder:
87
+ # - public_attribute_macros
88
+ # - initializer
89
+ # ----
90
+ #
91
+ # To make it work as expected, you must also specify `Categories` like this:
92
+ #
93
+ # [source,yaml]
94
+ # ----
95
+ # Layout/ClassStructure:
96
+ # ExpectedOrder:
97
+ # - public_attribute_macros
98
+ # - initializer
99
+ # Categories:
100
+ # attribute_macros:
101
+ # - attr_reader
102
+ # - attr_writer
103
+ # - attr_accessor
104
+ # ----
105
+ #
71
106
  # @safety
72
107
  # Autocorrection is unsafe because class methods and module inclusion
73
108
  # can behave differently, based on which methods or constants have
@@ -116,7 +116,7 @@ module RuboCop
116
116
  def allowed_only_before_style?(node)
117
117
  if node.special_modifier?
118
118
  return true if processed_source[node.last_line] == 'end'
119
- return false if next_line_empty?(node.last_line)
119
+ return false if next_line_empty_and_exists?(node.last_line)
120
120
  end
121
121
 
122
122
  previous_line_empty?(node.first_line)
@@ -129,7 +129,7 @@ module RuboCop
129
129
  when :around
130
130
  corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line)
131
131
  when :only_before
132
- if next_line_empty?(node.last_line)
132
+ if next_line_empty_and_exists?(node.last_line)
133
133
  range = next_empty_line_range(node)
134
134
 
135
135
  corrector.remove(range)
@@ -154,6 +154,10 @@ module RuboCop
154
154
  body_end?(last_send_line) || next_line.blank?
155
155
  end
156
156
 
157
+ def next_line_empty_and_exists?(last_send_line)
158
+ next_line_empty?(last_send_line) && last_send_line.next != processed_source.lines.size
159
+ end
160
+
157
161
  def empty_lines_around?(node)
158
162
  previous_line_empty?(node.first_line) && next_line_empty?(node.last_line)
159
163
  end
@@ -155,7 +155,7 @@ module RuboCop
155
155
  def on_send(node)
156
156
  return unless should_check?(node)
157
157
  return if same_line?(node, node.first_argument)
158
- return if style != :consistent && enforce_first_argument_with_fixed_indentation? &&
158
+ return if enforce_first_argument_with_fixed_indentation? &&
159
159
  !enable_layout_first_method_argument_line_break?
160
160
 
161
161
  indent = base_indentation(node) + configured_indentation_width
@@ -250,7 +250,7 @@ module RuboCop
250
250
  reset!
251
251
 
252
252
  alignment_for(first_pair).each do |alignment|
253
- delta = alignment.deltas_for_first_pair(first_pair, node)
253
+ delta = alignment.deltas_for_first_pair(first_pair)
254
254
  check_delta delta, node: first_pair, alignment: alignment
255
255
  end
256
256
 
@@ -58,6 +58,12 @@ module RuboCop
58
58
  # attr_reader :name #: String
59
59
  # attr_reader :age #: Integer?
60
60
  #
61
+ # #: (
62
+ # #| Integer,
63
+ # #| String
64
+ # #| ) -> void
65
+ # def foo; end
66
+ #
61
67
  # @example AllowRBSInlineAnnotation: true
62
68
  #
63
69
  # # good
@@ -67,6 +73,12 @@ module RuboCop
67
73
  # attr_reader :name #: String
68
74
  # attr_reader :age #: Integer?
69
75
  #
76
+ # #: (
77
+ # #| Integer,
78
+ # #| String
79
+ # #| ) -> void
80
+ # def foo; end
81
+ #
70
82
  # @example AllowSteepAnnotation: false (default)
71
83
  #
72
84
  # # bad
@@ -175,7 +187,7 @@ module RuboCop
175
187
  end
176
188
 
177
189
  def rbs_inline_annotation?(comment)
178
- allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]/)
190
+ allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]|#\|/)
179
191
  end
180
192
 
181
193
  def allow_steep_annotation?
@@ -23,6 +23,16 @@ module RuboCop
23
23
  def kind(token)
24
24
  'semicolon' if token.semicolon?
25
25
  end
26
+
27
+ def space_missing?(token1, token2)
28
+ super && !semicolon_sequence?(token1, token2)
29
+ end
30
+
31
+ private
32
+
33
+ def semicolon_sequence?(token, next_token)
34
+ token.semicolon? && next_token.semicolon?
35
+ end
26
36
  end
27
37
  end
28
38
  end
@@ -22,51 +22,25 @@ module RuboCop
22
22
  RESTRICT_ON_SEND = %i[[] []=].freeze
23
23
 
24
24
  def on_send(node)
25
- return unless (first_argument = node.first_argument)
26
-
27
- begin_pos = first_argument.source_range.begin_pos
28
- return unless (range = offense_range(node, begin_pos))
29
-
30
- register_offense(range)
31
- end
32
-
33
- private
34
-
35
- def offense_range(node, begin_pos)
36
25
  receiver_end_pos = node.receiver.source_range.end_pos
37
26
  selector_begin_pos = node.loc.selector.begin_pos
38
27
  return if receiver_end_pos >= selector_begin_pos
39
28
  return if dot_before_brackets?(node, receiver_end_pos, selector_begin_pos)
40
29
 
41
- if reference_variable_with_brackets?(node)
42
- range_between(receiver_end_pos, selector_begin_pos)
43
- elsif node.method?(:[]=)
44
- offense_range_for_assignment(node, begin_pos)
30
+ range = range_between(receiver_end_pos, selector_begin_pos)
31
+
32
+ add_offense(range) do |corrector|
33
+ corrector.remove(range)
45
34
  end
46
35
  end
47
36
 
37
+ private
38
+
48
39
  def dot_before_brackets?(node, receiver_end_pos, selector_begin_pos)
49
40
  return false unless node.loc.respond_to?(:dot) && (dot = node.loc.dot)
50
41
 
51
42
  dot.begin_pos == receiver_end_pos && dot.end_pos == selector_begin_pos
52
43
  end
53
-
54
- def offense_range_for_assignment(node, begin_pos)
55
- end_pos = node.receiver.source_range.end_pos
56
-
57
- return if begin_pos - end_pos == 1 ||
58
- (range = range_between(end_pos, begin_pos - 1)).source.start_with?('[')
59
-
60
- range
61
- end
62
-
63
- def register_offense(range)
64
- add_offense(range) { |corrector| corrector.remove(range) }
65
- end
66
-
67
- def reference_variable_with_brackets?(node)
68
- node.receiver&.variable? && node.method?(:[]) && node.arguments.size == 1
69
- end
70
44
  end
71
45
  end
72
46
  end
@@ -6,6 +6,8 @@ module RuboCop
6
6
  # Checks that brackets used for array literals have or don't have
7
7
  # surrounding space depending on configuration.
8
8
  #
9
+ # Array pattern matching is handled in the same way.
10
+ #
9
11
  # @example EnforcedStyle: no_space (default)
10
12
  # # The `no_space` style enforces that array literals have
11
13
  # # no surrounding space.
@@ -82,9 +84,10 @@ module RuboCop
82
84
  EMPTY_MSG = '%<command>s space inside empty array brackets.'
83
85
 
84
86
  def on_array(node)
85
- return unless node.square_brackets?
87
+ return if node.array_type? && !node.square_brackets?
86
88
 
87
89
  tokens, left, right = array_brackets(node)
90
+ return unless left && right
88
91
 
89
92
  if empty_brackets?(left, right, tokens: tokens)
90
93
  return empty_offenses(node, left, right, EMPTY_MSG)
@@ -95,6 +98,7 @@ module RuboCop
95
98
 
96
99
  issue_offenses(node, left, right, start_ok, end_ok)
97
100
  end
101
+ alias on_array_pattern on_array
98
102
 
99
103
  private
100
104
 
@@ -6,6 +6,8 @@ module RuboCop
6
6
  # Checks that braces used for hash literals have or don't have
7
7
  # surrounding space depending on configuration.
8
8
  #
9
+ # Hash pattern matching is handled in the same way.
10
+ #
9
11
  # @example EnforcedStyle: space (default)
10
12
  # # The `space` style enforces that hash literals have
11
13
  # # surrounding space.
@@ -87,6 +89,7 @@ module RuboCop
87
89
  check(tokens[-2], tokens[-1]) if tokens.size > 2
88
90
  check_whitespace_only_hash(node) if enforce_no_space_style_for_empty_braces?
89
91
  end
92
+ alias on_hash_pattern on_hash
90
93
 
91
94
  private
92
95
 
@@ -51,10 +51,9 @@ module RuboCop
51
51
  'in a regexp.'
52
52
 
53
53
  def on_interpolation(begin_node)
54
- final_node = begin_node.children.last
55
-
56
- return unless begin_node.parent.regexp_type?
54
+ return unless (final_node = begin_node.children.last)
57
55
  return unless final_node.array_type?
56
+ return unless begin_node.parent.regexp_type?
58
57
 
59
58
  if array_of_literal_values?(final_node)
60
59
  register_array_of_literal_values(begin_node, final_node)
@@ -43,7 +43,7 @@ module RuboCop
43
43
  dup: 'to_h',
44
44
  exists?: 'exist?',
45
45
  gethostbyaddr: 'Addrinfo#getnameinfo',
46
- gethostbyname: 'Addrinfo#getaddrinfo',
46
+ gethostbyname: 'Addrinfo.getaddrinfo',
47
47
  iterator?: 'block_given?'
48
48
  }.freeze
49
49
 
@@ -39,9 +39,52 @@ module RuboCop
39
39
  # end
40
40
  #
41
41
  # alias bar foo
42
+ #
43
+ # @example AllCops:ActiveSupportExtensionsEnabled: false (default)
44
+ #
45
+ # # good
46
+ # def foo
47
+ # 1
48
+ # end
49
+ #
50
+ # delegate :foo, to: :bar
51
+ #
52
+ # @example AllCops:ActiveSupportExtensionsEnabled: true
53
+ #
54
+ # # bad
55
+ # def foo
56
+ # 1
57
+ # end
58
+ #
59
+ # delegate :foo, to: :bar
60
+ #
61
+ # # good
62
+ # def foo
63
+ # 1
64
+ # end
65
+ #
66
+ # delegate :baz, to: :bar
67
+ #
68
+ # # good - delegate with splat arguments is ignored
69
+ # def foo
70
+ # 1
71
+ # end
72
+ #
73
+ # delegate :foo, **options
74
+ #
75
+ # # good - delegate inside a condition is ignored
76
+ # def foo
77
+ # 1
78
+ # end
79
+ #
80
+ # if cond
81
+ # delegate :foo, to: :bar
82
+ # end
83
+ #
42
84
  class DuplicateMethods < Base
43
85
  MSG = 'Method `%<method>s` is defined at both %<defined>s and %<current>s.'
44
- RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer attr_accessor attr].freeze
86
+ RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer attr_accessor attr
87
+ delegate].freeze
45
88
 
46
89
  def initialize(config = nil, options = nil)
47
90
  super
@@ -85,15 +128,28 @@ module RuboCop
85
128
  (send nil? :alias_method (sym $_name) _)
86
129
  PATTERN
87
130
 
131
+ # @!method delegate_method?(node)
132
+ def_node_matcher :delegate_method?, <<~PATTERN
133
+ (send nil? :delegate
134
+ ({sym str} $_)+
135
+ (hash <(pair (sym :to) {sym str}) ...>)
136
+ )
137
+ PATTERN
138
+
88
139
  # @!method sym_name(node)
89
140
  def_node_matcher :sym_name, '(sym $_name)'
90
- def on_send(node)
141
+
142
+ def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
91
143
  if (name = alias_method?(node))
92
144
  return if node.ancestors.any?(&:if_type?)
93
145
 
94
146
  found_instance_method(node, name)
95
147
  elsif (attr = node.attribute_accessor?)
96
148
  on_attr(node, *attr)
149
+ elsif active_support_extensions_enabled? && (names = delegate_method?(node))
150
+ return if node.ancestors.any?(&:if_type?)
151
+
152
+ on_delegate(node, names)
97
153
  end
98
154
  end
99
155
 
@@ -118,6 +174,32 @@ module RuboCop
118
174
  current: source_location(node))
119
175
  end
120
176
 
177
+ def on_delegate(node, method_names)
178
+ name_prefix = delegate_prefix(node)
179
+
180
+ method_names.each do |name|
181
+ name = "#{name_prefix}_#{name}" if name_prefix
182
+
183
+ found_instance_method(node, name)
184
+ end
185
+ end
186
+
187
+ def delegate_prefix(node)
188
+ kwargs_node = node.last_argument
189
+
190
+ return unless (prefix = hash_value(kwargs_node, :prefix))
191
+
192
+ if prefix.true_type?
193
+ hash_value(kwargs_node, :to).value
194
+ elsif prefix.type?(:sym, :str)
195
+ prefix.value
196
+ end
197
+ end
198
+
199
+ def hash_value(node, key)
200
+ node.pairs.find { |pair| pair.key.value == key }&.value
201
+ end
202
+
121
203
  def found_instance_method(node, name)
122
204
  return found_sclass_method(node, name) unless (scope = node.parent_module_name)
123
205
 
@@ -15,6 +15,14 @@ module RuboCop
15
15
  # x == 0.1
16
16
  # x != 0.1
17
17
  #
18
+ # # bad
19
+ # case value
20
+ # when 1.0
21
+ # foo
22
+ # when 2.0
23
+ # bar
24
+ # end
25
+ #
18
26
  # # good - using BigDecimal
19
27
  # x.to_d == 0.1.to_d
20
28
  #
@@ -32,12 +40,21 @@ module RuboCop
32
40
  # # good - comparing against nil
33
41
  # Float(x, exception: false) == nil
34
42
  #
43
+ # # good - using epsilon comparison in case expression
44
+ # case
45
+ # when (value - 1.0).abs < Float::EPSILON
46
+ # foo
47
+ # when (value - 2.0).abs < Float::EPSILON
48
+ # bar
49
+ # end
50
+ #
35
51
  # # Or some other epsilon based type of comparison:
36
52
  # # https://www.embeddeduse.com/2019/08/26/qt-compare-two-floats/
37
53
  #
38
54
  class FloatComparison < Base
39
55
  MSG_EQUALITY = 'Avoid equality comparisons of floats as they are unreliable.'
40
56
  MSG_INEQUALITY = 'Avoid inequality comparisons of floats as they are unreliable.'
57
+ MSG_CASE = 'Avoid float literal comparisons in case statements as they are unreliable.'
41
58
 
42
59
  EQUALITY_METHODS = %i[== != eql? equal?].freeze
43
60
  FLOAT_RETURNING_METHODS = %i[to_f Float fdiv].freeze
@@ -58,6 +75,16 @@ module RuboCop
58
75
  end
59
76
  alias on_csend on_send
60
77
 
78
+ def on_case(node)
79
+ node.when_branches.each do |when_branch|
80
+ when_branch.each_condition do |condition|
81
+ next if !float?(condition) || literal_safe?(condition)
82
+
83
+ add_offense(condition, message: MSG_CASE)
84
+ end
85
+ end
86
+ end
87
+
61
88
  private
62
89
 
63
90
  def float?(node)
@@ -248,7 +248,7 @@ module RuboCop
248
248
  add_offense(cond) do |corrector|
249
249
  corrector.replace(node, "else\n #{node.else_branch.source}")
250
250
  end
251
- elsif result
251
+ elsif node.if_branch && result
252
252
  add_offense(cond) do |corrector|
253
253
  corrector.replace(node, node.if_branch.source)
254
254
  end
@@ -104,6 +104,8 @@ module RuboCop
104
104
  end
105
105
 
106
106
  def chained_assignment?(node)
107
+ return true if node.lvasgn_type? && node.expression&.send_type?
108
+
107
109
  node.respond_to?(:expression) && node.expression&.lvasgn_type?
108
110
  end
109
111
 
@@ -128,8 +128,8 @@ module RuboCop
128
128
 
129
129
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
130
130
  def check_void_op(node, &block)
131
- node = node.children.first while node.begin_type?
132
- return unless node.call_type? && OPERATORS.include?(node.method_name)
131
+ node = node.children.first while node&.begin_type?
132
+ return unless node&.call_type? && OPERATORS.include?(node.method_name)
133
133
  if !UNARY_OPERATORS.include?(node.method_name) && node.loc.dot && node.arguments.none?
134
134
  return
135
135
  end
@@ -39,7 +39,7 @@ module RuboCop
39
39
  class AbcSize < Base
40
40
  include MethodComplexity
41
41
 
42
- MSG = 'Assignment Branch Condition size for %<method>s is too high. ' \
42
+ MSG = 'Assignment Branch Condition size for `%<method>s` is too high. ' \
43
43
  '[%<abc_vector>s %<complexity>.4g/%<max>.4g]'
44
44
 
45
45
  private