rubocop 1.48.1 → 1.52.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 +3 -3
- data/config/default.yml +59 -12
- data/lib/rubocop/cli/command/execute_runner.rb +7 -2
- data/lib/rubocop/cli.rb +6 -6
- data/lib/rubocop/config.rb +5 -1
- data/lib/rubocop/config_loader.rb +8 -8
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +28 -12
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +33 -9
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
- data/lib/rubocop/cop/layout/class_structure.rb +1 -0
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +0 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -4
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
- data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
- data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
- data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/missing_super.rb +3 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
- data/lib/rubocop/cop/lint/useless_assignment.rb +59 -1
- data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
- data/lib/rubocop/cop/lint/void.rb +69 -9
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +22 -7
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
- data/lib/rubocop/cop/style/class_equality_comparison.rb +51 -40
- data/lib/rubocop/cop/style/collection_compact.rb +19 -6
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +6 -3
- data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/dir_empty.rb +8 -14
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +2 -2
- data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/file_empty.rb +3 -3
- data/lib/rubocop/cop/style/file_read.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
- data/lib/rubocop/cop/style/hash_except.rb +23 -12
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +41 -12
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +9 -5
- data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
- data/lib/rubocop/cop/style/map_to_set.rb +4 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -9
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
- data/lib/rubocop/cop/style/multiline_method_signature.rb +6 -3
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/parallel_assignment.rb +26 -18
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +183 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +11 -5
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
- data/lib/rubocop/cop/style/semicolon.rb +12 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +5 -3
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/variable_force/assignment.rb +33 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +1 -0
- data/lib/rubocop/cops_documentation_generator.rb +10 -3
- data/lib/rubocop/ext/regexp_node.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/options.rb +4 -1
- data/lib/rubocop/result_cache.rb +2 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/server/cache.rb +1 -1
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/server/helper.rb +1 -1
- data/lib/rubocop/server/server_command/exec.rb +1 -1
- data/lib/rubocop/target_ruby.rb +3 -2
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +8 -0
- metadata +20 -12
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# NOTE: This cop does not move the first argument, if you want that to
|
10
10
|
# be on a separate line, see `Layout/FirstMethodArgumentLineBreak`.
|
11
11
|
#
|
12
|
-
# @example
|
12
|
+
# @example
|
13
13
|
#
|
14
14
|
# # bad
|
15
15
|
# foo(a, b,
|
@@ -31,6 +31,8 @@ module RuboCop
|
|
31
31
|
# # good
|
32
32
|
# foo(a, b, c)
|
33
33
|
#
|
34
|
+
# @example AllowMultilineFinalElement: false (default)
|
35
|
+
#
|
34
36
|
# # good
|
35
37
|
# foo(
|
36
38
|
# a,
|
@@ -42,26 +44,6 @@ module RuboCop
|
|
42
44
|
#
|
43
45
|
# @example AllowMultilineFinalElement: true
|
44
46
|
#
|
45
|
-
# # bad
|
46
|
-
# foo(a, b,
|
47
|
-
# c
|
48
|
-
# )
|
49
|
-
#
|
50
|
-
# # good
|
51
|
-
# foo(a, b, {
|
52
|
-
# foo: "bar",
|
53
|
-
# })
|
54
|
-
#
|
55
|
-
# # good
|
56
|
-
# foo(
|
57
|
-
# a,
|
58
|
-
# b,
|
59
|
-
# c
|
60
|
-
# )
|
61
|
-
#
|
62
|
-
# # good
|
63
|
-
# foo(a, b, c)
|
64
|
-
#
|
65
47
|
# # good
|
66
48
|
# foo(
|
67
49
|
# a,
|
@@ -70,6 +52,7 @@ module RuboCop
|
|
70
52
|
# foo: "bar",
|
71
53
|
# }
|
72
54
|
# )
|
55
|
+
#
|
73
56
|
class MultilineMethodArgumentLineBreaks < Base
|
74
57
|
include MultilineElementLineBreaks
|
75
58
|
extend AutoCorrector
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# NOTE: This cop does not move the first argument, if you want that to
|
10
10
|
# be on a separate line, see `Layout/FirstMethodParameterLineBreak`.
|
11
11
|
#
|
12
|
-
# @example
|
12
|
+
# @example
|
13
13
|
#
|
14
14
|
# # bad
|
15
15
|
# def foo(a, b,
|
@@ -17,12 +17,6 @@ module RuboCop
|
|
17
17
|
# )
|
18
18
|
# end
|
19
19
|
#
|
20
|
-
# # bad
|
21
|
-
# def foo(a, b = {
|
22
|
-
# foo: "bar",
|
23
|
-
# })
|
24
|
-
# end
|
25
|
-
#
|
26
20
|
# # good
|
27
21
|
# def foo(
|
28
22
|
# a,
|
@@ -44,40 +38,22 @@ module RuboCop
|
|
44
38
|
# def foo(a, b, c)
|
45
39
|
# end
|
46
40
|
#
|
47
|
-
# @example AllowMultilineFinalElement:
|
41
|
+
# @example AllowMultilineFinalElement: false (default)
|
48
42
|
#
|
49
43
|
# # bad
|
50
|
-
# def foo(a, b,
|
51
|
-
# c
|
52
|
-
# )
|
53
|
-
# end
|
54
|
-
#
|
55
|
-
# # good
|
56
44
|
# def foo(a, b = {
|
57
45
|
# foo: "bar",
|
58
46
|
# })
|
59
47
|
# end
|
60
48
|
#
|
61
|
-
#
|
62
|
-
# def foo(
|
63
|
-
# a,
|
64
|
-
# b,
|
65
|
-
# c
|
66
|
-
# )
|
67
|
-
# end
|
49
|
+
# @example AllowMultilineFinalElement: true
|
68
50
|
#
|
69
51
|
# # good
|
70
|
-
# def foo(
|
71
|
-
#
|
72
|
-
#
|
73
|
-
# foo: "bar",
|
74
|
-
# }
|
75
|
-
# )
|
52
|
+
# def foo(a, b = {
|
53
|
+
# foo: "bar",
|
54
|
+
# })
|
76
55
|
# end
|
77
56
|
#
|
78
|
-
# # good
|
79
|
-
# def foo(a, b, c)
|
80
|
-
# end
|
81
57
|
class MultilineMethodParameterLineBreaks < Base
|
82
58
|
include MultilineElementLineBreaks
|
83
59
|
extend AutoCorrector
|
@@ -50,9 +50,9 @@ module RuboCop
|
|
50
50
|
|
51
51
|
def on_send(node)
|
52
52
|
# Include "the whole expression".
|
53
|
-
node = node.parent while
|
54
|
-
|
55
|
-
node.parent
|
53
|
+
node = node.parent while node.parent&.send_type? ||
|
54
|
+
convertible_block?(node) ||
|
55
|
+
node.parent.is_a?(RuboCop::AST::BinaryOperatorNode)
|
56
56
|
|
57
57
|
return unless offense?(node) && !part_of_ignored_node?(node)
|
58
58
|
|
@@ -69,15 +69,14 @@ module RuboCop
|
|
69
69
|
|
70
70
|
def register_offense(node)
|
71
71
|
add_offense(node) do |corrector|
|
72
|
-
corrector.replace(node
|
72
|
+
corrector.replace(node, to_single_line(node.source).strip)
|
73
73
|
end
|
74
74
|
ignore_node(node)
|
75
75
|
end
|
76
76
|
|
77
77
|
def offense?(node)
|
78
|
-
|
79
|
-
|
80
|
-
node.multiline? && !too_long?(node) && suitable_as_single_line?(node)
|
78
|
+
node.multiline? && !too_long?(node) && suitable_as_single_line?(node) &&
|
79
|
+
!configured_to_not_be_inspected?(node)
|
81
80
|
end
|
82
81
|
|
83
82
|
def configured_to_not_be_inspected?(node)
|
@@ -34,6 +34,7 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def on_send(node)
|
36
36
|
return unless regular_method_call_with_arguments?(node)
|
37
|
+
return if node.parenthesized?
|
37
38
|
|
38
39
|
first_arg = node.first_argument.source_range
|
39
40
|
first_arg_with_space = range_with_surrounding_space(first_arg, side: :left)
|
@@ -52,7 +53,6 @@ module RuboCop
|
|
52
53
|
end
|
53
54
|
|
54
55
|
def expect_params_after_method_name?(node)
|
55
|
-
return false if node.parenthesized?
|
56
56
|
return true if no_space_between_method_name_and_first_argument?(node)
|
57
57
|
|
58
58
|
first_arg = node.first_argument
|
@@ -236,6 +236,8 @@ module RuboCop
|
|
236
236
|
end
|
237
237
|
|
238
238
|
def offense(begin_pos, end_pos, msg, style_param = 'EnforcedStyle')
|
239
|
+
return if begin_pos > end_pos
|
240
|
+
|
239
241
|
range = range_between(begin_pos, end_pos)
|
240
242
|
add_offense(range, message: msg) do |corrector|
|
241
243
|
case range.source
|
@@ -91,7 +91,7 @@ module RuboCop
|
|
91
91
|
if !left_parens?(token1, token2) && !right_parens?(token1, token2)
|
92
92
|
correct_missing_space(token1, token2)
|
93
93
|
else
|
94
|
-
|
94
|
+
correct_extraneous_space_between_consecutive_parens(token1, token2)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
end
|
@@ -112,7 +112,7 @@ module RuboCop
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
-
def
|
115
|
+
def correct_extraneous_space_between_consecutive_parens(token1, token2)
|
116
116
|
return if range_between(token1.end_pos, token2.begin_pos).source != ' '
|
117
117
|
|
118
118
|
range = range_between(token1.end_pos, token2.begin_pos)
|
@@ -52,6 +52,8 @@ module RuboCop
|
|
52
52
|
# expect { do_something }.to not_change { object.attribute }
|
53
53
|
#
|
54
54
|
class AmbiguousBlockAssociation < Base
|
55
|
+
extend AutoCorrector
|
56
|
+
|
55
57
|
include AllowedMethods
|
56
58
|
include AllowedPattern
|
57
59
|
|
@@ -68,7 +70,9 @@ module RuboCop
|
|
68
70
|
|
69
71
|
message = message(node)
|
70
72
|
|
71
|
-
add_offense(node, message: message)
|
73
|
+
add_offense(node, message: message) do |corrector|
|
74
|
+
wrap_in_parentheses(corrector, node)
|
75
|
+
end
|
72
76
|
end
|
73
77
|
alias on_csend on_send
|
74
78
|
|
@@ -89,6 +93,14 @@ module RuboCop
|
|
89
93
|
|
90
94
|
format(MSG, param: block_param.source, method: block_param.send_node.source)
|
91
95
|
end
|
96
|
+
|
97
|
+
def wrap_in_parentheses(corrector, node)
|
98
|
+
range = node.loc.selector.end.join(node.first_argument.source_range.begin)
|
99
|
+
|
100
|
+
corrector.remove(range)
|
101
|
+
corrector.insert_before(range, '(')
|
102
|
+
corrector.insert_after(node.last_argument, ')')
|
103
|
+
end
|
92
104
|
end
|
93
105
|
end
|
94
106
|
end
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
38
38
|
attr clone dup exists? freeze gethostbyaddr gethostbyname iterator?
|
39
39
|
].freeze
|
40
40
|
|
41
|
-
|
41
|
+
PREFERRED_METHODS = {
|
42
42
|
clone: 'to_h',
|
43
43
|
dup: 'to_h',
|
44
44
|
exists?: 'exist?',
|
@@ -97,11 +97,11 @@ module RuboCop
|
|
97
97
|
|
98
98
|
"#{preferred_attr_method} #{node.first_argument.source}"
|
99
99
|
elsif dir_env_file_const?(node.receiver)
|
100
|
-
prefer =
|
100
|
+
prefer = PREFERRED_METHODS[node.method_name]
|
101
101
|
|
102
102
|
prefer ? "#{node.receiver.source}.#{prefer}" : 'ENV'
|
103
103
|
else
|
104
|
-
|
104
|
+
PREFERRED_METHODS[node.method_name]
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Checks that there are no repeated patterns used in `in` keywords.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# case x
|
12
|
+
# in 'first'
|
13
|
+
# do_something
|
14
|
+
# in 'first'
|
15
|
+
# do_something_else
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# case x
|
20
|
+
# in 'first'
|
21
|
+
# do_something
|
22
|
+
# in 'second'
|
23
|
+
# do_something_else
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # bad - repeated alternate patterns with the same conditions don't depend on the order
|
27
|
+
# case x
|
28
|
+
# in foo | bar
|
29
|
+
# first_method
|
30
|
+
# in bar | foo
|
31
|
+
# second_method
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# case x
|
36
|
+
# in foo | bar
|
37
|
+
# first_method
|
38
|
+
# in bar | baz
|
39
|
+
# second_method
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # bad - repeated hash patterns with the same conditions don't depend on the order
|
43
|
+
# case x
|
44
|
+
# in foo: a, bar: b
|
45
|
+
# first_method
|
46
|
+
# in bar: b, foo: a
|
47
|
+
# second_method
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# # good
|
51
|
+
# case x
|
52
|
+
# in foo: a, bar: b
|
53
|
+
# first_method
|
54
|
+
# in bar: b, baz: c
|
55
|
+
# second_method
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# # bad - repeated array patterns with elements in the same order
|
59
|
+
# case x
|
60
|
+
# in [foo, bar]
|
61
|
+
# first_method
|
62
|
+
# in [foo, bar]
|
63
|
+
# second_method
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# # good
|
67
|
+
# case x
|
68
|
+
# in [foo, bar]
|
69
|
+
# first_method
|
70
|
+
# in [bar, foo]
|
71
|
+
# second_method
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# # bad - repeated the same patterns and guard conditions
|
75
|
+
# case x
|
76
|
+
# in foo if bar
|
77
|
+
# first_method
|
78
|
+
# in foo if bar
|
79
|
+
# second_method
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # good
|
83
|
+
# case x
|
84
|
+
# in foo if bar
|
85
|
+
# first_method
|
86
|
+
# in foo if baz
|
87
|
+
# second_method
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
class DuplicateMatchPattern < Base
|
91
|
+
extend TargetRubyVersion
|
92
|
+
|
93
|
+
MSG = 'Duplicate `in` pattern detected.'
|
94
|
+
|
95
|
+
minimum_target_ruby_version 2.7
|
96
|
+
|
97
|
+
def on_case_match(case_node)
|
98
|
+
case_node.in_pattern_branches.each_with_object(Set.new) do |in_pattern_node, previous|
|
99
|
+
pattern = in_pattern_node.pattern
|
100
|
+
next if previous.add?(pattern_identity(pattern))
|
101
|
+
|
102
|
+
add_offense(pattern)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def pattern_identity(pattern)
|
109
|
+
pattern_source = if pattern.hash_pattern_type? || pattern.match_alt_type?
|
110
|
+
pattern.children.map(&:source).sort.to_s
|
111
|
+
else
|
112
|
+
pattern.source
|
113
|
+
end
|
114
|
+
|
115
|
+
return pattern_source unless (guard = pattern.parent.children[1])
|
116
|
+
|
117
|
+
pattern_source + guard.source
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -25,7 +25,7 @@ module RuboCop
|
|
25
25
|
def on_interpolation(begin_node)
|
26
26
|
return unless begin_node.children.empty?
|
27
27
|
|
28
|
-
add_offense(begin_node) { |corrector| corrector.remove(begin_node
|
28
|
+
add_offense(begin_node) { |corrector| corrector.remove(begin_node) }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -3,14 +3,13 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
7
|
-
# This cop emulates the following Ruby warnings in Ruby 2.6.
|
6
|
+
# Emulates the following Ruby warnings in Ruby 2.6.
|
8
7
|
#
|
9
8
|
# [source,console]
|
10
9
|
# ----
|
11
|
-
#
|
10
|
+
# $ cat example.rb
|
12
11
|
# ERB.new('hi', nil, '-', '@output_buffer')
|
13
|
-
#
|
12
|
+
# $ ruby -rerb example.rb
|
14
13
|
# example.rb:1: warning: Passing safe_level with the 2nd argument of ERB.new is
|
15
14
|
# deprecated. Do not use it, and specify other arguments as keyword arguments.
|
16
15
|
# example.rb:1: warning: Passing trim_mode with the 3rd argument of ERB.new is
|
@@ -3,8 +3,10 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
+
# Checks for `IO.select` that is incompatible with Fiber Scheduler since Ruby 3.0.
|
6
7
|
#
|
7
|
-
#
|
8
|
+
# When an array of IO objects waiting for an exception (the third argument of `IO.select`)
|
9
|
+
# is used as an argument, there is no alternative API, so offenses are not registered.
|
8
10
|
#
|
9
11
|
# NOTE: When the method is successful the return value of `IO.select` is `[[IO]]`,
|
10
12
|
# and the return value of `io.wait_readable` and `io.wait_writable` are `self`.
|
@@ -42,8 +44,8 @@ module RuboCop
|
|
42
44
|
PATTERN
|
43
45
|
|
44
46
|
def on_send(node)
|
45
|
-
read, write,
|
46
|
-
return
|
47
|
+
read, write, excepts, timeout = *io_select(node)
|
48
|
+
return if excepts && !excepts.children.empty?
|
47
49
|
return unless scheduler_compatible?(read, write) || scheduler_compatible?(write, read)
|
48
50
|
|
49
51
|
preferred = preferred_method(read, write, timeout)
|
@@ -58,6 +58,7 @@ module RuboCop
|
|
58
58
|
|
59
59
|
def on_class(node)
|
60
60
|
return unless node.parent_class && exception_class?(node.parent_class)
|
61
|
+
return if inherit_exception_class_with_omitted_namespace?(node)
|
61
62
|
|
62
63
|
message = message(node.parent_class)
|
63
64
|
|
@@ -87,6 +88,14 @@ module RuboCop
|
|
87
88
|
class_node.const_name == 'Exception'
|
88
89
|
end
|
89
90
|
|
91
|
+
def inherit_exception_class_with_omitted_namespace?(class_node)
|
92
|
+
return false if class_node.parent_class.namespace&.cbase_type?
|
93
|
+
|
94
|
+
class_node.left_siblings.any? do |sibling|
|
95
|
+
sibling.respond_to?(:identifier) && exception_class?(sibling.identifier)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
90
99
|
def preferred_base_class
|
91
100
|
PREFERRED_BASE_CLASS[style]
|
92
101
|
end
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
6
6
|
# Checks uses of lambda without a literal block.
|
7
7
|
# It emulates the following warning in Ruby 3.0:
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# $ ruby -vwe 'lambda(&proc {})'
|
10
10
|
# ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
|
11
11
|
# -e:1: warning: lambda without a literal block is deprecated; use the proc without
|
12
12
|
# lambda instead
|
@@ -11,6 +11,9 @@ module RuboCop
|
|
11
11
|
# missing method. In other cases, the theoretical ideal handling could be
|
12
12
|
# challenging or verbose for no actual gain.
|
13
13
|
#
|
14
|
+
# Autocorrection is not supported because the position of `super` cannot be
|
15
|
+
# determined automatically.
|
16
|
+
#
|
14
17
|
# @example
|
15
18
|
# # bad
|
16
19
|
# class Employee < Person
|
@@ -131,12 +131,12 @@ module RuboCop
|
|
131
131
|
|
132
132
|
# @!method eval_call?(node)
|
133
133
|
def_node_matcher :eval_call?, <<~PATTERN
|
134
|
-
(block (send _ {:instance_eval :class_eval :module_eval} ...) ...)
|
134
|
+
({block numblock} (send _ {:instance_eval :class_eval :module_eval} ...) ...)
|
135
135
|
PATTERN
|
136
136
|
|
137
137
|
# @!method exec_call?(node)
|
138
138
|
def_node_matcher :exec_call?, <<~PATTERN
|
139
|
-
(block (send _ {:instance_exec :class_exec :module_exec} ...) ...)
|
139
|
+
({block numblock} (send _ {:instance_exec :class_exec :module_exec} ...) ...)
|
140
140
|
PATTERN
|
141
141
|
end
|
142
142
|
end
|
@@ -6,13 +6,13 @@ module RuboCop
|
|
6
6
|
# Checks for uses of numbered parameter assignment.
|
7
7
|
# It emulates the following warning in Ruby 2.7:
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# $ ruby -ve '_1 = :value'
|
10
10
|
# ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
|
11
11
|
# -e:1: warning: `_1' is reserved for numbered parameter; consider another name
|
12
12
|
#
|
13
13
|
# Assigning to a numbered parameter (from `_1` to `_9`) causes an error in Ruby 3.0.
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# $ ruby -ve '_1 = :value'
|
16
16
|
# ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
|
17
17
|
# -e:1: _1 is reserved for numbered parameter
|
18
18
|
#
|
@@ -65,13 +65,13 @@ module RuboCop
|
|
65
65
|
def on_when(node)
|
66
66
|
regexp_conditions = node.conditions.select(&:regexp_type?)
|
67
67
|
|
68
|
-
@valid_ref = regexp_conditions.
|
68
|
+
@valid_ref = regexp_conditions.filter_map { |condition| check_regexp(condition) }.max
|
69
69
|
end
|
70
70
|
|
71
71
|
def on_in_pattern(node)
|
72
72
|
regexp_patterns = regexp_patterns(node)
|
73
73
|
|
74
|
-
@valid_ref = regexp_patterns.
|
74
|
+
@valid_ref = regexp_patterns.filter_map { |pattern| check_regexp(pattern) }.max
|
75
75
|
end
|
76
76
|
|
77
77
|
def on_nth_ref(node)
|
@@ -281,7 +281,7 @@ module RuboCop
|
|
281
281
|
.drop_while { |r| !r.equal?(range) }
|
282
282
|
.each_cons(2)
|
283
283
|
.map { |range1, range2| range1.end.join(range2.begin).source }
|
284
|
-
.all?
|
284
|
+
.all?(/\A\s*,\s*\z/)
|
285
285
|
end
|
286
286
|
|
287
287
|
SIMILAR_COP_NAMES_CACHE = Hash.new do |hash, cop_name|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
# Checks for string conversion in string interpolation,
|
6
|
+
# Checks for string conversion in string interpolation, `print`, `puts`, and `warn` arguments,
|
7
7
|
# which is redundant.
|
8
8
|
#
|
9
9
|
# @example
|
@@ -11,18 +11,26 @@ module RuboCop
|
|
11
11
|
# # bad
|
12
12
|
#
|
13
13
|
# "result is #{something.to_s}"
|
14
|
+
# print something.to_s
|
15
|
+
# puts something.to_s
|
16
|
+
# warn something.to_s
|
14
17
|
#
|
15
18
|
# @example
|
16
19
|
#
|
17
20
|
# # good
|
18
21
|
#
|
19
22
|
# "result is #{something}"
|
23
|
+
# print something
|
24
|
+
# puts something
|
25
|
+
# warn something
|
26
|
+
#
|
20
27
|
class RedundantStringCoercion < Base
|
21
28
|
include Interpolation
|
22
29
|
extend AutoCorrector
|
23
30
|
|
24
|
-
MSG_DEFAULT = 'Redundant use of `Object#to_s` in
|
25
|
-
MSG_SELF = 'Use `self` instead of `Object#to_s` in
|
31
|
+
MSG_DEFAULT = 'Redundant use of `Object#to_s` in %<context>s.'
|
32
|
+
MSG_SELF = 'Use `self` instead of `Object#to_s` in %<context>s.'
|
33
|
+
RESTRICT_ON_SEND = %i[print puts warn].freeze
|
26
34
|
|
27
35
|
# @!method to_s_without_args?(node)
|
28
36
|
def_node_matcher :to_s_without_args?, '(send _ :to_s)'
|
@@ -32,18 +40,30 @@ module RuboCop
|
|
32
40
|
|
33
41
|
return unless to_s_without_args?(final_node)
|
34
42
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
register_offense(final_node, 'interpolation')
|
44
|
+
end
|
45
|
+
|
46
|
+
def on_send(node)
|
47
|
+
return if node.receiver
|
48
|
+
|
49
|
+
node.each_child_node(:send) do |child|
|
50
|
+
next if !child.method?(:to_s) || child.arguments.any?
|
51
|
+
|
52
|
+
register_offense(child, "`#{node.method_name}`")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def register_offense(node, context)
|
59
|
+
receiver = node.receiver
|
60
|
+
template = receiver ? MSG_DEFAULT : MSG_SELF
|
61
|
+
message = format(template, context: context)
|
62
|
+
|
63
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
64
|
+
replacement = receiver ? receiver.source : 'self'
|
65
|
+
|
66
|
+
corrector.replace(node, replacement)
|
47
67
|
end
|
48
68
|
end
|
49
69
|
end
|
@@ -3,8 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
7
|
-
# This cop checks for `send`, `public_send`, and `__send__` methods
|
6
|
+
# Checks for `send`, `public_send`, and `__send__` methods
|
8
7
|
# when using mix-in.
|
9
8
|
#
|
10
9
|
# `include` and `prepend` methods were private methods until Ruby 2.0,
|
@@ -121,18 +121,12 @@ module RuboCop
|
|
121
121
|
|
122
122
|
if rescued_exceptions.any?
|
123
123
|
rescued_exceptions.each_with_object([]) do |exception, converted|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
begin
|
128
|
-
RuboCop::Util.silence_warnings do
|
129
|
-
# Avoid printing deprecation warnings about constants
|
130
|
-
converted << Kernel.const_get(exception.source)
|
131
|
-
end
|
132
|
-
rescue NameError
|
133
|
-
converted << nil
|
124
|
+
RuboCop::Util.silence_warnings do
|
125
|
+
# Avoid printing deprecation warnings about constants
|
126
|
+
converted << Kernel.const_get(exception.source)
|
134
127
|
end
|
135
|
-
|
128
|
+
rescue NameError
|
129
|
+
converted << nil
|
136
130
|
end
|
137
131
|
else
|
138
132
|
# treat an empty `rescue` as `rescue StandardError`
|