rubocop 1.68.0 → 1.69.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +41 -6
  4. data/lib/rubocop/cop/base.rb +1 -1
  5. data/lib/rubocop/cop/bundler/gem_filename.rb +0 -1
  6. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +0 -1
  7. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +1 -1
  8. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -2
  9. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +0 -2
  10. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +2 -4
  11. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/operator_keyword.rb +46 -0
  13. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +0 -2
  14. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  15. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -2
  16. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  17. data/lib/rubocop/cop/layout/begin_end_alignment.rb +0 -1
  18. data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
  19. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  20. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -3
  21. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -4
  22. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -1
  23. data/lib/rubocop/cop/layout/indentation_width.rb +7 -7
  24. data/lib/rubocop/cop/layout/leading_comment_space.rb +15 -0
  25. data/lib/rubocop/cop/layout/line_length.rb +118 -4
  26. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  27. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  28. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -3
  29. data/lib/rubocop/cop/layout/parameter_alignment.rb +3 -4
  30. data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -35
  31. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -2
  32. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  33. data/lib/rubocop/cop/layout/space_around_operators.rb +16 -17
  34. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +6 -0
  35. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +4 -0
  36. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +0 -1
  37. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +11 -12
  38. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -0
  39. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  40. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  41. data/lib/rubocop/cop/lint/empty_file.rb +0 -2
  42. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  43. data/lib/rubocop/cop/lint/float_comparison.rb +14 -6
  44. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -3
  45. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +55 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +9 -0
  47. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +3 -0
  48. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +1 -1
  49. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -5
  50. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  51. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +2 -2
  52. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +1 -1
  53. data/lib/rubocop/cop/lint/number_conversion.rb +0 -1
  54. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -2
  55. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +106 -0
  56. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -2
  57. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
  58. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  59. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +12 -7
  60. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +8 -7
  61. data/lib/rubocop/cop/lint/regexp_as_condition.rb +0 -1
  62. data/lib/rubocop/cop/lint/rescue_type.rb +3 -7
  63. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +2 -0
  64. data/lib/rubocop/cop/lint/self_assignment.rb +8 -10
  65. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  66. data/lib/rubocop/cop/lint/unused_method_argument.rb +18 -2
  67. data/lib/rubocop/cop/lint/useless_defined.rb +55 -0
  68. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  69. data/lib/rubocop/cop/lint/useless_setter_call.rb +14 -25
  70. data/lib/rubocop/cop/lint/void.rb +3 -2
  71. data/lib/rubocop/cop/metrics/class_length.rb +7 -7
  72. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  73. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -2
  74. data/lib/rubocop/cop/mixin/check_assignment.rb +4 -12
  75. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +49 -0
  76. data/lib/rubocop/cop/mixin/dig_help.rb +27 -0
  77. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +5 -9
  78. data/lib/rubocop/cop/mixin/range_help.rb +0 -1
  79. data/lib/rubocop/cop/mixin/target_ruby_version.rb +17 -1
  80. data/lib/rubocop/cop/naming/constant_name.rb +6 -7
  81. data/lib/rubocop/cop/naming/file_name.rb +0 -2
  82. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +11 -12
  83. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -11
  84. data/lib/rubocop/cop/naming/variable_name.rb +3 -4
  85. data/lib/rubocop/cop/naming/variable_number.rb +2 -3
  86. data/lib/rubocop/cop/style/access_modifier_declarations.rb +53 -24
  87. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +1 -1
  88. data/lib/rubocop/cop/style/array_intersect.rb +5 -4
  89. data/lib/rubocop/cop/style/bitwise_predicate.rb +1 -1
  90. data/lib/rubocop/cop/style/block_delimiters.rb +1 -1
  91. data/lib/rubocop/cop/style/case_like_if.rb +8 -11
  92. data/lib/rubocop/cop/style/commented_keyword.rb +11 -1
  93. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -21
  94. data/lib/rubocop/cop/style/constant_visibility.rb +3 -12
  95. data/lib/rubocop/cop/style/dig_chain.rb +90 -0
  96. data/lib/rubocop/cop/style/file_null.rb +73 -0
  97. data/lib/rubocop/cop/style/file_touch.rb +75 -0
  98. data/lib/rubocop/cop/style/for.rb +0 -1
  99. data/lib/rubocop/cop/style/global_vars.rb +1 -3
  100. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  101. data/lib/rubocop/cop/style/hash_conversion.rb +1 -2
  102. data/lib/rubocop/cop/style/if_inside_else.rb +0 -1
  103. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -2
  104. data/lib/rubocop/cop/style/if_with_semicolon.rb +14 -5
  105. data/lib/rubocop/cop/style/inverse_methods.rb +0 -1
  106. data/lib/rubocop/cop/style/keyword_arguments_merging.rb +2 -2
  107. data/lib/rubocop/cop/style/lambda_call.rb +0 -1
  108. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -1
  109. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -11
  110. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +33 -3
  111. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  112. data/lib/rubocop/cop/style/mutable_constant.rb +4 -5
  113. data/lib/rubocop/cop/style/negated_if_else_condition.rb +6 -4
  114. data/lib/rubocop/cop/style/nested_ternary_operator.rb +5 -4
  115. data/lib/rubocop/cop/style/not.rb +1 -1
  116. data/lib/rubocop/cop/style/one_line_conditional.rb +25 -4
  117. data/lib/rubocop/cop/style/operator_method_call.rb +5 -6
  118. data/lib/rubocop/cop/style/or_assignment.rb +3 -6
  119. data/lib/rubocop/cop/style/parallel_assignment.rb +8 -13
  120. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  121. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -1
  122. data/lib/rubocop/cop/style/redundant_condition.rb +36 -21
  123. data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -6
  124. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  125. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +1 -0
  126. data/lib/rubocop/cop/style/redundant_return.rb +2 -2
  127. data/lib/rubocop/cop/style/redundant_self.rb +7 -14
  128. data/lib/rubocop/cop/style/redundant_self_assignment.rb +7 -5
  129. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +4 -4
  130. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  131. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -3
  132. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  133. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -1
  134. data/lib/rubocop/cop/style/self_assignment.rb +11 -17
  135. data/lib/rubocop/cop/style/signal_exception.rb +2 -3
  136. data/lib/rubocop/cop/style/single_argument_dig.rb +9 -5
  137. data/lib/rubocop/cop/style/single_line_do_end_block.rb +13 -3
  138. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -3
  139. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  140. data/lib/rubocop/cop/style/string_concatenation.rb +0 -1
  141. data/lib/rubocop/cop/style/swap_values.rb +4 -15
  142. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -4
  143. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -2
  144. data/lib/rubocop/cop/variable_force.rb +4 -10
  145. data/lib/rubocop/cops_documentation_generator.rb +9 -1
  146. data/lib/rubocop/version.rb +1 -1
  147. data/lib/rubocop.rb +8 -0
  148. metadata +17 -8
@@ -125,15 +125,14 @@ module RuboCop
125
125
  MSG = 'Freeze mutable objects assigned to constants.'
126
126
 
127
127
  def on_casgn(node)
128
- _scope, _const_name, value = *node
129
- if value.nil? # This is only the case for `CONST += ...` or similarg66
128
+ if node.expression.nil? # This is only the case for `CONST += ...` or similarg66
130
129
  parent = node.parent
131
130
  return unless parent.or_asgn_type? # We only care about `CONST ||= ...`
132
131
 
133
- value = parent.children.last
132
+ on_assignment(parent.children.last)
133
+ else
134
+ on_assignment(node.expression)
134
135
  end
135
-
136
- on_assignment(value)
137
136
  end
138
137
 
139
138
  private
@@ -46,10 +46,12 @@ module RuboCop
46
46
  @corrected_nodes = nil
47
47
  end
48
48
 
49
+ # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
49
50
  def on_if(node)
50
51
  return unless if_else?(node)
51
52
  return unless (condition = unwrap_begin_nodes(node.condition))
52
53
  return if double_negation?(condition) || !negated_condition?(condition)
54
+ return unless condition.arguments.size < 2
53
55
 
54
56
  message = message(node)
55
57
  add_offense(node, message: message) do |corrector|
@@ -62,6 +64,7 @@ module RuboCop
62
64
  end
63
65
  end
64
66
  end
67
+ # rubocop:enable Metrics/AbcSize,Metrics/CyclomaticComplexity
65
68
 
66
69
  private
67
70
 
@@ -92,13 +95,12 @@ module RuboCop
92
95
  end
93
96
 
94
97
  def correct_negated_condition(corrector, node)
95
- receiver, method_name, rhs = *node
96
98
  replacement =
97
99
  if node.negation_method?
98
- receiver.source
100
+ node.receiver.source
99
101
  else
100
- inverted_method = method_name.to_s.sub('!', '=')
101
- "#{receiver.source} #{inverted_method} #{rhs.source}"
102
+ inverted_method = node.method_name.to_s.sub('!', '=')
103
+ "#{node.receiver.source} #{inverted_method} #{node.first_argument.source}"
102
104
  end
103
105
 
104
106
  corrector.replace(node, replacement)
@@ -18,7 +18,6 @@ module RuboCop
18
18
  class NestedTernaryOperator < Base
19
19
  extend AutoCorrector
20
20
  include RangeHelp
21
- include IgnoredNode
22
21
 
23
22
  MSG = 'Ternary operators must not be nested. Prefer `if` or `else` constructs instead.'
24
23
 
@@ -45,9 +44,11 @@ module RuboCop
45
44
  end
46
45
 
47
46
  def remove_parentheses(source)
48
- return source unless source.start_with?('(')
49
-
50
- source.delete_prefix('(').delete_suffix(')')
47
+ if source.start_with?('(') && source.end_with?(')')
48
+ source.delete_prefix('(').delete_suffix(')')
49
+ else
50
+ source
51
+ end
51
52
  end
52
53
 
53
54
  def replace_loc_and_whitespace(corrector, range, replacement)
@@ -52,7 +52,7 @@ module RuboCop
52
52
  end
53
53
 
54
54
  def requires_parens?(child)
55
- child.and_type? || child.or_type? ||
55
+ child.operator_keyword? ||
56
56
  (child.send_type? && child.binary_operation?) ||
57
57
  (child.if_type? && child.ternary?)
58
58
  end
@@ -4,8 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Checks for uses of if/then/else/end constructs on a single line.
7
- # AlwaysCorrectToMultiline config option can be set to true to auto-convert all offenses to
8
- # multi-line constructs. When AlwaysCorrectToMultiline is false (default case) the
7
+ # `AlwaysCorrectToMultiline` config option can be set to true to autocorrect all offenses to
8
+ # multi-line constructs. When `AlwaysCorrectToMultiline` is false (default case) the
9
9
  # autocorrect will first try converting them to ternary operators.
10
10
  #
11
11
  # @example
@@ -30,6 +30,25 @@ module RuboCop
30
30
  # else
31
31
  # baz
32
32
  # end
33
+ #
34
+ # @example AlwaysCorrectToMultiline: false (default)
35
+ # # bad
36
+ # if cond then run else dont end
37
+ #
38
+ # # good
39
+ # cond ? run : dont
40
+ #
41
+ # @example AlwaysCorrectToMultiline: true
42
+ # # bad
43
+ # if cond then run else dont end
44
+ #
45
+ # # good
46
+ # if cond
47
+ # run
48
+ # else
49
+ # dont
50
+ # end
51
+ #
33
52
  class OneLineConditional < Base
34
53
  include Alignment
35
54
  include ConfigurableEnforcedStyle
@@ -83,11 +102,13 @@ module RuboCop
83
102
  end
84
103
 
85
104
  def cannot_replace_to_ternary?(node)
86
- node.elsif_conditional?
105
+ return true if node.elsif_conditional?
106
+
107
+ node.else_branch.begin_type? && node.else_branch.children.compact.count >= 2
87
108
  end
88
109
 
89
110
  def ternary_replacement(node)
90
- condition, if_branch, else_branch = *node
111
+ condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
91
112
 
92
113
  "#{expr_replacement(condition)} ? " \
93
114
  "#{expr_replacement(if_branch)} : " \
@@ -31,10 +31,9 @@ module RuboCop
31
31
  return unless (dot = node.loc.dot)
32
32
  return if node.receiver.const_type? || !node.arguments.one?
33
33
 
34
- _lhs, _op, rhs = *node
35
- if !rhs || method_call_with_parenthesized_arg?(rhs) || invalid_syntax_argument?(rhs)
36
- return
37
- end
34
+ return unless (rhs = node.first_argument)
35
+ return if method_call_with_parenthesized_arg?(rhs)
36
+ return if invalid_syntax_argument?(rhs)
38
37
 
39
38
  add_offense(dot) do |corrector|
40
39
  wrap_in_parentheses_if_chained(corrector, node)
@@ -73,7 +72,7 @@ module RuboCop
73
72
  end
74
73
 
75
74
  def insert_space_after?(node)
76
- _lhs, op, rhs = *node
75
+ rhs = node.first_argument
77
76
  selector = node.loc.selector
78
77
 
79
78
  return true if selector.end_pos == rhs.source_range.begin_pos
@@ -82,7 +81,7 @@ module RuboCop
82
81
  # For `/` operations, if the RHS starts with a `(` without space,
83
82
  # add one to avoid a syntax error.
84
83
  range = selector.end.join(rhs.source_range.begin)
85
- return true if op == :/ && range.source == '('
84
+ return true if node.method?(:/) && range.source == '('
86
85
 
87
86
  false
88
87
  end
@@ -78,18 +78,15 @@ module RuboCop
78
78
  end
79
79
 
80
80
  def take_variable_and_default_from_ternary(node)
81
- variable, if_statement = *node
82
- [variable, if_statement.else_branch]
81
+ [node.name, node.expression.else_branch]
83
82
  end
84
83
 
85
84
  def take_variable_and_default_from_unless(node)
86
85
  if node.if_branch
87
- variable, default = *node.if_branch
86
+ [node.if_branch.name, node.if_branch.expression]
88
87
  else
89
- variable, default = *node.else_branch
88
+ [node.else_branch.name, node.else_branch.expression]
90
89
  end
91
-
92
- [variable, default]
93
90
  end
94
91
  end
95
92
  end
@@ -28,28 +28,25 @@ module RuboCop
28
28
 
29
29
  MSG = 'Do not use parallel assignment.'
30
30
 
31
- def on_masgn(node)
32
- lhs, rhs = *node
33
- lhs_elements = *lhs
31
+ def on_masgn(node) # rubocop:disable Metrics/AbcSize
32
+ rhs = node.rhs
34
33
  rhs = rhs.body if rhs.rescue_type?
35
34
  rhs_elements = Array(rhs).compact # edge case for one constant
36
35
 
37
- return if allowed_lhs?(lhs) || allowed_rhs?(rhs) ||
38
- allowed_masign?(lhs_elements, rhs_elements)
36
+ return if allowed_lhs?(node.assignments) || allowed_rhs?(rhs) ||
37
+ allowed_masign?(node.assignments, rhs_elements)
39
38
 
40
39
  range = node.source_range.begin.join(rhs.source_range.end)
41
40
 
42
41
  add_offense(range) do |corrector|
43
- autocorrect(corrector, node, lhs, rhs)
42
+ autocorrect(corrector, node, rhs)
44
43
  end
45
44
  end
46
45
 
47
46
  private
48
47
 
49
- def autocorrect(corrector, node, lhs, rhs)
50
- left_elements = *lhs
51
- right_elements = Array(rhs).compact
52
- order = find_valid_order(left_elements, right_elements)
48
+ def autocorrect(corrector, node, rhs)
49
+ order = find_valid_order(node.assignments, Array(rhs).compact)
53
50
  correction = assignment_corrector(node, rhs, order)
54
51
 
55
52
  corrector.replace(correction.correction_range, correction.correction)
@@ -61,9 +58,7 @@ module RuboCop
61
58
  add_self_to_getters(rhs_elements))
62
59
  end
63
60
 
64
- def allowed_lhs?(node)
65
- elements = *node
66
-
61
+ def allowed_lhs?(elements)
67
62
  # Account for edge cases using one variable with a comma
68
63
  # E.g.: `foo, = *bar`
69
64
  elements.one? || elements.any?(&:splat_type?)
@@ -150,7 +150,7 @@ module RuboCop
150
150
  end
151
151
 
152
152
  def requires_parens?(parent)
153
- parent.and_type? || parent.or_type? || (parent.if_type? && parent.ternary?)
153
+ parent.operator_keyword? || (parent.if_type? && parent.ternary?)
154
154
  end
155
155
  end
156
156
  end
@@ -93,7 +93,7 @@ module RuboCop
93
93
  end
94
94
 
95
95
  def check_ensure_node(node)
96
- check_branch(node.body)
96
+ check_branch(node.branch)
97
97
  end
98
98
 
99
99
  def check_begin_node(node)
@@ -5,6 +5,19 @@ module RuboCop
5
5
  module Style
6
6
  # Checks for unnecessary conditional expressions.
7
7
  #
8
+ # NOTE: Since the intention of the comment cannot be automatically determined,
9
+ # autocorrection is not applied when a comment is used, as shown below:
10
+ #
11
+ # [source,ruby]
12
+ # -----
13
+ # if b
14
+ # # Important note.
15
+ # b
16
+ # else
17
+ # c
18
+ # end
19
+ # -----
20
+ #
8
21
  # @example
9
22
  # # bad
10
23
  # a = b ? b : c
@@ -30,6 +43,7 @@ module RuboCop
30
43
  # end
31
44
  #
32
45
  class RedundantCondition < Base
46
+ include CommentsHelp
33
47
  include RangeHelp
34
48
  extend AutoCorrector
35
49
 
@@ -39,25 +53,15 @@ module RuboCop
39
53
  splat block_pass forwarded_restarg forwarded_kwrestarg forwarded_args
40
54
  ].freeze
41
55
 
42
- # rubocop:disable Metrics/AbcSize
43
56
  def on_if(node)
44
57
  return if node.modifier_form? || node.elsif_conditional? || !offense?(node)
45
58
 
46
59
  message = message(node)
47
60
 
48
61
  add_offense(range_of_offense(node), message: message) do |corrector|
49
- if node.ternary? && !branches_have_method?(node)
50
- correct_ternary(corrector, node)
51
- elsif redundant_condition?(node)
52
- corrector.replace(node, node.if_branch.source)
53
- else
54
- corrected = make_ternary_form(node)
55
-
56
- corrector.replace(node, corrected)
57
- end
62
+ autocorrect(corrector, node)
58
63
  end
59
64
  end
60
- # rubocop:enable Metrics/AbcSize
61
65
 
62
66
  private
63
67
 
@@ -69,6 +73,20 @@ module RuboCop
69
73
  end
70
74
  end
71
75
 
76
+ def autocorrect(corrector, node)
77
+ return if node.each_descendant.any? { |descendant| contains_comments?(descendant) }
78
+
79
+ if node.ternary? && !branches_have_method?(node)
80
+ correct_ternary(corrector, node)
81
+ elsif redundant_condition?(node)
82
+ corrector.replace(node, node.if_branch.source)
83
+ else
84
+ corrected = make_ternary_form(node)
85
+
86
+ corrector.replace(node, corrected)
87
+ end
88
+ end
89
+
72
90
  def range_of_offense(node)
73
91
  return node.source_range unless node.ternary?
74
92
  return node.source_range if node.ternary? && branches_have_method?(node)
@@ -77,8 +95,7 @@ module RuboCop
77
95
  end
78
96
 
79
97
  def offense?(node)
80
- _condition, _if_branch, else_branch = *node
81
-
98
+ _condition, _if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
82
99
  return false if use_if_branch?(else_branch) || use_hash_key_assignment?(else_branch)
83
100
 
84
101
  synonymous_condition_and_branch?(node) && !node.elsif? &&
@@ -102,7 +119,7 @@ module RuboCop
102
119
  end
103
120
 
104
121
  def synonymous_condition_and_branch?(node)
105
- condition, if_branch, _else_branch = *node
122
+ condition, if_branch, _else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
106
123
  # e.g.
107
124
  # if var
108
125
  # var
@@ -130,7 +147,7 @@ module RuboCop
130
147
  end
131
148
 
132
149
  def branches_have_assignment?(node)
133
- _condition, if_branch, else_branch = *node
150
+ _condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
134
151
 
135
152
  return false unless if_branch && else_branch
136
153
 
@@ -144,12 +161,10 @@ module RuboCop
144
161
  end
145
162
 
146
163
  def branches_have_method?(node)
147
- _condition, if_branch, else_branch = *node
148
-
149
- return false unless if_branch && else_branch
164
+ return false unless node.if_branch && node.else_branch
150
165
 
151
- single_argument_method?(if_branch) && single_argument_method?(else_branch) &&
152
- same_method?(if_branch, else_branch)
166
+ single_argument_method?(node.if_branch) && single_argument_method?(node.else_branch) &&
167
+ same_method?(node.if_branch, node.else_branch)
153
168
  end
154
169
 
155
170
  def single_argument_method?(node)
@@ -221,7 +236,7 @@ module RuboCop
221
236
  end
222
237
 
223
238
  def make_ternary_form(node)
224
- _condition, if_branch, else_branch = *node
239
+ _condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
225
240
  arithmetic_operation = use_arithmetic_operation?(if_branch)
226
241
 
227
242
  ternary_form = [
@@ -69,7 +69,7 @@ module RuboCop
69
69
  extend AutoCorrector
70
70
 
71
71
  MSG = 'Redundant line continuation.'
72
- LINE_CONTINUATION = "\\\n"
72
+ LINE_CONTINUATION = '\\'
73
73
  LINE_CONTINUATION_PATTERN = /(\\\n)/.freeze
74
74
  ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
75
75
  ARGUMENT_TYPES = %i[
@@ -90,7 +90,7 @@ module RuboCop
90
90
  end
91
91
  end
92
92
 
93
- inspect_eof_line_continuation
93
+ inspect_end_of_ruby_code_line_continuation
94
94
  end
95
95
 
96
96
  private
@@ -136,11 +136,12 @@ module RuboCop
136
136
  parse(source.gsub("\\\n", "\n")).valid_syntax?
137
137
  end
138
138
 
139
- def inspect_eof_line_continuation
140
- return unless processed_source.raw_source.end_with?(LINE_CONTINUATION)
139
+ def inspect_end_of_ruby_code_line_continuation
140
+ last_line = processed_source.lines[processed_source.ast.last_line - 1]
141
+ return unless last_line.end_with?(LINE_CONTINUATION)
141
142
 
142
- rindex = processed_source.raw_source.rindex(LINE_CONTINUATION)
143
- line_continuation_range = range_between(rindex, rindex + 1)
143
+ last_column = last_line.length
144
+ line_continuation_range = range_between(last_column - 1, last_column)
144
145
 
145
146
  add_offense(line_continuation_range) do |corrector|
146
147
  corrector.remove_trailing(line_continuation_range, 1)
@@ -147,7 +147,7 @@ module RuboCop
147
147
 
148
148
  return if begin_node.chained?
149
149
 
150
- if node.and_type? || node.or_type?
150
+ if node.operator_keyword?
151
151
  return if node.semantic_operator? && begin_node.parent
152
152
  return if node.multiline? && allow_in_multiline_conditions?
153
153
  return if ALLOWED_NODE_TYPES.include?(begin_node.parent&.type)
@@ -71,6 +71,7 @@ module RuboCop
71
71
 
72
72
  if new_argument.include?('"')
73
73
  new_argument.gsub!("'", "\\\\'")
74
+ new_argument.gsub!('\"', '"')
74
75
  quote = "'"
75
76
  elsif new_argument.include?('\\')
76
77
  quote = '"'
@@ -61,9 +61,9 @@ module RuboCop
61
61
  RESTRICT_ON_SEND = %i[define_method define_singleton_method lambda].freeze
62
62
 
63
63
  def on_send(node)
64
- return unless (parent = node.parent) && parent.block_type?
64
+ return unless node.block_literal?
65
65
 
66
- check_branch(parent.body)
66
+ check_branch(node.parent.body)
67
67
  end
68
68
 
69
69
  def on_def(node)
@@ -66,14 +66,12 @@ module RuboCop
66
66
  # Assignment of self.x
67
67
 
68
68
  def on_or_asgn(node)
69
- lhs, _rhs = *node
70
- allow_self(lhs)
69
+ allow_self(node.lhs)
71
70
  end
72
71
  alias on_and_asgn on_or_asgn
73
72
 
74
73
  def on_op_asgn(node)
75
- lhs, _op, _rhs = *node
76
- allow_self(lhs)
74
+ allow_self(node.lhs)
77
75
  end
78
76
 
79
77
  # Using self.x to distinguish from local variable x
@@ -92,13 +90,11 @@ module RuboCop
92
90
  end
93
91
 
94
92
  def on_masgn(node)
95
- lhs, rhs = *node
96
- add_masgn_lhs_variables(rhs, lhs)
93
+ add_masgn_lhs_variables(node.rhs, node.lhs)
97
94
  end
98
95
 
99
96
  def on_lvasgn(node)
100
- lhs, rhs = *node
101
- add_lhs_to_local_variables_scopes(rhs, lhs)
97
+ add_lhs_to_local_variables_scopes(node.rhs, node.lhs)
102
98
  end
103
99
 
104
100
  def on_in_pattern(node)
@@ -127,12 +123,10 @@ module RuboCop
127
123
  # Allow conditional nodes to use `self` in the condition if that variable
128
124
  # name is used in an `lvasgn` or `masgn` within the `if`.
129
125
  node.child_nodes.each do |child_node|
130
- lhs, _rhs = *child_node
131
-
132
126
  if child_node.lvasgn_type?
133
- add_lhs_to_local_variables_scopes(node.condition, lhs)
127
+ add_lhs_to_local_variables_scopes(node.condition, child_node.lhs)
134
128
  elsif child_node.masgn_type?
135
- add_masgn_lhs_variables(node.condition, lhs)
129
+ add_masgn_lhs_variables(node.condition, child_node.lhs)
136
130
  end
137
131
  end
138
132
  end
@@ -182,8 +176,7 @@ module RuboCop
182
176
  if node.mlhs_type?
183
177
  on_args(node)
184
178
  else
185
- name, = *node
186
- @local_variables_scopes[node] << name
179
+ @local_variables_scopes[node] << node.name
187
180
  end
188
181
  end
189
182
 
@@ -49,19 +49,21 @@ module RuboCop
49
49
  gvasgn: :gvar
50
50
  }.freeze
51
51
 
52
+ # rubocop:disable Metrics/AbcSize
52
53
  def on_lvasgn(node)
53
- lhs, rhs = *node
54
- receiver, method_name, = *rhs
55
- return unless receiver && method_returning_self?(method_name)
54
+ return unless (rhs = node.rhs)
55
+ return unless rhs.send_type? && method_returning_self?(rhs.method_name)
56
+ return unless (receiver = rhs.receiver)
56
57
 
57
58
  receiver_type = ASSIGNMENT_TYPE_TO_RECEIVER_TYPE[node.type]
58
- return unless receiver.type == receiver_type && receiver.children.first == lhs
59
+ return unless receiver.type == receiver_type && receiver.children.first == node.lhs
59
60
 
60
- message = format(MSG, method_name: method_name)
61
+ message = format(MSG, method_name: rhs.method_name)
61
62
  add_offense(node.loc.operator, message: message) do |corrector|
62
63
  corrector.replace(node, rhs.source)
63
64
  end
64
65
  end
66
+ # rubocop:enable Metrics/AbcSize
65
67
  alias on_ivasgn on_lvasgn
66
68
  alias on_cvasgn on_lvasgn
67
69
  alias on_gvasgn on_lvasgn
@@ -23,7 +23,6 @@ module RuboCop
23
23
  # foo = bar unless condition
24
24
  #
25
25
  class RedundantSelfAssignmentBranch < Base
26
- include RangeHelp
27
26
  extend AutoCorrector
28
27
 
29
28
  MSG = 'Remove the self-assignment branch.'
@@ -34,16 +33,17 @@ module RuboCop
34
33
  PATTERN
35
34
 
36
35
  def on_lvasgn(node)
37
- variable, expression = *node
36
+ expression = node.expression
37
+
38
38
  return unless use_if_and_else_branch?(expression)
39
39
 
40
40
  if_branch = expression.if_branch
41
41
  else_branch = expression.else_branch
42
42
  return if inconvertible_to_modifier?(if_branch, else_branch)
43
43
 
44
- if self_assign?(variable, if_branch)
44
+ if self_assign?(node.name, if_branch)
45
45
  register_offense(expression, if_branch, else_branch, 'unless')
46
- elsif self_assign?(variable, else_branch)
46
+ elsif self_assign?(node.name, else_branch)
47
47
  register_offense(expression, else_branch, if_branch, 'if')
48
48
  end
49
49
  end
@@ -201,7 +201,7 @@ module RuboCop
201
201
  def with_logical_operator?(node)
202
202
  return false unless (parent = node.parent)
203
203
 
204
- parent.or_type? || parent.and_type?
204
+ parent.operator_keyword?
205
205
  end
206
206
  end
207
207
  end
@@ -68,8 +68,7 @@ module RuboCop
68
68
  end
69
69
 
70
70
  def correct_rescue_block(corrector, node, parenthesized)
71
- operation, rescue_modifier, = *node
72
- *_, rescue_args = *rescue_modifier
71
+ operation = node.body
73
72
 
74
73
  node_indentation, node_offset = indentation_and_offset(node, parenthesized)
75
74
 
@@ -78,7 +77,7 @@ module RuboCop
78
77
  corrector.insert_after(heredoc_end(operation) || operation, <<~RESCUE_CLAUSE.chop)
79
78
 
80
79
  #{node_offset}rescue
81
- #{node_indentation}#{rescue_args.source}
80
+ #{node_indentation}#{node.resbody_branches.first.body.source}
82
81
  #{node_offset}end
83
82
  RESCUE_CLAUSE
84
83
  end
@@ -312,7 +312,7 @@ module RuboCop
312
312
  end
313
313
 
314
314
  def chain_length(method_chain, method)
315
- method.each_ancestor(:send).inject(0) do |total, ancestor|
315
+ method.each_ancestor(:send, :csend).inject(0) do |total, ancestor|
316
316
  break total + 1 if ancestor == method_chain
317
317
 
318
318
  total + 1
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Looks for places where an subset of an Enumerable (array,
6
+ # Looks for places where a subset of an Enumerable (array,
7
7
  # range, set, etc.; see note below) is calculated based on a `Regexp`
8
8
  # match, and suggests `grep` or `grep_v` instead.
9
9
  #