rubocop 1.39.0 → 1.44.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/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +149 -11
- data/exe/rubocop +1 -1
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/comment_config.rb +5 -0
- data/lib/rubocop/config.rb +39 -15
- data/lib/rubocop/config_loader.rb +26 -20
- data/lib/rubocop/config_loader_resolver.rb +6 -2
- data/lib/rubocop/config_validator.rb +1 -1
- data/lib/rubocop/cop/badge.rb +9 -4
- data/lib/rubocop/cop/base.rb +84 -74
- data/lib/rubocop/cop/commissioner.rb +8 -3
- data/lib/rubocop/cop/cop.rb +29 -29
- data/lib/rubocop/cop/corrector.rb +30 -10
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +22 -6
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
- data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
- data/lib/rubocop/cop/layout/class_structure.rb +32 -11
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
- data/lib/rubocop/cop/layout/empty_lines.rb +2 -0
- data/lib/rubocop/cop/layout/extra_spacing.rb +10 -6
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +38 -2
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +49 -2
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +61 -2
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +52 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
- data/lib/rubocop/cop/layout/indentation_style.rb +7 -2
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +5 -0
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +11 -5
- data/lib/rubocop/cop/layout/line_length.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +51 -2
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +49 -2
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +53 -2
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +58 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +0 -2
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +11 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +11 -1
- data/lib/rubocop/cop/lint/constant_resolution.rb +4 -0
- data/lib/rubocop/cop/lint/debugger.rb +3 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
- data/lib/rubocop/cop/lint/deprecated_constants.rb +8 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +0 -2
- data/lib/rubocop/cop/lint/duplicate_methods.rb +19 -8
- data/lib/rubocop/cop/lint/else_layout.rb +2 -6
- data/lib/rubocop/cop/lint/empty_block.rb +1 -5
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +11 -7
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
- data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +5 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +15 -3
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +10 -12
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +5 -4
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -3
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
- data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
- data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
- data/lib/rubocop/cop/lint/void.rb +25 -16
- data/lib/rubocop/cop/metrics/block_length.rb +9 -4
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +10 -5
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +9 -4
- data/lib/rubocop/cop/metrics/module_length.rb +10 -5
- data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -3
- data/lib/rubocop/cop/mixin/alignment.rb +2 -2
- data/lib/rubocop/cop/mixin/allowed_identifiers.rb +2 -2
- data/lib/rubocop/cop/mixin/annotation_comment.rb +13 -6
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +11 -7
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +59 -6
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -2
- data/lib/rubocop/cop/mixin/method_complexity.rb +5 -3
- data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -3
- data/lib/rubocop/cop/mixin/percent_array.rb +3 -5
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/require_library.rb +2 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +3 -3
- data/lib/rubocop/cop/mixin/statement_modifier.rb +16 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +5 -1
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +2 -0
- data/lib/rubocop/cop/naming/inclusive_language.rb +4 -1
- data/lib/rubocop/cop/registry.rb +63 -43
- data/lib/rubocop/cop/security/compound_hash.rb +2 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +18 -10
- data/lib/rubocop/cop/style/alias.rb +9 -1
- data/lib/rubocop/cop/style/array_intersect.rb +111 -0
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +2 -9
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +86 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
- data/lib/rubocop/cop/style/documentation.rb +11 -5
- data/lib/rubocop/cop/style/guard_clause.rb +44 -9
- data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +4 -4
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
- data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -1
- data/lib/rubocop/cop/style/map_to_set.rb +61 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
- data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
- data/lib/rubocop/cop/style/missing_else.rb +13 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
- data/lib/rubocop/cop/style/operator_method_call.rb +15 -1
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +3 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
- data/lib/rubocop/cop/style/redundant_constant_base.rb +85 -0
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +45 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
- data/lib/rubocop/cop/style/redundant_return.rb +7 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +6 -3
- data/lib/rubocop/cop/style/require_order.rb +135 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +35 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +13 -5
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +26 -3
- data/lib/rubocop/cop/style/signal_exception.rb +8 -6
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -5
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -4
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
- data/lib/rubocop/cop/style/word_array.rb +41 -0
- data/lib/rubocop/cop/style/yoda_expression.rb +81 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
- data/lib/rubocop/cop/team.rb +30 -30
- data/lib/rubocop/cop/util.rb +32 -5
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
- data/lib/rubocop/cop/variable_force.rb +18 -30
- data/lib/rubocop/cops_documentation_generator.rb +33 -11
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/file_patterns.rb +43 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter.rb +4 -1
- data/lib/rubocop/options.rb +8 -0
- data/lib/rubocop/path_util.rb +50 -22
- data/lib/rubocop/result_cache.rb +2 -2
- data/lib/rubocop/rspec/cop_helper.rb +4 -1
- data/lib/rubocop/rspec/expect_offense.rb +6 -4
- data/lib/rubocop/rspec/support.rb +2 -2
- data/lib/rubocop/runner.rb +10 -3
- data/lib/rubocop/server/cache.rb +3 -1
- data/lib/rubocop/server/core.rb +1 -1
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +23 -6
- metadata +23 -9
@@ -128,23 +128,35 @@ module RuboCop
|
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
131
132
|
def each_already_disabled(cop, line_ranges)
|
132
133
|
line_ranges.each_cons(2) do |previous_range, range|
|
133
134
|
next if ignore_offense?(range)
|
134
|
-
next unless followed_ranges?(previous_range, range)
|
135
|
-
|
136
135
|
# If a cop is disabled in a range that begins on the same line as
|
137
136
|
# the end of the previous range, it means that the cop was
|
138
137
|
# already disabled by an earlier comment. So it's redundant
|
139
138
|
# whether there are offenses or not.
|
139
|
+
next unless followed_ranges?(previous_range, range)
|
140
|
+
|
140
141
|
comment = processed_source.comment_at_line(range.begin)
|
141
142
|
|
143
|
+
next unless comment
|
142
144
|
# Comments disabling all cops don't count since it's reasonable
|
143
145
|
# to disable a few select cops first and then all cops further
|
144
146
|
# down in the code.
|
145
|
-
|
147
|
+
next if all_disabled?(comment)
|
148
|
+
|
149
|
+
redundant =
|
150
|
+
if department_disabled?(cop, comment)
|
151
|
+
find_redundant_department(cop, range)
|
152
|
+
else
|
153
|
+
cop
|
154
|
+
end
|
155
|
+
|
156
|
+
yield comment, redundant if redundant
|
146
157
|
end
|
147
158
|
end
|
159
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
148
160
|
|
149
161
|
def find_redundant_cop(cop, range)
|
150
162
|
cop_offenses = offenses_to_check.select { |offense| offense.cop_name == cop }
|
@@ -42,7 +42,7 @@ module RuboCop
|
|
42
42
|
MSG = 'Unnecessary enabling of %<cop>s.'
|
43
43
|
|
44
44
|
def on_new_investigation
|
45
|
-
return if processed_source.blank?
|
45
|
+
return if processed_source.blank? || !processed_source.raw_source.include?('enable')
|
46
46
|
|
47
47
|
offenses = processed_source.comment_config.extra_enabled_comments
|
48
48
|
offenses.each { |comment, cop_names| register_offense(comment, cop_names) }
|
@@ -38,6 +38,10 @@ module RuboCop
|
|
38
38
|
MSG = 'Remove unnecessary `require` statement.'
|
39
39
|
RESTRICT_ON_SEND = %i[require].freeze
|
40
40
|
RUBY_22_LOADED_FEATURES = %w[rational complex].freeze
|
41
|
+
PRETTY_PRINT_METHODS = %i[
|
42
|
+
pretty_inspect pretty_print pretty_print_cycle
|
43
|
+
pretty_print_inspect pretty_print_instance_variables
|
44
|
+
].freeze
|
41
45
|
|
42
46
|
# @!method redundant_require_statement?(node)
|
43
47
|
def_node_matcher :redundant_require_statement?, <<~PATTERN
|
@@ -68,12 +72,18 @@ module RuboCop
|
|
68
72
|
feature_name == 'enumerator' ||
|
69
73
|
(target_ruby_version >= 2.1 && feature_name == 'thread') ||
|
70
74
|
(target_ruby_version >= 2.2 && RUBY_22_LOADED_FEATURES.include?(feature_name)) ||
|
71
|
-
(target_ruby_version >= 2.5 && feature_name == 'pp') ||
|
75
|
+
(target_ruby_version >= 2.5 && feature_name == 'pp' && !use_pretty_print_method?) ||
|
72
76
|
(target_ruby_version >= 2.7 && feature_name == 'ruby2_keywords') ||
|
73
77
|
(target_ruby_version >= 3.1 && feature_name == 'fiber') ||
|
74
78
|
(target_ruby_version >= 3.2 && feature_name == 'set')
|
75
79
|
end
|
76
80
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
81
|
+
|
82
|
+
def use_pretty_print_method?
|
83
|
+
processed_source.ast.each_descendant(:send).any? do |node|
|
84
|
+
PRETTY_PRINT_METHODS.include?(node.method_name)
|
85
|
+
end
|
86
|
+
end
|
77
87
|
end
|
78
88
|
end
|
79
89
|
end
|
@@ -17,13 +17,19 @@ module RuboCop
|
|
17
17
|
# do_something
|
18
18
|
# end
|
19
19
|
class RegexpAsCondition < Base
|
20
|
+
include IgnoredNode
|
20
21
|
extend AutoCorrector
|
21
22
|
|
22
23
|
MSG = 'Do not use regexp literal as a condition. ' \
|
23
24
|
'The regexp literal matches `$_` implicitly.'
|
24
25
|
|
25
26
|
def on_match_current_line(node)
|
27
|
+
return if node.ancestors.none?(&:conditional?)
|
28
|
+
return if part_of_ignored_node?(node)
|
29
|
+
|
26
30
|
add_offense(node) { |corrector| corrector.replace(node, "#{node.source} =~ $_") }
|
31
|
+
|
32
|
+
ignore_node(node)
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
@@ -46,7 +46,9 @@ module RuboCop
|
|
46
46
|
private
|
47
47
|
|
48
48
|
def check_ternary(ternary, node)
|
49
|
-
|
49
|
+
if node.method?(:[]) || node.assignment_method? || !ternary.condition.operator_keyword?
|
50
|
+
return
|
51
|
+
end
|
50
52
|
|
51
53
|
range = range_between(node.source_range.begin_pos, ternary.condition.source_range.end_pos)
|
52
54
|
|
@@ -45,13 +45,12 @@ module RuboCop
|
|
45
45
|
bad_method?(node) do |safe_nav, method|
|
46
46
|
return if nil_methods.include?(method) || PLUS_MINUS_METHODS.include?(node.method_name)
|
47
47
|
|
48
|
-
method_chain = method_chain(node)
|
49
48
|
location =
|
50
49
|
Parser::Source::Range.new(node.source_range.source_buffer,
|
51
50
|
safe_nav.source_range.end_pos,
|
52
|
-
|
51
|
+
node.source_range.end_pos)
|
53
52
|
add_offense(location) do |corrector|
|
54
|
-
autocorrect(corrector, offense_range: location, send_node:
|
53
|
+
autocorrect(corrector, offense_range: location, send_node: node)
|
55
54
|
end
|
56
55
|
end
|
57
56
|
end
|
@@ -62,15 +61,16 @@ module RuboCop
|
|
62
61
|
# @param [RuboCop::AST::SendNode] send_node
|
63
62
|
# @return [String]
|
64
63
|
def add_safe_navigation_operator(offense_range:, send_node:)
|
65
|
-
source =
|
66
|
-
if
|
64
|
+
source =
|
65
|
+
if brackets?(send_node)
|
67
66
|
format(
|
68
|
-
'%<method_name>s(%<arguments>s)',
|
67
|
+
'%<method_name>s(%<arguments>s)%<method_chain>s',
|
69
68
|
arguments: send_node.arguments.map(&:source).join(', '),
|
70
|
-
method_name: send_node.method_name
|
69
|
+
method_name: send_node.method_name,
|
70
|
+
method_chain: send_node.source_range.end.join(send_node.source_range.end).source
|
71
71
|
)
|
72
72
|
else
|
73
|
-
offense_range.source
|
73
|
+
offense_range.source
|
74
74
|
end
|
75
75
|
source.prepend('.') unless source.start_with?('.')
|
76
76
|
source.prepend('&')
|
@@ -89,10 +89,8 @@ module RuboCop
|
|
89
89
|
)
|
90
90
|
end
|
91
91
|
|
92
|
-
def
|
93
|
-
|
94
|
-
chain = chain.parent if chain.send_type? && chain.parent&.call_type?
|
95
|
-
chain
|
92
|
+
def brackets?(send_node)
|
93
|
+
send_node.method?(:[]) || send_node.method?(:[]=)
|
96
94
|
end
|
97
95
|
end
|
98
96
|
end
|
@@ -48,16 +48,17 @@ module RuboCop
|
|
48
48
|
(send
|
49
49
|
(const _ _) {:#{SEND_METHODS.join(' :')}}
|
50
50
|
({sym str} $#mixin_method?)
|
51
|
-
$(const _ _))
|
51
|
+
$(const _ _)+)
|
52
52
|
PATTERN
|
53
53
|
|
54
54
|
def on_send(node)
|
55
|
-
send_with_mixin_argument?(node) do |method,
|
56
|
-
|
55
|
+
send_with_mixin_argument?(node) do |method, module_names|
|
56
|
+
module_names_source = module_names.map(&:source).join(', ')
|
57
|
+
message = message(method, module_names_source, bad_location(node).source)
|
57
58
|
|
58
59
|
bad_location = bad_location(node)
|
59
60
|
add_offense(bad_location, message: message) do |corrector|
|
60
|
-
corrector.replace(bad_location, "#{method} #{
|
61
|
+
corrector.replace(bad_location, "#{method} #{module_names_source}")
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
@@ -73,10 +73,11 @@ module RuboCop
|
|
73
73
|
outer_local_variable_node =
|
74
74
|
find_conditional_node_from_ascendant(outer_local_variable.declaration_node)
|
75
75
|
return true unless outer_local_variable_node
|
76
|
+
return false unless outer_local_variable_node.conditional?
|
77
|
+
return true if variable_node == outer_local_variable_node
|
76
78
|
|
77
|
-
outer_local_variable_node.
|
78
|
-
|
79
|
-
variable_node == outer_local_variable_node.else_branch)
|
79
|
+
outer_local_variable_node.if_type? &&
|
80
|
+
variable_node == outer_local_variable_node.else_branch
|
80
81
|
end
|
81
82
|
|
82
83
|
def variable_node(variable)
|
@@ -100,7 +100,8 @@ module RuboCop
|
|
100
100
|
|
101
101
|
unless variable.keyword_argument?
|
102
102
|
message << " If it's necessary, use `_` or `_#{variable.name}` " \
|
103
|
-
"as an argument name to indicate that it won't be used."
|
103
|
+
"as an argument name to indicate that it won't be used. " \
|
104
|
+
"If it's unnecessary, remove it."
|
104
105
|
end
|
105
106
|
|
106
107
|
scope = variable.scope
|
@@ -41,7 +41,7 @@ module RuboCop
|
|
41
41
|
MSG = 'Useless method definition detected.'
|
42
42
|
|
43
43
|
def on_def(node)
|
44
|
-
return if
|
44
|
+
return if use_rest_or_optional_args?(node)
|
45
45
|
return unless delegating?(node.body, node)
|
46
46
|
|
47
47
|
add_offense(node) { |corrector| corrector.remove(node) }
|
@@ -50,8 +50,8 @@ module RuboCop
|
|
50
50
|
|
51
51
|
private
|
52
52
|
|
53
|
-
def
|
54
|
-
node.arguments.any? { |arg| arg.optarg_type? || arg.kwoptarg_type? }
|
53
|
+
def use_rest_or_optional_args?(node)
|
54
|
+
node.arguments.any? { |arg| arg.restarg_type? || arg.optarg_type? || arg.kwoptarg_type? }
|
55
55
|
end
|
56
56
|
|
57
57
|
def delegating?(node, def_node)
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Checks for useless `rescue`s, which only reraise rescued exceptions.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# def foo
|
11
|
+
# do_something
|
12
|
+
# rescue
|
13
|
+
# raise
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
# def foo
|
18
|
+
# do_something
|
19
|
+
# rescue => e
|
20
|
+
# raise # or 'raise e', or 'raise $!', or 'raise $ERROR_INFO'
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# def foo
|
25
|
+
# do_something
|
26
|
+
# rescue
|
27
|
+
# do_cleanup
|
28
|
+
# raise
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # bad (latest rescue)
|
32
|
+
# def foo
|
33
|
+
# do_something
|
34
|
+
# rescue ArgumentError
|
35
|
+
# # noop
|
36
|
+
# rescue
|
37
|
+
# raise
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# # good (not the latest rescue)
|
41
|
+
# def foo
|
42
|
+
# do_something
|
43
|
+
# rescue ArgumentError
|
44
|
+
# raise
|
45
|
+
# rescue
|
46
|
+
# # noop
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
class UselessRescue < Base
|
50
|
+
MSG = 'Useless `rescue` detected.'
|
51
|
+
|
52
|
+
def on_rescue(node)
|
53
|
+
resbody_node = node.resbody_branches.last
|
54
|
+
add_offense(resbody_node) if only_reraising?(resbody_node)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def only_reraising?(resbody_node)
|
60
|
+
return false if use_exception_variable_in_ensure?(resbody_node)
|
61
|
+
|
62
|
+
body = resbody_node.body
|
63
|
+
return false if body.nil? || !body.send_type? || !body.method?(:raise)
|
64
|
+
return true unless body.arguments?
|
65
|
+
return false if body.arguments.size > 1
|
66
|
+
|
67
|
+
exception_name = body.first_argument.source
|
68
|
+
|
69
|
+
exception_objects(resbody_node).include?(exception_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
def use_exception_variable_in_ensure?(resbody_node)
|
73
|
+
return false unless (exception_variable = resbody_node.exception_variable)
|
74
|
+
return false unless (ensure_node = resbody_node.each_ancestor(:ensure).first)
|
75
|
+
|
76
|
+
ensure_node.body.each_descendant(:lvar).map(&:source).include?(exception_variable.source)
|
77
|
+
end
|
78
|
+
|
79
|
+
def exception_objects(resbody_node)
|
80
|
+
[resbody_node.exception_variable&.source, '$!', '$ERROR_INFO']
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -77,10 +77,12 @@ module RuboCop
|
|
77
77
|
PATTERN
|
78
78
|
|
79
79
|
def on_send(node)
|
80
|
-
|
81
|
-
|
80
|
+
return unless (first_argument = node.first_argument)
|
81
|
+
|
82
|
+
if first_argument.def_type?
|
83
|
+
inspect_def(node, first_argument)
|
82
84
|
elsif node.first_argument.sym_type?
|
83
|
-
inspect_sym(node,
|
85
|
+
inspect_sym(node, first_argument)
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
@@ -96,7 +98,7 @@ module RuboCop
|
|
96
98
|
return unless node.parent
|
97
99
|
|
98
100
|
method_name = sym_node.value
|
99
|
-
definition = node
|
101
|
+
definition = find_method_definition(node, method_name)
|
100
102
|
|
101
103
|
return unless definition
|
102
104
|
return if allowed_arguments(definition.arguments)
|
@@ -104,6 +106,14 @@ module RuboCop
|
|
104
106
|
add_offense(node, message: format(MSG, method_name: method_name))
|
105
107
|
end
|
106
108
|
|
109
|
+
def find_method_definition(node, method_name)
|
110
|
+
node.each_ancestor.lazy.map do |ancestor|
|
111
|
+
ancestor.each_child_node(:def, :block, :numblock).find do |child|
|
112
|
+
method_definition(child, method_name)
|
113
|
+
end
|
114
|
+
end.find(&:itself)
|
115
|
+
end
|
116
|
+
|
107
117
|
# `ruby2_keywords` is only allowed if there's a `restarg` and no keyword arguments
|
108
118
|
def allowed_arguments(arguments)
|
109
119
|
return false if arguments.empty?
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
# Checks for operators, variables, literals, and nonmutating
|
6
|
+
# Checks for operators, variables, literals, lambda, proc and nonmutating
|
7
7
|
# methods used in void context.
|
8
8
|
#
|
9
9
|
# @example CheckForMethodsWithNoSideEffects: false (default)
|
@@ -45,20 +45,24 @@ module RuboCop
|
|
45
45
|
VAR_MSG = 'Variable `%<var>s` used in void context.'
|
46
46
|
LIT_MSG = 'Literal `%<lit>s` used in void context.'
|
47
47
|
SELF_MSG = '`self` used in void context.'
|
48
|
-
|
49
|
-
NONMUTATING_MSG = 'Method `#%<method>s` used in void context. Did you mean `#%<
|
48
|
+
EXPRESSION_MSG = '`%<expression>s` used in void context.'
|
49
|
+
NONMUTATING_MSG = 'Method `#%<method>s` used in void context. Did you mean `#%<suggest>s`?'
|
50
50
|
|
51
51
|
BINARY_OPERATORS = %i[* / % + - == === != < > <= >= <=>].freeze
|
52
52
|
UNARY_OPERATORS = %i[+@ -@ ~ !].freeze
|
53
53
|
OPERATORS = (BINARY_OPERATORS + UNARY_OPERATORS).freeze
|
54
54
|
VOID_CONTEXT_TYPES = %i[def for block].freeze
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
NONMUTATING_METHODS_WITH_BANG_VERSION = %i[capitalize chomp chop compact
|
56
|
+
delete_prefix delete_suffix downcase
|
57
|
+
encode flatten gsub lstrip merge next
|
58
|
+
reject reverse rotate rstrip scrub select
|
59
|
+
shuffle slice sort sort_by squeeze strip sub
|
60
|
+
succ swapcase tr tr_s transform_values
|
61
|
+
unicode_normalize uniq upcase].freeze
|
62
|
+
METHODS_REPLACABLE_BY_EACH = %i[collect map].freeze
|
63
|
+
|
64
|
+
NONMUTATING_METHODS = (NONMUTATING_METHODS_WITH_BANG_VERSION +
|
65
|
+
METHODS_REPLACABLE_BY_EACH).freeze
|
62
66
|
|
63
67
|
def on_block(node)
|
64
68
|
return unless node.body && !node.body.begin_type?
|
@@ -87,7 +91,7 @@ module RuboCop
|
|
87
91
|
check_literal(expr)
|
88
92
|
check_var(expr)
|
89
93
|
check_self(expr)
|
90
|
-
|
94
|
+
check_void_expression(expr)
|
91
95
|
return unless cop_config['CheckForMethodsWithNoSideEffects']
|
92
96
|
|
93
97
|
check_nonmutating(expr)
|
@@ -117,16 +121,21 @@ module RuboCop
|
|
117
121
|
add_offense(node, message: SELF_MSG)
|
118
122
|
end
|
119
123
|
|
120
|
-
def
|
121
|
-
return unless node.defined_type?
|
124
|
+
def check_void_expression(node)
|
125
|
+
return unless node.defined_type? || node.lambda_or_proc?
|
122
126
|
|
123
|
-
add_offense(node, message: format(
|
127
|
+
add_offense(node, message: format(EXPRESSION_MSG, expression: node.source))
|
124
128
|
end
|
125
129
|
|
126
130
|
def check_nonmutating(node)
|
127
|
-
return unless node.
|
131
|
+
return unless node.respond_to?(:method_name)
|
128
132
|
|
129
|
-
|
133
|
+
method_name = node.method_name
|
134
|
+
return unless NONMUTATING_METHODS.include?(method_name)
|
135
|
+
|
136
|
+
suggestion = METHODS_REPLACABLE_BY_EACH.include?(method_name) ? 'each' : "#{method_name}!"
|
137
|
+
add_offense(node,
|
138
|
+
message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
|
130
139
|
end
|
131
140
|
|
132
141
|
def in_void_context?(node)
|
@@ -8,8 +8,8 @@ module RuboCop
|
|
8
8
|
# The maximum allowed length is configurable.
|
9
9
|
# The cop can be configured to ignore blocks passed to certain methods.
|
10
10
|
#
|
11
|
-
# You can set
|
12
|
-
# Available are: 'array', 'hash', and '
|
11
|
+
# You can set constructs you want to fold with `CountAsOne`.
|
12
|
+
# Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
|
13
13
|
# will be counted as one line regardless of its actual size.
|
14
14
|
#
|
15
15
|
#
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
# for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns`
|
18
18
|
# instead. By default, there are no methods to allowed.
|
19
19
|
#
|
20
|
-
# @example CountAsOne: ['array', 'heredoc']
|
20
|
+
# @example CountAsOne: ['array', 'heredoc', 'method_call']
|
21
21
|
#
|
22
22
|
# something do
|
23
23
|
# array = [ # +1
|
@@ -33,7 +33,12 @@ module RuboCop
|
|
33
33
|
# Heredoc
|
34
34
|
# content.
|
35
35
|
# HEREDOC
|
36
|
-
#
|
36
|
+
#
|
37
|
+
# foo( # +1
|
38
|
+
# 1,
|
39
|
+
# 2
|
40
|
+
# )
|
41
|
+
# end # 6 points
|
37
42
|
#
|
38
43
|
# NOTE: This cop does not apply for `Struct` definitions.
|
39
44
|
class BlockLength < Base
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# The maximum level of nesting allowed is configurable.
|
14
14
|
class BlockNesting < Base
|
15
|
-
NESTING_BLOCKS = %i[case if while while_post until until_post for resbody].freeze
|
15
|
+
NESTING_BLOCKS = %i[case case_match if while while_post until until_post for resbody].freeze
|
16
16
|
|
17
17
|
exclude_limit 'Max'
|
18
18
|
|
@@ -3,15 +3,15 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Metrics
|
6
|
-
# Checks if the length a class exceeds some maximum value.
|
6
|
+
# Checks if the length of a class exceeds some maximum value.
|
7
7
|
# Comment lines can optionally be ignored.
|
8
8
|
# The maximum allowed length is configurable.
|
9
9
|
#
|
10
|
-
# You can set
|
11
|
-
# Available are: 'array', 'hash', and '
|
10
|
+
# You can set constructs you want to fold with `CountAsOne`.
|
11
|
+
# Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
|
12
12
|
# will be counted as one line regardless of its actual size.
|
13
13
|
#
|
14
|
-
# @example CountAsOne: ['array', 'heredoc']
|
14
|
+
# @example CountAsOne: ['array', 'heredoc', 'method_call']
|
15
15
|
#
|
16
16
|
# class Foo
|
17
17
|
# ARRAY = [ # +1
|
@@ -27,7 +27,12 @@ module RuboCop
|
|
27
27
|
# Heredoc
|
28
28
|
# content.
|
29
29
|
# HEREDOC
|
30
|
-
#
|
30
|
+
#
|
31
|
+
# foo( # +1
|
32
|
+
# 1,
|
33
|
+
# 2
|
34
|
+
# )
|
35
|
+
# end # 6 points
|
31
36
|
#
|
32
37
|
#
|
33
38
|
# NOTE: This cop also applies for `Struct` definitions.
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
|
36
36
|
MSG = 'Cyclomatic complexity for %<method>s is too high. [%<complexity>d/%<max>d]'
|
37
37
|
COUNTED_NODES = %i[if while until for csend block block_pass
|
38
|
-
rescue when and or or_asgn and_asgn].freeze
|
38
|
+
rescue when in_pattern and or or_asgn and_asgn].freeze
|
39
39
|
|
40
40
|
private
|
41
41
|
|
@@ -7,8 +7,8 @@ module RuboCop
|
|
7
7
|
# Comment lines can optionally be allowed.
|
8
8
|
# The maximum allowed length is configurable.
|
9
9
|
#
|
10
|
-
# You can set
|
11
|
-
# Available are: 'array', 'hash', and '
|
10
|
+
# You can set constructs you want to fold with `CountAsOne`.
|
11
|
+
# Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
|
12
12
|
# will be counted as one line regardless of its actual size.
|
13
13
|
#
|
14
14
|
# NOTE: The `ExcludedMethods` and `IgnoredMethods` configuration is
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
16
16
|
# Please use `AllowedMethods` and `AllowedPatterns` instead.
|
17
17
|
# By default, there are no methods to allowed.
|
18
18
|
#
|
19
|
-
# @example CountAsOne: ['array', 'heredoc']
|
19
|
+
# @example CountAsOne: ['array', 'heredoc', 'method_call']
|
20
20
|
#
|
21
21
|
# def m
|
22
22
|
# array = [ # +1
|
@@ -32,7 +32,12 @@ module RuboCop
|
|
32
32
|
# Heredoc
|
33
33
|
# content.
|
34
34
|
# HEREDOC
|
35
|
-
#
|
35
|
+
#
|
36
|
+
# foo( # +1
|
37
|
+
# 1,
|
38
|
+
# 2
|
39
|
+
# )
|
40
|
+
# end # 6 points
|
36
41
|
#
|
37
42
|
class MethodLength < Base
|
38
43
|
include CodeLength
|
@@ -3,15 +3,15 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Metrics
|
6
|
-
# Checks if the length a module exceeds some maximum value.
|
6
|
+
# Checks if the length of a module exceeds some maximum value.
|
7
7
|
# Comment lines can optionally be ignored.
|
8
8
|
# The maximum allowed length is configurable.
|
9
9
|
#
|
10
|
-
# You can set
|
11
|
-
# Available are: 'array', 'hash', and '
|
10
|
+
# You can set constructs you want to fold with `CountAsOne`.
|
11
|
+
# Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
|
12
12
|
# will be counted as one line regardless of its actual size.
|
13
13
|
#
|
14
|
-
# @example CountAsOne: ['array', 'heredoc']
|
14
|
+
# @example CountAsOne: ['array', 'heredoc', 'method_call']
|
15
15
|
#
|
16
16
|
# module M
|
17
17
|
# ARRAY = [ # +1
|
@@ -27,7 +27,12 @@ module RuboCop
|
|
27
27
|
# Heredoc
|
28
28
|
# content.
|
29
29
|
# HEREDOC
|
30
|
-
#
|
30
|
+
#
|
31
|
+
# foo( # +1
|
32
|
+
# 1,
|
33
|
+
# 2
|
34
|
+
# )
|
35
|
+
# end # 6 points
|
31
36
|
#
|
32
37
|
class ModuleLength < Base
|
33
38
|
include CodeLength
|
@@ -9,6 +9,20 @@ module RuboCop
|
|
9
9
|
# Keyword arguments can optionally be excluded from the total count,
|
10
10
|
# as they add less complexity than positional or optional parameters.
|
11
11
|
#
|
12
|
+
# Any number of arguments for `initialize` method inside a block of
|
13
|
+
# `Struct.new` and `Data.define` like this is always allowed:
|
14
|
+
#
|
15
|
+
# [source,ruby]
|
16
|
+
# ----
|
17
|
+
# Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
|
18
|
+
# def initialize(one:, two:, three:, four:, five:)
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
# ----
|
22
|
+
#
|
23
|
+
# This is because checking the number of arguments of the `initialize` method
|
24
|
+
# does not make sense.
|
25
|
+
#
|
12
26
|
# NOTE: Explicit block argument `&block` is not counted to prevent
|
13
27
|
# erroneous change that is avoided by making block argument implicit.
|
14
28
|
#
|
@@ -63,6 +77,16 @@ module RuboCop
|
|
63
77
|
NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
|
64
78
|
private_constant :NAMED_KEYWORD_TYPES
|
65
79
|
|
80
|
+
# @!method struct_new_or_data_define_block?(node)
|
81
|
+
def_node_matcher :struct_new_or_data_define_block?, <<~PATTERN
|
82
|
+
(block
|
83
|
+
{
|
84
|
+
(send (const {nil? cbase} :Struct) :new ...)
|
85
|
+
(send (const {nil? cbase} :Data) :define ...)
|
86
|
+
}
|
87
|
+
(args) ...)
|
88
|
+
PATTERN
|
89
|
+
|
66
90
|
def on_def(node)
|
67
91
|
optargs = node.arguments.select(&:optarg_type?)
|
68
92
|
return if optargs.count <= max_optional_parameters
|
@@ -78,6 +102,9 @@ module RuboCop
|
|
78
102
|
alias on_defs on_def
|
79
103
|
|
80
104
|
def on_args(node)
|
105
|
+
parent = node.parent
|
106
|
+
return if parent.method?(:initialize) && struct_new_or_data_define_block?(parent.parent)
|
107
|
+
|
81
108
|
count = args_count(node)
|
82
109
|
return unless count > max_params
|
83
110
|
|