rubocop 0.93.1 → 1.3.1
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.
- checksums.yaml +4 -4
- data/README.md +36 -16
- data/config/default.yml +207 -72
- data/exe/rubocop +1 -1
- data/lib/rubocop.rb +16 -2
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
- data/lib/rubocop/cli/command/version.rb +1 -1
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_loader.rb +33 -7
- data/lib/rubocop/config_loader_resolver.rb +7 -5
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/badge.rb +9 -24
- data/lib/rubocop/cop/base.rb +16 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/commissioner.rb +37 -23
- data/lib/rubocop/cop/corrector.rb +3 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/force.rb +1 -1
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +2 -9
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
- data/lib/rubocop/cop/layout/class_structure.rb +22 -3
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/line_length.rb +8 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
- data/lib/rubocop/cop/lint/debugger.rb +17 -28
- data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
- data/lib/rubocop/cop/lint/else_layout.rb +29 -3
- data/lib/rubocop/cop/lint/empty_block.rb +82 -0
- data/lib/rubocop/cop/lint/empty_class.rb +93 -0
- data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +38 -6
- data/lib/rubocop/cop/lint/loop.rb +4 -4
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
- data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +86 -0
- data/lib/rubocop/cop/lint/to_json.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +185 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +12 -2
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
- data/lib/rubocop/cop/naming/variable_number.rb +98 -8
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/security/open.rb +12 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/and_or.rb +1 -3
- data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
- data/lib/rubocop/cop/style/case_like_if.rb +0 -4
- data/lib/rubocop/cop/style/collection_compact.rb +91 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
- data/lib/rubocop/cop/style/double_negation.rb +6 -1
- data/lib/rubocop/cop/style/format_string_token.rb +47 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +7 -3
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -11
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
- data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
- data/lib/rubocop/cop/style/raise_args.rb +21 -6
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +3 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
- data/lib/rubocop/cop/style/semicolon.rb +3 -0
- data/lib/rubocop/cop/style/static_class.rb +97 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +13 -1
- data/lib/rubocop/cop/style/swap_values.rb +108 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
- data/lib/rubocop/cop/team.rb +6 -1
- data/lib/rubocop/cop/util.rb +5 -1
- data/lib/rubocop/ext/regexp_node.rb +17 -9
- data/lib/rubocop/ext/regexp_parser.rb +84 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +2 -2
- data/lib/rubocop/options.rb +6 -1
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +65 -1
- data/lib/rubocop/version.rb +56 -6
- metadata +22 -6
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# puts $1 # => foo
|
20
20
|
#
|
21
21
|
class OutOfRangeRegexpRef < Base
|
22
|
-
MSG = '
|
22
|
+
MSG = '$%<backref>s is out of range (%<count>s regexp capture %<group>s detected).'
|
23
23
|
|
24
24
|
REGEXP_RECEIVER_METHODS = %i[=~ === match].to_set.freeze
|
25
25
|
REGEXP_ARGUMENT_METHODS = %i[=~ match grep gsub gsub! sub sub! [] slice slice! index rindex
|
@@ -35,14 +35,13 @@ module RuboCop
|
|
35
35
|
check_regexp(node.children.first)
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
38
|
+
def after_send(node)
|
39
39
|
@valid_ref = nil
|
40
40
|
|
41
|
-
if node
|
42
|
-
check_regexp(node.receiver)
|
43
|
-
elsif node.first_argument&.regexp_type? \
|
44
|
-
&& REGEXP_ARGUMENT_METHODS.include?(node.method_name)
|
41
|
+
if regexp_first_argument?(node)
|
45
42
|
check_regexp(node.first_argument)
|
43
|
+
elsif regexp_receiver?(node)
|
44
|
+
check_regexp(node.receiver)
|
46
45
|
end
|
47
46
|
end
|
48
47
|
|
@@ -56,9 +55,16 @@ module RuboCop
|
|
56
55
|
|
57
56
|
def on_nth_ref(node)
|
58
57
|
backref, = *node
|
59
|
-
return if @valid_ref.nil?
|
58
|
+
return if @valid_ref.nil? || backref <= @valid_ref
|
59
|
+
|
60
|
+
message = format(
|
61
|
+
MSG,
|
62
|
+
backref: backref,
|
63
|
+
count: @valid_ref.zero? ? 'no' : @valid_ref,
|
64
|
+
group: @valid_ref == 1 ? 'group' : 'groups'
|
65
|
+
)
|
60
66
|
|
61
|
-
add_offense(node
|
67
|
+
add_offense(node, message: message)
|
62
68
|
end
|
63
69
|
|
64
70
|
private
|
@@ -73,6 +79,19 @@ module RuboCop
|
|
73
79
|
node.each_capture(named: false).count
|
74
80
|
end
|
75
81
|
end
|
82
|
+
|
83
|
+
def regexp_first_argument?(send_node)
|
84
|
+
send_node.first_argument&.regexp_type? \
|
85
|
+
&& REGEXP_ARGUMENT_METHODS.include?(send_node.method_name)
|
86
|
+
end
|
87
|
+
|
88
|
+
def regexp_receiver?(send_node)
|
89
|
+
send_node.receiver&.regexp_type?
|
90
|
+
end
|
91
|
+
|
92
|
+
def nth_ref_receiver?(send_node)
|
93
|
+
send_node.receiver&.nth_ref_type?
|
94
|
+
end
|
76
95
|
end
|
77
96
|
end
|
78
97
|
end
|
@@ -88,31 +88,34 @@ module RuboCop
|
|
88
88
|
begin_pos = reposition(source, begin_pos, -1)
|
89
89
|
end_pos = reposition(source, end_pos, 1)
|
90
90
|
|
91
|
-
|
92
|
-
if source[begin_pos - 1] == ','
|
93
|
-
:before
|
94
|
-
elsif source[end_pos] == ','
|
95
|
-
:after
|
96
|
-
else
|
97
|
-
:none
|
98
|
-
end
|
99
|
-
|
100
|
-
range_to_remove(begin_pos, end_pos, comma_pos, comment)
|
91
|
+
range_to_remove(begin_pos, end_pos, comment)
|
101
92
|
end
|
102
93
|
|
103
|
-
def range_to_remove(begin_pos, end_pos,
|
94
|
+
def range_to_remove(begin_pos, end_pos, comment)
|
104
95
|
start = comment_start(comment)
|
96
|
+
source = comment.loc.expression.source
|
105
97
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
range_between(start + begin_pos, start + end_pos + 1)
|
98
|
+
if source[begin_pos - 1] == ','
|
99
|
+
range_with_comma_before(start, begin_pos, end_pos)
|
100
|
+
elsif source[end_pos] == ','
|
101
|
+
range_with_comma_after(comment, start, begin_pos, end_pos)
|
111
102
|
else
|
112
103
|
range_between(start, comment.loc.expression.end_pos)
|
113
104
|
end
|
114
105
|
end
|
115
106
|
|
107
|
+
def range_with_comma_before(start, begin_pos, end_pos)
|
108
|
+
range_between(start + begin_pos - 1, start + end_pos)
|
109
|
+
end
|
110
|
+
|
111
|
+
# If the list of cops is comma-separated, but without a empty space after the comma,
|
112
|
+
# we should **not** remove the prepending empty space, thus begin_pos += 1
|
113
|
+
def range_with_comma_after(comment, start, begin_pos, end_pos)
|
114
|
+
begin_pos += 1 if comment.loc.expression.source[end_pos + 1] != ' '
|
115
|
+
|
116
|
+
range_between(start + begin_pos, start + end_pos + 1)
|
117
|
+
end
|
118
|
+
|
116
119
|
def all_or_name(name)
|
117
120
|
name == 'all' ? 'all cops' : name
|
118
121
|
end
|
@@ -140,11 +140,10 @@ module RuboCop
|
|
140
140
|
rescued_groups.each_cons(2).all? do |x, y|
|
141
141
|
if x.include?(Exception)
|
142
142
|
false
|
143
|
-
elsif y.include?(Exception)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
# `nil`s
|
143
|
+
elsif y.include?(Exception) ||
|
144
|
+
# consider sorted if a group is empty or only contains
|
145
|
+
# `nil`s
|
146
|
+
x.none? || y.none?
|
148
147
|
true
|
149
148
|
else
|
150
149
|
(x <=> y || 0) <= 0
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop ensures that `to_enum`/`enum_for`, called for the current method,
|
7
|
+
# has correct arguments.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# def foo(x, y = 1)
|
12
|
+
# return to_enum(__callee__, x) # `y` is missing
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# def foo(x, y = 1)
|
17
|
+
# return to_enum(__callee__, x, y)
|
18
|
+
# # alternatives to `__callee__` are `__method__` and `:foo`
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
class ToEnumArguments < Base
|
22
|
+
MSG = 'Ensure you correctly provided all the arguments.'
|
23
|
+
|
24
|
+
RESTRICT_ON_SEND = %i[to_enum enum_for].freeze
|
25
|
+
|
26
|
+
def_node_matcher :enum_conversion_call?, <<~PATTERN
|
27
|
+
(send {nil? self} {:to_enum :enum_for} $_ $...)
|
28
|
+
PATTERN
|
29
|
+
|
30
|
+
def_node_matcher :method_name?, <<~PATTERN
|
31
|
+
{(send nil? {:__method__ :__callee__}) (sym %1)}
|
32
|
+
PATTERN
|
33
|
+
|
34
|
+
def_node_matcher :passing_keyword_arg?, <<~PATTERN
|
35
|
+
(pair (sym %1) (lvar %1))
|
36
|
+
PATTERN
|
37
|
+
|
38
|
+
def on_send(node)
|
39
|
+
def_node = node.each_ancestor(:def, :defs).first
|
40
|
+
return unless def_node
|
41
|
+
|
42
|
+
enum_conversion_call?(node) do |method_node, arguments|
|
43
|
+
add_offense(node) unless method_name?(method_node, def_node.method_name) &&
|
44
|
+
arguments_match?(arguments, def_node)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def arguments_match?(arguments, def_node)
|
51
|
+
index = 0
|
52
|
+
|
53
|
+
def_node.arguments.reject(&:blockarg_type?).all? do |def_arg|
|
54
|
+
send_arg = arguments[index]
|
55
|
+
case def_arg.type
|
56
|
+
when :arg, :restarg, :optarg
|
57
|
+
index += 1
|
58
|
+
end
|
59
|
+
|
60
|
+
send_arg && argument_match?(send_arg, def_arg)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
65
|
+
def argument_match?(send_arg, def_arg)
|
66
|
+
def_arg_name = def_arg.children[0]
|
67
|
+
|
68
|
+
case def_arg.type
|
69
|
+
when :arg, :restarg
|
70
|
+
send_arg.source == def_arg.source
|
71
|
+
when :optarg
|
72
|
+
send_arg.source == def_arg_name.to_s
|
73
|
+
when :kwoptarg, :kwarg
|
74
|
+
send_arg.hash_type? &&
|
75
|
+
send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
|
76
|
+
when :kwrestarg
|
77
|
+
send_arg.each_child_node(:kwsplat).any? { |child| child.source == def_arg.source }
|
78
|
+
when :forward_arg
|
79
|
+
send_arg.forwarded_args_type?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Looks for `reduce` or `inject` blocks where the value returned (implicitly or
|
7
|
+
# explicitly) does not include the accumulator. A block is considered valid as
|
8
|
+
# long as at least one return value includes the accumulator.
|
9
|
+
#
|
10
|
+
# If the accumulator is not included in the return value, then the entire
|
11
|
+
# block will just return a transformation of the last element value, and
|
12
|
+
# could be rewritten as such without a loop.
|
13
|
+
#
|
14
|
+
# Also catches instances where an index of the accumulator is returned, as
|
15
|
+
# this may change the type of object being retained.
|
16
|
+
#
|
17
|
+
# NOTE: For the purpose of reducing false positives, this cop only flags
|
18
|
+
# returns in `reduce` blocks where the element is the only variable in
|
19
|
+
# the expression (since we will not be able to tell what other variables
|
20
|
+
# relate to via static analysis).
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
#
|
24
|
+
# # bad
|
25
|
+
# (1..4).reduce(0) do |acc, el|
|
26
|
+
# el * 2
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # bad, may raise a NoMethodError after the first iteration
|
30
|
+
# %w(a b c).reduce({}) do |acc, letter|
|
31
|
+
# acc[letter] = true
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# (1..4).reduce(0) do |acc, el|
|
36
|
+
# acc + el * 2
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # good, element is returned but modified using the accumulator
|
40
|
+
# values.reduce do |acc, el|
|
41
|
+
# el << acc
|
42
|
+
# el
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# # good, returns the accumulator instead of the index
|
46
|
+
# %w(a b c).reduce({}) do |acc, letter|
|
47
|
+
# acc[letter] = true
|
48
|
+
# acc
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# # good, at least one branch returns the accumulator
|
52
|
+
# values.reduce(nil) do |result, value|
|
53
|
+
# break result if something?
|
54
|
+
# value
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# # ignored as the return value cannot be determined
|
58
|
+
# enum.reduce do |acc, el|
|
59
|
+
# x = foo(acc, el)
|
60
|
+
# bar(x)
|
61
|
+
# end
|
62
|
+
class UnmodifiedReduceAccumulator < Base
|
63
|
+
MSG = 'Ensure the accumulator `%<accum>s` will be modified by `%<method>s`.'
|
64
|
+
MSG_INDEX = 'Do not return an element of the accumulator in `%<method>s`.'
|
65
|
+
|
66
|
+
def_node_matcher :reduce_with_block?, <<~PATTERN
|
67
|
+
(block (send _recv {:reduce :inject} ...) (args arg+) ...)
|
68
|
+
PATTERN
|
69
|
+
|
70
|
+
def_node_matcher :accumulator_index?, <<~PATTERN
|
71
|
+
(send (lvar %1) {:[] :[]=} ...)
|
72
|
+
PATTERN
|
73
|
+
|
74
|
+
def_node_search :element_modified?, <<~PATTERN
|
75
|
+
{
|
76
|
+
(send _receiver !{:[] :[]=} <`(lvar %1) `_ ...>) # method(el, ...)
|
77
|
+
(send (lvar %1) _message <{ivar gvar cvar lvar send} ...>) # el.method(...)
|
78
|
+
(lvasgn %1 _) # el = ...
|
79
|
+
(%RuboCop::AST::Node::SHORTHAND_ASSIGNMENTS (lvasgn %1) ... _) # el += ...
|
80
|
+
}
|
81
|
+
PATTERN
|
82
|
+
|
83
|
+
def_node_matcher :lvar_used?, <<~PATTERN
|
84
|
+
{
|
85
|
+
(lvar %1)
|
86
|
+
(lvasgn %1 ...)
|
87
|
+
(send (lvar %1) :<< ...)
|
88
|
+
(dstr (begin (lvar %1)))
|
89
|
+
(%RuboCop::AST::Node::SHORTHAND_ASSIGNMENTS (lvasgn %1))
|
90
|
+
}
|
91
|
+
PATTERN
|
92
|
+
|
93
|
+
def_node_search :expression_values, <<~PATTERN
|
94
|
+
{
|
95
|
+
(%RuboCop::AST::Node::VARIABLES $_)
|
96
|
+
(%RuboCop::AST::Node::EQUALS_ASSIGNMENTS $_ ...)
|
97
|
+
(send (%RuboCop::AST::Node::VARIABLES $_) :<< ...)
|
98
|
+
$(send _ _)
|
99
|
+
(dstr (begin {(%RuboCop::AST::Node::VARIABLES $_)}))
|
100
|
+
(%RuboCop::AST::Node::SHORTHAND_ASSIGNMENTS (%RuboCop::AST::Node::EQUALS_ASSIGNMENTS $_) ...)
|
101
|
+
}
|
102
|
+
PATTERN
|
103
|
+
|
104
|
+
def on_block(node)
|
105
|
+
return unless reduce_with_block?(node)
|
106
|
+
|
107
|
+
check_return_values(node)
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
# Return values in a block are either the value given to next,
|
113
|
+
# the last line of a multiline block, or the only line of the block
|
114
|
+
def return_values(block_body_node)
|
115
|
+
nodes = [block_body_node.begin_type? ? block_body_node.child_nodes.last : block_body_node]
|
116
|
+
|
117
|
+
block_body_node.each_descendant(:next, :break) do |n|
|
118
|
+
# Ignore `next`/`break` inside an inner block
|
119
|
+
next if n.each_ancestor(:block).first != block_body_node.parent
|
120
|
+
next unless n.first_argument
|
121
|
+
|
122
|
+
nodes << n.first_argument
|
123
|
+
end
|
124
|
+
|
125
|
+
nodes
|
126
|
+
end
|
127
|
+
|
128
|
+
def check_return_values(block_node)
|
129
|
+
return_values = return_values(block_node.body)
|
130
|
+
accumulator_name = block_arg_name(block_node, 0)
|
131
|
+
element_name = block_arg_name(block_node, 1)
|
132
|
+
message_opts = { method: block_node.method_name, accum: accumulator_name }
|
133
|
+
|
134
|
+
if (node = returned_accumulator_index(return_values, accumulator_name))
|
135
|
+
add_offense(node, message: format(MSG_INDEX, message_opts))
|
136
|
+
elsif potential_offense?(return_values, block_node.body, element_name, accumulator_name)
|
137
|
+
return_values.each do |return_val|
|
138
|
+
unless acceptable_return?(return_val, element_name)
|
139
|
+
add_offense(return_val, message: format(MSG, message_opts))
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def block_arg_name(node, index)
|
146
|
+
node.arguments[index].node_parts[0]
|
147
|
+
end
|
148
|
+
|
149
|
+
# Look for an index of the accumulator being returned
|
150
|
+
# This is always an offense, in order to try to catch potential exceptions
|
151
|
+
# due to type mismatches
|
152
|
+
def returned_accumulator_index(return_values, accumulator_name)
|
153
|
+
return_values.detect { |val| accumulator_index?(val, accumulator_name) }
|
154
|
+
end
|
155
|
+
|
156
|
+
def potential_offense?(return_values, block_body, element_name, accumulator_name)
|
157
|
+
!(element_modified?(block_body, element_name) ||
|
158
|
+
returns_accumulator_anywhere?(return_values, accumulator_name))
|
159
|
+
end
|
160
|
+
|
161
|
+
# If the accumulator is used in any return value, the node is acceptable since
|
162
|
+
# the accumulator has a chance to change each iteration
|
163
|
+
def returns_accumulator_anywhere?(return_values, accumulator_name)
|
164
|
+
return_values.any? { |node| lvar_used?(node, accumulator_name) }
|
165
|
+
end
|
166
|
+
|
167
|
+
# Determine if a return value is acceptable for the purposes of this cop
|
168
|
+
# If it is an expression containing the accumulator, it is acceptable
|
169
|
+
# Otherwise, it is only unacceptable if it contains the iterated element, since we
|
170
|
+
# otherwise do not have enough information to prevent false positives.
|
171
|
+
def acceptable_return?(return_val, element_name)
|
172
|
+
vars = expression_values(return_val).uniq
|
173
|
+
return true if vars.none? || (vars - [element_name]).any?
|
174
|
+
|
175
|
+
false
|
176
|
+
end
|
177
|
+
|
178
|
+
# Exclude `begin` nodes inside a `dstr` from being collected by `return_values`
|
179
|
+
def allowed_type?(parent_node)
|
180
|
+
!parent_node.dstr_type?
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -255,7 +255,7 @@ module RuboCop
|
|
255
255
|
PATTERN
|
256
256
|
end
|
257
257
|
|
258
|
-
|
258
|
+
public_send(matcher_name, child)
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
@@ -279,7 +279,7 @@ module RuboCop
|
|
279
279
|
PATTERN
|
280
280
|
end
|
281
281
|
|
282
|
-
|
282
|
+
public_send(matcher_name, child)
|
283
283
|
end
|
284
284
|
end
|
285
285
|
end
|
@@ -54,11 +54,9 @@ module RuboCop
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def delegating?(node, def_node)
|
57
|
-
if node
|
58
|
-
false
|
59
|
-
elsif node.zsuper_type?
|
57
|
+
if node&.zsuper_type?
|
60
58
|
true
|
61
|
-
elsif node
|
59
|
+
elsif node&.super_type?
|
62
60
|
node.arguments.map(&:source) == def_node.arguments.map(&:source)
|
63
61
|
else
|
64
62
|
false
|