rubocop 1.71.2 → 1.73.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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +55 -13
  4. data/config/internal_affairs.yml +20 -0
  5. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  6. data/lib/rubocop/comment_config.rb +1 -1
  7. data/lib/rubocop/config.rb +4 -0
  8. data/lib/rubocop/config_loader.rb +44 -9
  9. data/lib/rubocop/config_loader_resolver.rb +23 -9
  10. data/lib/rubocop/config_validator.rb +1 -1
  11. data/lib/rubocop/cop/internal_affairs/example_description.rb +7 -3
  12. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  13. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -1
  14. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
  15. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  16. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  17. data/lib/rubocop/cop/internal_affairs.rb +2 -16
  18. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  19. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +4 -4
  20. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  22. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +26 -1
  23. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  25. data/lib/rubocop/cop/layout/line_length.rb +3 -3
  26. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  27. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  28. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -14
  30. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  31. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  32. data/lib/rubocop/cop/lint/float_comparison.rb +1 -6
  33. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  34. data/lib/rubocop/cop/lint/literal_as_condition.rb +99 -9
  35. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
  36. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  37. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +252 -0
  38. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  39. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +80 -0
  40. data/lib/rubocop/cop/lint/void.rb +6 -0
  41. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  42. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  43. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  44. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  45. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +18 -18
  46. data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
  47. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  48. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  49. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  50. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  51. data/lib/rubocop/cop/mixin/trailing_comma.rb +12 -0
  52. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  53. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  54. data/lib/rubocop/cop/naming/variable_name.rb +64 -6
  55. data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
  56. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  57. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  58. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  59. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  60. data/lib/rubocop/cop/style/inverse_methods.rb +8 -5
  61. data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
  62. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  63. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  64. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  65. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  66. data/lib/rubocop/cop/style/redundant_condition.rb +45 -0
  67. data/lib/rubocop/cop/style/redundant_format.rb +250 -0
  68. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  69. data/lib/rubocop/cop/style/redundant_parentheses.rb +18 -4
  70. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  71. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  72. data/lib/rubocop/cop/style/sole_nested_conditional.rb +0 -6
  73. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  74. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  75. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  76. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  77. data/lib/rubocop/cop/util.rb +1 -1
  78. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  79. data/lib/rubocop/cops_documentation_generator.rb +12 -1
  80. data/lib/rubocop/directive_comment.rb +35 -2
  81. data/lib/rubocop/lsp/runtime.rb +2 -0
  82. data/lib/rubocop/lsp/server.rb +0 -2
  83. data/lib/rubocop/options.rb +26 -11
  84. data/lib/rubocop/path_util.rb +4 -0
  85. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  86. data/lib/rubocop/plugin/load_error.rb +26 -0
  87. data/lib/rubocop/plugin/loader.rb +100 -0
  88. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  89. data/lib/rubocop/plugin.rb +46 -0
  90. data/lib/rubocop/rake_task.rb +4 -1
  91. data/lib/rubocop/rspec/cop_helper.rb +9 -0
  92. data/lib/rubocop/rspec/shared_contexts.rb +15 -0
  93. data/lib/rubocop/rspec/support.rb +1 -0
  94. data/lib/rubocop/server/cache.rb +35 -2
  95. data/lib/rubocop/server/cli.rb +2 -2
  96. data/lib/rubocop/version.rb +17 -2
  97. data/lib/rubocop.rb +5 -1
  98. data/lib/ruby_lsp/rubocop/addon.rb +7 -10
  99. data/lib/ruby_lsp/rubocop/{wraps_built_in_lsp_runtime.rb → runtime_adapter.rb} +5 -8
  100. metadata +36 -10
  101. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -3,8 +3,15 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Naming
6
- # Makes sure that all variables use the configured style,
7
- # snake_case or camelCase, for their names.
6
+ # Checks that the configured style (snake_case or camelCase) is used for all variable names.
7
+ # This includes local variables, instance variables, class variables, method arguments
8
+ # (positional, keyword, rest or block), and block arguments.
9
+ #
10
+ # The cop can also be configured to forbid using specific names for variables, using
11
+ # `ForbiddenIdentifiers` or `ForbiddenPatterns`. In addition to the above, this applies
12
+ # to global variables as well.
13
+ #
14
+ # Method definitions and method calls are not affected by this cop.
8
15
  #
9
16
  # @example EnforcedStyle: snake_case (default)
10
17
  # # bad
@@ -26,7 +33,21 @@ module RuboCop
26
33
  #
27
34
  # @example AllowedPatterns: ['_v\d+\z']
28
35
  # # good (with EnforcedStyle: camelCase)
29
- # :release_v1
36
+ # release_v1 = true
37
+ #
38
+ # @example ForbiddenIdentifiers: ['fooBar']
39
+ # # bad (in all cases)
40
+ # fooBar = 1
41
+ # @fooBar = 1
42
+ # @@fooBar = 1
43
+ # $fooBar = 1
44
+ #
45
+ # @example ForbiddenPatterns: ['_v\d+\z']
46
+ # # bad (in all cases)
47
+ # release_v1 = true
48
+ # @release_v1 = true
49
+ # @@release_v1 = true
50
+ # $release_v1 = true
30
51
  #
31
52
  class VariableName < Base
32
53
  include AllowedIdentifiers
@@ -34,16 +55,21 @@ module RuboCop
34
55
  include AllowedPattern
35
56
 
36
57
  MSG = 'Use %<style>s for variable names.'
58
+ MSG_FORBIDDEN = '`%<identifier>s` is forbidden, use another name instead.'
37
59
 
38
60
  def valid_name?(node, name, given_style = style)
39
61
  super || matches_allowed_pattern?(name)
40
62
  end
41
63
 
42
64
  def on_lvasgn(node)
43
- return unless node.name
44
- return if allowed_identifier?(node.name)
65
+ return unless (name = node.name)
66
+ return if allowed_identifier?(name)
45
67
 
46
- check_name(node, node.name, node.loc.name)
68
+ if forbidden_name?(name)
69
+ register_forbidden_name(node)
70
+ else
71
+ check_name(node, name, node.loc.name)
72
+ end
47
73
  end
48
74
  alias on_ivasgn on_lvasgn
49
75
  alias on_cvasgn on_lvasgn
@@ -56,11 +82,43 @@ module RuboCop
56
82
  alias on_blockarg on_lvasgn
57
83
  alias on_lvar on_lvasgn
58
84
 
85
+ # Only forbidden names are checked for global variable assignment
86
+ def on_gvasgn(node)
87
+ return unless (name = node.name)
88
+ return unless forbidden_name?(name)
89
+
90
+ register_forbidden_name(node)
91
+ end
92
+
59
93
  private
60
94
 
61
95
  def message(style)
62
96
  format(MSG, style: style)
63
97
  end
98
+
99
+ def forbidden_identifiers
100
+ cop_config.fetch('ForbiddenIdentifiers', [])
101
+ end
102
+
103
+ def forbidden_patterns
104
+ cop_config.fetch('ForbiddenPatterns', [])
105
+ end
106
+
107
+ def matches_forbidden_pattern?(name)
108
+ forbidden_patterns.any? { |pattern| Regexp.new(pattern).match?(name) }
109
+ end
110
+
111
+ def forbidden_name?(name)
112
+ name = name.to_s.delete(SIGILS)
113
+
114
+ (forbidden_identifiers.any? && forbidden_identifiers.include?(name)) ||
115
+ (forbidden_patterns.any? && matches_forbidden_pattern?(name))
116
+ end
117
+
118
+ def register_forbidden_name(node)
119
+ message = format(MSG_FORBIDDEN, identifier: node.name)
120
+ add_offense(node.loc.name, message: message)
121
+ end
64
122
  end
65
123
  end
66
124
  end
@@ -139,12 +139,16 @@ module RuboCop
139
139
  style == :separated
140
140
  end
141
141
 
142
+ def groupable_sibling_accessor?(node, sibling)
143
+ sibling.attribute_accessor? &&
144
+ sibling.method?(node.method_name) &&
145
+ node_visibility(sibling) == node_visibility(node) &&
146
+ groupable_accessor?(sibling) && !previous_line_comment?(sibling)
147
+ end
148
+
142
149
  def groupable_sibling_accessors(send_node)
143
150
  send_node.parent.each_child_node(:send).select do |sibling|
144
- sibling.attribute_accessor? &&
145
- sibling.method?(send_node.method_name) &&
146
- node_visibility(sibling) == node_visibility(send_node) &&
147
- groupable_accessor?(sibling) && !previous_line_comment?(sibling)
151
+ groupable_sibling_accessor?(send_node, sibling)
148
152
  end
149
153
  end
150
154
 
@@ -155,13 +159,23 @@ module RuboCop
155
159
 
156
160
  def preferred_accessors(node)
157
161
  if grouped_style?
162
+ return if skip_for_grouping?(node)
163
+
158
164
  accessors = groupable_sibling_accessors(node)
159
- group_accessors(node, accessors) if node.loc == accessors.first.loc
165
+ if node.loc == accessors.first.loc || skip_for_grouping?(accessors.first)
166
+ group_accessors(node, accessors)
167
+ end
160
168
  else
161
169
  separate_accessors(node)
162
170
  end
163
171
  end
164
172
 
173
+ # Group after constants
174
+ def skip_for_grouping?(node)
175
+ node.right_siblings.any?(&:casgn_type?) &&
176
+ node.right_siblings.any? { |n| n.send_type? && groupable_sibling_accessor?(node, n) }
177
+ end
178
+
165
179
  def group_accessors(node, accessors)
166
180
  accessor_names = accessors.flat_map { |accessor| accessor.arguments.map(&:source) }.uniq
167
181
 
@@ -32,10 +32,10 @@ module RuboCop
32
32
  # This cop handles not only method forwarding but also forwarding to `super`.
33
33
  #
34
34
  # [NOTE]
35
- # --
35
+ # ====
36
36
  # Because of a bug in Ruby 3.3.0, when a block is referenced inside of another block,
37
37
  # no offense will be registered until Ruby 3.4:
38
-
38
+ #
39
39
  # [source,ruby]
40
40
  # ----
41
41
  # def foo(&block)
@@ -43,7 +43,7 @@ module RuboCop
43
43
  # block_method { bar(&block) }
44
44
  # end
45
45
  # ----
46
- # --
46
+ # ====
47
47
  #
48
48
  # @example
49
49
  # # bad
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  REGEXP = /(?<keyword>\S+).*#/.freeze
59
59
 
60
- SUBCLASS_DEFINITION = /\A\s*class\s+\w+\s*<\s*\w+/.freeze
60
+ SUBCLASS_DEFINITION = /\A\s*class\s+(\w|::)+\s*<\s*(\w|::)+/.freeze
61
61
  METHOD_DEFINITION = /\A\s*def\s/.freeze
62
62
 
63
63
  def on_new_investigation
@@ -5,8 +5,11 @@ module RuboCop
5
5
  module Style
6
6
  # Checks for endless methods.
7
7
  #
8
- # It can enforce either the use of endless methods definitions
9
- # for single-lined method bodies, or disallow endless methods.
8
+ # It can enforce endless method definitions whenever possible or with single line methods.
9
+ # It can also disallow multiline endless method definitions or all endless definitions.
10
+ #
11
+ # `require_single_line` style enforces endless method definitions for single line methods.
12
+ # `require_always` style enforces endless method definitions for single statement methods.
10
13
  #
11
14
  # Other method definition types are not considered by this cop.
12
15
  #
@@ -15,36 +18,116 @@ module RuboCop
15
18
  # * allow_single_line (default) - only single line endless method definitions are allowed.
16
19
  # * allow_always - all endless method definitions are allowed.
17
20
  # * disallow - all endless method definitions are disallowed.
21
+ # * require_single_line - endless method definitions are required for single line methods.
22
+ # * require_always - all endless method definitions are required.
18
23
  #
19
24
  # NOTE: Incorrect endless method definitions will always be
20
25
  # corrected to a multi-line definition.
21
26
  #
22
27
  # @example EnforcedStyle: allow_single_line (default)
28
+ # # bad, multi-line endless method
29
+ # def my_method = x.foo
30
+ # .bar
31
+ # .baz
32
+ #
23
33
  # # good
24
- # def my_method() = x
34
+ # def my_method
35
+ # x
36
+ # end
25
37
  #
26
- # # bad, multi-line endless method
27
- # def my_method() = x.foo
28
- # .bar
29
- # .baz
38
+ # # good
39
+ # def my_method = x
40
+ #
41
+ # # good
42
+ # def my_method
43
+ # x.foo
44
+ # .bar
45
+ # .baz
46
+ # end
30
47
  #
31
48
  # @example EnforcedStyle: allow_always
32
49
  # # good
33
- # def my_method() = x
50
+ # def my_method
51
+ # x
52
+ # end
34
53
  #
35
54
  # # good
36
- # def my_method() = x.foo
37
- # .bar
38
- # .baz
55
+ # def my_method = x
56
+ #
57
+ # # good
58
+ # def my_method = x.foo
59
+ # .bar
60
+ # .baz
61
+ #
62
+ # # good
63
+ # def my_method
64
+ # x.foo
65
+ # .bar
66
+ # .baz
67
+ # end
39
68
  #
40
69
  # @example EnforcedStyle: disallow
41
70
  # # bad
42
- # def my_method() = x
71
+ # def my_method = x
72
+ #
73
+ # # bad
74
+ # def my_method = x.foo
75
+ # .bar
76
+ # .baz
77
+ #
78
+ # # good
79
+ # def my_method
80
+ # x
81
+ # end
82
+ #
83
+ # # good
84
+ # def my_method
85
+ # x.foo
86
+ # .bar
87
+ # .baz
88
+ # end
89
+ #
90
+ # @example EnforcedStyle: require_single_line
91
+ # # bad
92
+ # def my_method
93
+ # x
94
+ # end
95
+ #
96
+ # # bad
97
+ # def my_method = x.foo
98
+ # .bar
99
+ # .baz
100
+ #
101
+ # # good
102
+ # def my_method = x
103
+ #
104
+ # # good
105
+ # def my_method
106
+ # x.foo
107
+ # .bar
108
+ # .baz
109
+ # end
110
+ #
111
+ # @example EnforcedStyle: require_always
112
+ # # bad
113
+ # def my_method
114
+ # x
115
+ # end
43
116
  #
44
117
  # # bad
45
- # def my_method() = x.foo
46
- # .bar
47
- # .baz
118
+ # def my_method
119
+ # x.foo
120
+ # .bar
121
+ # .baz
122
+ # end
123
+ #
124
+ # # good
125
+ # def my_method = x
126
+ #
127
+ # # good
128
+ # def my_method = x.foo
129
+ # .bar
130
+ # .baz
48
131
  #
49
132
  class EndlessMethod < Base
50
133
  include ConfigurableEnforcedStyle
@@ -57,12 +140,21 @@ module RuboCop
57
140
  CORRECTION_STYLES = %w[multiline single_line].freeze
58
141
  MSG = 'Avoid endless method definitions.'
59
142
  MSG_MULTI_LINE = 'Avoid endless method definitions with multiple lines.'
143
+ MSG_REQUIRE_SINGLE = 'Use endless method definitions for single line methods.'
144
+ MSG_REQUIRE_ALWAYS = 'Use endless method definitions.'
60
145
 
61
146
  def on_def(node)
62
- if style == :disallow
63
- handle_disallow_style(node)
64
- else
147
+ return if node.assignment_method?
148
+
149
+ case style
150
+ when :allow_single_line, :allow_always
65
151
  handle_allow_style(node)
152
+ when :disallow
153
+ handle_disallow_style(node)
154
+ when :require_single_line
155
+ handle_require_single_line_style(node)
156
+ when :require_always
157
+ handle_require_always_style(node)
66
158
  end
67
159
  end
68
160
 
@@ -77,11 +169,64 @@ module RuboCop
77
169
  end
78
170
  end
79
171
 
172
+ def handle_require_single_line_style(node)
173
+ if node.endless? && !node.single_line?
174
+ add_offense(node, message: MSG_MULTI_LINE) do |corrector|
175
+ correct_to_multiline(corrector, node)
176
+ end
177
+ elsif !node.endless? && can_be_made_endless?(node) && node.body.single_line?
178
+ return if too_long_when_made_endless?(node)
179
+
180
+ add_offense(node, message: MSG_REQUIRE_SINGLE) do |corrector|
181
+ corrector.replace(node, endless_replacement(node))
182
+ end
183
+ end
184
+ end
185
+
186
+ def handle_require_always_style(node)
187
+ return if node.endless? || !can_be_made_endless?(node)
188
+ return if too_long_when_made_endless?(node)
189
+
190
+ add_offense(node, message: MSG_REQUIRE_ALWAYS) do |corrector|
191
+ corrector.replace(node, endless_replacement(node))
192
+ end
193
+ end
194
+
80
195
  def handle_disallow_style(node)
81
196
  return unless node.endless?
82
197
 
83
198
  add_offense(node) { |corrector| correct_to_multiline(corrector, node) }
84
199
  end
200
+
201
+ def correct_to_multiline(corrector, node)
202
+ replacement = <<~RUBY.strip
203
+ def #{node.method_name}#{arguments(node)}
204
+ #{node.body.source}
205
+ end
206
+ RUBY
207
+
208
+ corrector.replace(node, replacement)
209
+ end
210
+
211
+ def endless_replacement(node)
212
+ <<~RUBY.strip
213
+ def #{node.method_name}#{arguments(node)} = #{node.body.source}
214
+ RUBY
215
+ end
216
+
217
+ def arguments(node, missing = '')
218
+ node.arguments.any? ? node.arguments.source : missing
219
+ end
220
+
221
+ def can_be_made_endless?(node)
222
+ node.body && !node.body.begin_type? && !node.body.kwbegin_type?
223
+ end
224
+
225
+ def too_long_when_made_endless?(node)
226
+ return false unless config.cop_enabled?('Layout/LineLength')
227
+
228
+ endless_replacement(node).length > config.for_cop('Layout/LineLength')['Max']
229
+ end
85
230
  end
86
231
  end
87
232
  end
@@ -137,11 +137,11 @@ module RuboCop
137
137
 
138
138
  case depth(stripped_current_path)
139
139
  when 0
140
- range = arguments_range(current_path)
140
+ range = arguments_range(current_path.parent)
141
141
 
142
142
  corrector.replace(range, '__FILE__')
143
143
  when 1
144
- range = arguments_range(current_path)
144
+ range = arguments_range(current_path.parent)
145
145
 
146
146
  corrector.replace(range, '__dir__')
147
147
  else
@@ -185,11 +185,6 @@ module RuboCop
185
185
  corrector.remove(node.loc.dot)
186
186
  corrector.remove(node.loc.selector)
187
187
  end
188
-
189
- def arguments_range(node)
190
- range_between(node.parent.first_argument.source_range.begin_pos,
191
- node.parent.last_argument.source_range.end_pos)
192
- end
193
188
  end
194
189
  end
195
190
  end
@@ -46,6 +46,7 @@ module RuboCop
46
46
 
47
47
  MSG = 'Use `%<inverse>s` instead of inverting `%<method>s`.'
48
48
  CLASS_COMPARISON_METHODS = %i[<= >= < >].freeze
49
+ SAFE_NAVIGATION_INCOMPATIBLE_METHODS = (CLASS_COMPARISON_METHODS + %i[any? none?]).freeze
49
50
  EQUALITY_METHODS = %i[== != =~ !~ <= >= < >].freeze
50
51
  NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
51
52
  CAMEL_CASE = /[A-Z]+[a-z]+/.freeze
@@ -77,7 +78,7 @@ module RuboCop
77
78
  def on_send(node)
78
79
  inverse_candidate?(node) do |method_call, lhs, method, rhs|
79
80
  return unless inverse_methods.key?(method)
80
- return if negated?(node) || relational_comparison_with_safe_navigation?(method_call)
81
+ return if negated?(node) || safe_navigation_incompatible?(method_call)
81
82
  return if part_of_ignored_node?(node)
82
83
  return if possible_class_hierarchy_check?(lhs, rhs, method)
83
84
 
@@ -154,10 +155,6 @@ module RuboCop
154
155
  node.parent.respond_to?(:method?) && node.parent.method?(:!)
155
156
  end
156
157
 
157
- def relational_comparison_with_safe_navigation?(node)
158
- node.csend_type? && CLASS_COMPARISON_METHODS.include?(node.method_name)
159
- end
160
-
161
158
  def not_to_receiver(node, method_call)
162
159
  node.loc.selector.begin.join(method_call.source_range.begin)
163
160
  end
@@ -166,6 +163,12 @@ module RuboCop
166
163
  method_call.source_range.end.join(node.source_range.end)
167
164
  end
168
165
 
166
+ def safe_navigation_incompatible?(node)
167
+ return false unless node.csend_type?
168
+
169
+ SAFE_NAVIGATION_INCOMPATIBLE_METHODS.include?(node.method_name)
170
+ end
171
+
169
172
  # When comparing classes, `!(Integer < Numeric)` is not the same as
170
173
  # `Integer > Numeric`.
171
174
  def possible_class_hierarchy_check?(lhs, rhs, method)
@@ -42,19 +42,25 @@ module RuboCop
42
42
  return if kwarg_nodes.empty?
43
43
 
44
44
  add_offense(node) do |corrector|
45
- if node.parent.find(&:kwoptarg_type?) == node
46
- corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
45
+ defining_node = node.each_ancestor(:def, :defs, :block).first
46
+ next if processed_source.contains_comment?(arguments_range(defining_node))
47
+ next unless node.parent.find(&:kwoptarg_type?) == node
47
48
 
48
- arguments = node.each_ancestor(:def, :defs, :block).first.arguments
49
- append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
50
-
51
- remove_kwargs(kwarg_nodes, corrector)
52
- end
49
+ autocorrect(corrector, node, defining_node, kwarg_nodes)
53
50
  end
54
51
  end
55
52
 
56
53
  private
57
54
 
55
+ def autocorrect(corrector, node, defining_node, kwarg_nodes)
56
+ corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
57
+
58
+ arguments = defining_node.arguments
59
+ append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
60
+
61
+ remove_kwargs(kwarg_nodes, corrector)
62
+ end
63
+
58
64
  def append_newline_to_last_kwoptarg(arguments, corrector)
59
65
  last_argument = arguments.last
60
66
  return if last_argument.type?(:kwrestarg, :blockarg)
@@ -36,7 +36,7 @@ module RuboCop
36
36
  include RangeHelp
37
37
  extend AutoCorrector
38
38
 
39
- MSG = 'Use `\\` instead of `+` or `<<` to concatenate those strings.'
39
+ MSG = 'Use `\\` instead of `%<operator>s` to concatenate multiline strings.'
40
40
  CONCAT_TOKEN_TYPES = %i[tPLUS tLSHFT].freeze
41
41
  SIMPLE_STRING_TOKEN_TYPE = :tSTRING
42
42
  COMPLEX_STRING_BEGIN_TOKEN = :tSTRING_BEG
@@ -61,14 +61,20 @@ module RuboCop
61
61
  successor = tokens[index + 2]
62
62
 
63
63
  return unless eligible_token_set?(predecessor, operator, successor)
64
-
65
64
  return if same_line?(operator, successor)
66
65
 
67
66
  next_successor = token_after_last_string(successor, index)
68
-
69
67
  return unless eligible_next_successor?(next_successor)
70
68
 
71
- add_offense(operator.pos) { |corrector| autocorrect(corrector, operator.pos) }
69
+ register_offense(operator)
70
+ end
71
+
72
+ def register_offense(operator)
73
+ message = format(MSG, operator: operator.text)
74
+
75
+ add_offense(operator.pos, message: message) do |corrector|
76
+ autocorrect(corrector, operator.pos)
77
+ end
72
78
  end
73
79
 
74
80
  def autocorrect(corrector, operator_range)
@@ -41,7 +41,7 @@ module RuboCop
41
41
  return if ignored_node?(node)
42
42
 
43
43
  return unless (receiver = node.receiver)
44
- return unless receiver.any_block_type? && receiver.loc.end.is?('end')
44
+ return unless receiver.any_block_type? && receiver.keywords?
45
45
 
46
46
  range = range_between(receiver.loc.end.begin_pos, node.source_range.end_pos)
47
47
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
  MSG = 'Avoid multi-line chains of blocks.'
29
29
 
30
30
  def on_block(node)
31
- node.send_node.each_node(:send, :csend) do |send_node|
31
+ node.send_node.each_node(:call) do |send_node|
32
32
  receiver = send_node.receiver
33
33
 
34
34
  next unless receiver&.any_block_type? && receiver.multiline?
@@ -50,7 +50,7 @@ module RuboCop
50
50
  corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
51
51
  end
52
52
 
53
- arguments_range = arguments_range(node)
53
+ arguments_range = range_with_surrounding_space(arguments_range(node), side: :left)
54
54
  # If the method name isn't on the same line as def, move it directly after def
55
55
  if arguments_range.first_line != opening_line(node)
56
56
  corrector.remove(node.loc.name)
@@ -66,14 +66,6 @@ module RuboCop
66
66
  processed_source[arguments.last_line - 1].strip
67
67
  end
68
68
 
69
- def arguments_range(node)
70
- range = range_between(
71
- node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
72
- )
73
-
74
- range_with_surrounding_space(range, side: :left)
75
- end
76
-
77
69
  def opening_line(node)
78
70
  node.first_line
79
71
  end
@@ -42,7 +42,28 @@ module RuboCop
42
42
  # c
43
43
  # end
44
44
  #
45
+ # # bad
46
+ # a.nil? ? true : a
47
+ #
48
+ # # good
49
+ # a.nil? || a
50
+ #
51
+ # # bad
52
+ # if a.nil?
53
+ # true
54
+ # else
55
+ # a
56
+ # end
57
+ #
58
+ # # good
59
+ # a.nil? || a
60
+ #
61
+ # @example AllowedMethods: ['nonzero?'] (default)
62
+ # # good
63
+ # num.nonzero? ? true : false
64
+ #
45
65
  class RedundantCondition < Base
66
+ include AllowedMethods
46
67
  include CommentsHelp
47
68
  include RangeHelp
48
69
  extend AutoCorrector
@@ -128,6 +149,16 @@ module RuboCop
128
149
  # end
129
150
  return true if condition == if_branch
130
151
 
152
+ # e.g.
153
+ # a.nil? ? true : a
154
+ # or
155
+ # if a.nil?
156
+ # true
157
+ # else
158
+ # a
159
+ # end
160
+ return true if if_branch_is_true_type_and_else_is_not?(node)
161
+
131
162
  # e.g.
132
163
  # if foo
133
164
  # @value = foo
@@ -146,6 +177,18 @@ module RuboCop
146
177
  !use_hash_key_access?(if_branch)
147
178
  end
148
179
 
180
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
181
+ def if_branch_is_true_type_and_else_is_not?(node)
182
+ return false unless node.ternary? || node.if?
183
+
184
+ cond = node.condition
185
+ return false unless cond.call_type?
186
+ return false if !cond.predicate_method? || allowed_method?(cond.method_name)
187
+
188
+ node.if_branch&.true_type? && node.else_branch && !node.else_branch.true_type?
189
+ end
190
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
191
+
149
192
  def branches_have_assignment?(node)
150
193
  _condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
151
194
 
@@ -194,6 +237,8 @@ module RuboCop
194
237
  argument_source = if_branch.first_argument.source
195
238
 
196
239
  "#{if_branch.receiver.source} #{if_branch.method_name} (#{argument_source}"
240
+ elsif if_branch.true_type?
241
+ if_branch.parent.condition.source
197
242
  else
198
243
  if_branch.source
199
244
  end