rubocop 1.72.1 → 1.81.7
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 +22 -18
- data/config/default.yml +240 -65
- data/config/internal_affairs.yml +20 -0
- data/config/obsoletion.yml +8 -3
- data/exe/rubocop +1 -8
- data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
- data/lib/rubocop/cli.rb +19 -4
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +8 -40
- data/lib/rubocop/config_loader_resolver.rb +9 -7
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
- data/lib/rubocop/config_obsoletion.rb +46 -2
- data/lib/rubocop/config_store.rb +5 -0
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/autocorrect_logic.rb +22 -14
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -2
- data/lib/rubocop/cop/gemspec/attribute_assignment.rb +91 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +37 -15
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/require_mfa.rb +15 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +4 -4
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +6 -2
- data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +36 -1
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +32 -14
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +34 -4
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -7
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
- data/lib/rubocop/cop/layout/line_length.rb +43 -10
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +9 -5
- data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +11 -5
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
- data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -38
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +12 -3
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
- data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +5 -2
- data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
- data/lib/rubocop/cop/lint/empty_interpolation.rb +14 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
- data/lib/rubocop/cop/lint/float_comparison.rb +32 -10
- data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
- data/lib/rubocop/cop/lint/literal_as_condition.rb +124 -10
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +17 -8
- data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +43 -13
- data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
- data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +4 -4
- data/lib/rubocop/cop/lint/self_assignment.rb +31 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +30 -4
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -0
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +9 -12
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
- data/lib/rubocop/cop/lint/useless_or.rb +98 -0
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +3 -3
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
- data/lib/rubocop/cop/lint/void.rb +16 -2
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -3
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
- data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
- data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -2
- data/lib/rubocop/cop/mixin/gemspec_help.rb +22 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
- data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
- data/lib/rubocop/cop/mixin/line_length_help.rb +24 -8
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +12 -0
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +18 -2
- data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +2 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +187 -15
- data/lib/rubocop/cop/naming/predicate_method.rb +319 -0
- data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +4 -4
- data/lib/rubocop/cop/naming/variable_name.rb +51 -6
- data/lib/rubocop/cop/registry.rb +9 -6
- data/lib/rubocop/cop/security/eval.rb +2 -1
- data/lib/rubocop/cop/security/json_load.rb +33 -11
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
- data/lib/rubocop/cop/style/arguments_forwarding.rb +21 -24
- data/lib/rubocop/cop/style/array_intersect.rb +113 -38
- data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
- data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +3 -2
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +48 -10
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +1 -0
- data/lib/rubocop/cop/style/collection_querying.rb +167 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +12 -5
- data/lib/rubocop/cop/style/comparable_between.rb +78 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +26 -8
- data/lib/rubocop/cop/style/constant_visibility.rb +14 -9
- data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
- data/lib/rubocop/cop/style/dig_chain.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +3 -3
- data/lib/rubocop/cop/style/empty_literal.rb +4 -0
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
- data/lib/rubocop/cop/style/endless_method.rb +176 -18
- data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
- data/lib/rubocop/cop/style/explicit_block_argument.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +5 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +32 -6
- data/lib/rubocop/cop/style/float_division.rb +15 -1
- data/lib/rubocop/cop/style/for.rb +1 -0
- data/lib/rubocop/cop/style/format_string_token.rb +38 -11
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
- data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
- data/lib/rubocop/cop/style/guard_clause.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +16 -8
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
- 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/identical_conditional_branches.rb +3 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
- data/lib/rubocop/cop/style/if_unless_modifier.rb +35 -8
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +10 -6
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
- data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
- data/lib/rubocop/cop/style/it_assignment.rb +69 -12
- data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/lambda_call.rb +7 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
- data/lib/rubocop/cop/style/map_into_array.rb +4 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +12 -3
- data/lib/rubocop/cop/style/map_to_set.rb +1 -3
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +9 -8
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
- data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
- data/lib/rubocop/cop/style/next.rb +44 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
- data/lib/rubocop/cop/style/object_then.rb +1 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
- data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/raise_args.rb +8 -8
- data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +35 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
- data/lib/rubocop/cop/style/redundant_format.rb +79 -18
- data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +12 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -4
- data/lib/rubocop/cop/style/redundant_parentheses.rb +73 -18
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
- data/lib/rubocop/cop/style/redundant_self.rb +9 -5
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
- data/lib/rubocop/cop/style/return_nil.rb +2 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +61 -14
- data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
- data/lib/rubocop/cop/style/semicolon.rb +23 -7
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +10 -7
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +75 -101
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +18 -15
- data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
- data/lib/rubocop/cop/style/super_arguments.rb +1 -2
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +52 -1
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +10 -9
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +15 -2
- data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +3 -8
- data/lib/rubocop/cop/variable_force.rb +26 -9
- data/lib/rubocop/cops_documentation_generator.rb +23 -7
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/ext/regexp_node.rb +0 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +19 -5
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/pacman_formatter.rb +2 -1
- data/lib/rubocop/lsp/diagnostic.rb +25 -24
- data/lib/rubocop/lsp/routes.rb +65 -9
- data/lib/rubocop/lsp/runtime.rb +5 -5
- data/lib/rubocop/lsp/server.rb +2 -2
- data/lib/rubocop/lsp/stdin_runner.rb +3 -17
- data/lib/rubocop/magic_comment.rb +8 -0
- data/lib/rubocop/pending_cops_reporter.rb +56 -0
- data/lib/rubocop/plugin/configuration_integrator.rb +2 -0
- data/lib/rubocop/plugin/load_error.rb +1 -1
- data/lib/rubocop/plugin.rb +9 -2
- data/lib/rubocop/result_cache.rb +14 -12
- data/lib/rubocop/rspec/cop_helper.rb +6 -1
- data/lib/rubocop/rspec/expect_offense.rb +9 -3
- data/lib/rubocop/rspec/shared_contexts.rb +34 -0
- data/lib/rubocop/rspec/support.rb +3 -0
- data/lib/rubocop/runner.rb +10 -4
- data/lib/rubocop/server/cache.rb +17 -12
- data/lib/rubocop/server/client_command/base.rb +10 -0
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/server/client_command/start.rb +11 -1
- data/lib/rubocop/target_finder.rb +13 -9
- data/lib/rubocop/target_ruby.rb +11 -2
- data/lib/rubocop/version.rb +14 -7
- data/lib/rubocop.rb +17 -2
- data/lib/ruby_lsp/rubocop/addon.rb +25 -10
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +57 -5
- metadata +24 -8
- data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
|
@@ -74,6 +74,7 @@ module RuboCop
|
|
|
74
74
|
check_unused_block_args(node, key, value)
|
|
75
75
|
end
|
|
76
76
|
alias on_numblock on_block
|
|
77
|
+
alias on_itblock on_block
|
|
77
78
|
|
|
78
79
|
# rubocop:disable Metrics/AbcSize
|
|
79
80
|
def check_unused_block_args(node, key, value)
|
|
@@ -128,8 +129,8 @@ module RuboCop
|
|
|
128
129
|
lvar_sources = node.body.each_descendant(:lvar).map(&:source)
|
|
129
130
|
|
|
130
131
|
if block_arg.mlhs_type?
|
|
131
|
-
block_arg.each_descendant(:arg, :restarg).all? do |
|
|
132
|
-
lvar_sources.none?(
|
|
132
|
+
block_arg.each_descendant(:arg, :restarg).all? do |descendant|
|
|
133
|
+
lvar_sources.none?(descendant.source.delete_prefix('*'))
|
|
133
134
|
end
|
|
134
135
|
else
|
|
135
136
|
lvar_sources.none?(block_arg.source.delete_prefix('*'))
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Style
|
|
6
|
+
# Use `Hash#dig` instead of chaining potentially null `fetch` calls.
|
|
7
|
+
#
|
|
8
|
+
# When `fetch(identifier, nil)` calls are chained on a hash, the expectation
|
|
9
|
+
# is that each step in the chain returns either `nil` or another hash,
|
|
10
|
+
# and in both cases, these can be simplified with a single call to `dig` with
|
|
11
|
+
# multiple arguments.
|
|
12
|
+
#
|
|
13
|
+
# If the 2nd parameter is `{}` or `Hash.new`, an offense will also be registered,
|
|
14
|
+
# as long as the final call in the chain is a nil value. If a non-nil value is given,
|
|
15
|
+
# the chain will not be registered as an offense, as the default value cannot be safely
|
|
16
|
+
# given with `dig`.
|
|
17
|
+
#
|
|
18
|
+
# NOTE: See `Style/DigChain` for replacing chains of `dig` calls with
|
|
19
|
+
# a single method call.
|
|
20
|
+
#
|
|
21
|
+
# @safety
|
|
22
|
+
# This cop is unsafe because it cannot be guaranteed that the receiver
|
|
23
|
+
# is a `Hash` or that `fetch` or `dig` have the expected standard implementation.
|
|
24
|
+
#
|
|
25
|
+
# @example
|
|
26
|
+
# # bad
|
|
27
|
+
# hash.fetch('foo', nil)&.fetch('bar', nil)
|
|
28
|
+
#
|
|
29
|
+
# # bad
|
|
30
|
+
# # earlier members of the chain can return `{}` as long as the final `fetch`
|
|
31
|
+
# # has `nil` as a default value
|
|
32
|
+
# hash.fetch('foo', {}).fetch('bar', nil)
|
|
33
|
+
#
|
|
34
|
+
# # good
|
|
35
|
+
# hash.dig('foo', 'bar')
|
|
36
|
+
#
|
|
37
|
+
# # ok - not handled by the cop since the final `fetch` value is non-nil
|
|
38
|
+
# hash.fetch('foo', {}).fetch('bar', {})
|
|
39
|
+
#
|
|
40
|
+
class HashFetchChain < Base
|
|
41
|
+
extend AutoCorrector
|
|
42
|
+
extend TargetRubyVersion
|
|
43
|
+
|
|
44
|
+
MSG = 'Use `%<replacement>s` instead.'
|
|
45
|
+
RESTRICT_ON_SEND = %i[fetch].freeze
|
|
46
|
+
|
|
47
|
+
minimum_target_ruby_version 2.3
|
|
48
|
+
|
|
49
|
+
# @!method diggable?(node)
|
|
50
|
+
def_node_matcher :diggable?, <<~PATTERN
|
|
51
|
+
(call _ :fetch $_arg {nil (hash) (send (const {nil? cbase} :Hash) :new)})
|
|
52
|
+
PATTERN
|
|
53
|
+
|
|
54
|
+
def on_send(node)
|
|
55
|
+
return if ignored_node?(node)
|
|
56
|
+
return if last_fetch_non_nil?(node)
|
|
57
|
+
|
|
58
|
+
last_replaceable_node, arguments = inspect_chain(node)
|
|
59
|
+
return unless last_replaceable_node
|
|
60
|
+
return unless arguments.size > 1
|
|
61
|
+
|
|
62
|
+
range = last_replaceable_node.selector.join(node.loc.end)
|
|
63
|
+
replacement = replacement(arguments)
|
|
64
|
+
message = format(MSG, replacement: replacement)
|
|
65
|
+
|
|
66
|
+
add_offense(range, message: message) do |corrector|
|
|
67
|
+
corrector.replace(range, replacement)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
alias on_csend on_send
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def last_fetch_non_nil?(node)
|
|
75
|
+
# When chaining `fetch` methods, `fetch(x, {})` is acceptable within
|
|
76
|
+
# the chain, as long as the last method in the chain has a `nil`
|
|
77
|
+
# default value.
|
|
78
|
+
|
|
79
|
+
return false unless node.method?(:fetch)
|
|
80
|
+
|
|
81
|
+
!node.last_argument&.nil_type?
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def inspect_chain(node)
|
|
85
|
+
arguments = []
|
|
86
|
+
|
|
87
|
+
while (arg = diggable?(node))
|
|
88
|
+
arguments.unshift(arg)
|
|
89
|
+
ignore_node(node)
|
|
90
|
+
last_replaceable_node = node
|
|
91
|
+
node = node.receiver
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
[last_replaceable_node, arguments]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def replacement(arguments)
|
|
98
|
+
values = arguments.map(&:source).join(', ')
|
|
99
|
+
"dig(#{values})"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -76,6 +76,9 @@ module RuboCop
|
|
|
76
76
|
# # good
|
|
77
77
|
# {foo:, bar:}
|
|
78
78
|
#
|
|
79
|
+
# # good - allowed to mix syntaxes
|
|
80
|
+
# {foo:, bar: baz}
|
|
81
|
+
#
|
|
79
82
|
# @example EnforcedShorthandSyntax: never
|
|
80
83
|
#
|
|
81
84
|
# # bad
|
|
@@ -209,7 +212,7 @@ module RuboCop
|
|
|
209
212
|
end
|
|
210
213
|
|
|
211
214
|
def word_symbol_pair?(pair)
|
|
212
|
-
return false unless pair.key.
|
|
215
|
+
return false unless pair.key.any_sym_type?
|
|
213
216
|
|
|
214
217
|
acceptable_19_syntax_symbol?(pair.key.source)
|
|
215
218
|
end
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
|
-
# Looks for uses of
|
|
7
|
-
#
|
|
6
|
+
# Looks for uses of `+_.each_with_object({}) {...}+`,
|
|
7
|
+
# `+_.map {...}.to_h+`, and `+Hash[_.map {...}]+` that are actually just
|
|
8
8
|
# transforming the keys of a hash, and tries to use a simpler & faster
|
|
9
9
|
# call to `transform_keys` instead.
|
|
10
10
|
# It should only be enabled on Ruby version 2.5 or newer.
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
|
-
# Looks for uses of
|
|
7
|
-
#
|
|
6
|
+
# Looks for uses of `+_.each_with_object({}) {...}+`,
|
|
7
|
+
# `+_.map {...}.to_h+`, and `+Hash[_.map {...}]+` that are actually just
|
|
8
8
|
# transforming the values of a hash, and tries to use a simpler & faster
|
|
9
9
|
# call to `transform_values` instead.
|
|
10
10
|
#
|
|
@@ -189,7 +189,7 @@ module RuboCop
|
|
|
189
189
|
end
|
|
190
190
|
end
|
|
191
191
|
|
|
192
|
-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
192
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
193
193
|
def check_expressions(node, expressions, insert_position)
|
|
194
194
|
return if expressions.any?(&:nil?)
|
|
195
195
|
|
|
@@ -197,7 +197,7 @@ module RuboCop
|
|
|
197
197
|
|
|
198
198
|
expressions.each do |expression|
|
|
199
199
|
add_offense(expression) do |corrector|
|
|
200
|
-
next if node.if_type? && node.ternary?
|
|
200
|
+
next if node.if_type? && (node.ternary? || node.then?)
|
|
201
201
|
|
|
202
202
|
range = range_by_whole_lines(expression.source_range, include_final_newline: true)
|
|
203
203
|
corrector.remove(range)
|
|
@@ -213,7 +213,7 @@ module RuboCop
|
|
|
213
213
|
end
|
|
214
214
|
end
|
|
215
215
|
end
|
|
216
|
-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
216
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
217
217
|
|
|
218
218
|
def correct_assignment(corrector, node, expression, insert_position)
|
|
219
219
|
if insert_position == :after_condition
|
|
@@ -97,34 +97,31 @@ module RuboCop
|
|
|
97
97
|
else
|
|
98
98
|
correct_to_elsif_from_if_inside_else_form(corrector, node, node.condition)
|
|
99
99
|
end
|
|
100
|
-
|
|
101
|
-
corrector.remove(range_by_whole_lines(find_end_range(node), include_final_newline: true))
|
|
102
|
-
return unless (if_branch = node.if_branch)
|
|
103
|
-
|
|
104
|
-
range = range_by_whole_lines(if_branch.source_range, include_final_newline: true)
|
|
105
|
-
corrector.remove(range)
|
|
106
100
|
end
|
|
107
101
|
|
|
108
102
|
def correct_to_elsif_from_modifier_form(corrector, node)
|
|
109
|
-
corrector.replace(node.parent.loc.else,
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
103
|
+
corrector.replace(node.parent.loc.else, "elsif #{node.condition.source}")
|
|
104
|
+
|
|
105
|
+
condition_range = range_between(
|
|
106
|
+
node.if_branch.source_range.end_pos, node.condition.source_range.end_pos
|
|
107
|
+
)
|
|
108
|
+
corrector.remove(condition_range)
|
|
114
109
|
end
|
|
115
110
|
|
|
116
|
-
def correct_to_elsif_from_if_inside_else_form(corrector, node, condition)
|
|
111
|
+
def correct_to_elsif_from_if_inside_else_form(corrector, node, condition) # rubocop:disable Metrics/AbcSize
|
|
117
112
|
corrector.replace(node.parent.loc.else, "elsif #{condition.source}")
|
|
118
113
|
|
|
119
114
|
if_condition_range = if_condition_range(node, condition)
|
|
120
115
|
|
|
121
116
|
if (if_branch = node.if_branch)
|
|
122
|
-
corrector.replace(if_condition_range, if_branch.source)
|
|
117
|
+
corrector.replace(if_condition_range, range_with_comments(if_branch).source)
|
|
118
|
+
corrector.remove(range_with_comments_and_lines(if_branch))
|
|
123
119
|
else
|
|
124
120
|
corrector.remove(range_by_whole_lines(if_condition_range, include_final_newline: true))
|
|
125
121
|
end
|
|
126
122
|
|
|
127
123
|
corrector.remove(condition)
|
|
124
|
+
corrector.remove(range_by_whole_lines(find_end_range(node), include_final_newline: true))
|
|
128
125
|
end
|
|
129
126
|
|
|
130
127
|
def then?(node)
|
|
@@ -23,6 +23,17 @@ module RuboCop
|
|
|
23
23
|
# end
|
|
24
24
|
# ----
|
|
25
25
|
#
|
|
26
|
+
# The code `def method_name = body if condition` is considered a bad case by
|
|
27
|
+
# `Style/AmbiguousEndlessMethodDefinition` cop. So, to respect the user's intention to use
|
|
28
|
+
# an endless method definition in the `if` body, the following code is allowed:
|
|
29
|
+
#
|
|
30
|
+
# [source,ruby]
|
|
31
|
+
# ----
|
|
32
|
+
# if condition
|
|
33
|
+
# def method_name = body
|
|
34
|
+
# end
|
|
35
|
+
# ----
|
|
36
|
+
#
|
|
26
37
|
# NOTE: It is allowed when `defined?` argument has an undefined value,
|
|
27
38
|
# because using the modifier form causes the following incompatibility:
|
|
28
39
|
#
|
|
@@ -77,10 +88,14 @@ module RuboCop
|
|
|
77
88
|
[Style::SoleNestedConditional]
|
|
78
89
|
end
|
|
79
90
|
|
|
91
|
+
# rubocop:disable Metrics/AbcSize
|
|
80
92
|
def on_if(node)
|
|
93
|
+
return if endless_method?(node.body)
|
|
94
|
+
|
|
81
95
|
condition = node.condition
|
|
82
96
|
return if defined_nodes(condition).any? { |n| defined_argument_is_undefined?(node, n) } ||
|
|
83
97
|
pattern_matching_nodes(condition).any?
|
|
98
|
+
|
|
84
99
|
return unless (msg = message(node))
|
|
85
100
|
|
|
86
101
|
add_offense(node.loc.keyword, message: format(msg, keyword: node.keyword)) do |corrector|
|
|
@@ -90,9 +105,14 @@ module RuboCop
|
|
|
90
105
|
ignore_node(node)
|
|
91
106
|
end
|
|
92
107
|
end
|
|
108
|
+
# rubocop:enable Metrics/AbcSize
|
|
93
109
|
|
|
94
110
|
private
|
|
95
111
|
|
|
112
|
+
def endless_method?(body)
|
|
113
|
+
body&.any_def_type? && body.endless?
|
|
114
|
+
end
|
|
115
|
+
|
|
96
116
|
def defined_nodes(condition)
|
|
97
117
|
if condition.defined_type?
|
|
98
118
|
[condition]
|
|
@@ -112,12 +132,10 @@ module RuboCop
|
|
|
112
132
|
end
|
|
113
133
|
|
|
114
134
|
def pattern_matching_nodes(condition)
|
|
115
|
-
if condition.
|
|
135
|
+
if condition.any_match_pattern_type?
|
|
116
136
|
[condition]
|
|
117
137
|
else
|
|
118
|
-
condition.each_descendant.select
|
|
119
|
-
node.type?(:match_pattern, :match_pattern_p)
|
|
120
|
-
end
|
|
138
|
+
condition.each_descendant.select(&:any_match_pattern_type?)
|
|
121
139
|
end
|
|
122
140
|
end
|
|
123
141
|
|
|
@@ -164,8 +182,8 @@ module RuboCop
|
|
|
164
182
|
|
|
165
183
|
def too_long_due_to_comment_after_modifier?(node, comment)
|
|
166
184
|
source_length = processed_source.lines[node.first_line - 1].length
|
|
167
|
-
|
|
168
|
-
|
|
185
|
+
|
|
186
|
+
max_line_length.between?(source_length - comment.source_range.length, source_length)
|
|
169
187
|
end
|
|
170
188
|
|
|
171
189
|
def allowed_patterns
|
|
@@ -205,8 +223,17 @@ module RuboCop
|
|
|
205
223
|
|
|
206
224
|
def too_long_line_based_on_allow_uri?(line)
|
|
207
225
|
if allow_uri?
|
|
208
|
-
uri_range =
|
|
209
|
-
return false if uri_range &&
|
|
226
|
+
uri_range = find_excessive_range(line, :uri)
|
|
227
|
+
return false if uri_range && allowed_position?(line, uri_range)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
true
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def too_long_line_based_on_allow_qualified_name?(line)
|
|
234
|
+
if allow_qualified_name?
|
|
235
|
+
namespace_range = find_excessive_range(line, :namespace)
|
|
236
|
+
return false if namespace_range && allowed_position?(line, namespace_range)
|
|
210
237
|
end
|
|
211
238
|
|
|
212
239
|
true
|
|
@@ -28,19 +28,16 @@ module RuboCop
|
|
|
28
28
|
|
|
29
29
|
MSG = 'Avoid modifier `%<keyword>s` after another conditional.'
|
|
30
30
|
|
|
31
|
+
# rubocop:disable Metrics/AbcSize
|
|
31
32
|
def on_if(node)
|
|
32
33
|
return unless node.modifier_form? && node.body.if_type?
|
|
33
34
|
|
|
34
35
|
add_offense(node.loc.keyword, message: format(MSG, keyword: node.keyword)) do |corrector|
|
|
35
|
-
keyword
|
|
36
|
-
|
|
37
|
-
corrector.replace(node, <<~RUBY.chop)
|
|
38
|
-
#{keyword} #{node.condition.source}
|
|
39
|
-
#{node.if_branch.source}
|
|
40
|
-
end
|
|
41
|
-
RUBY
|
|
36
|
+
corrector.wrap(node.if_branch, "#{node.keyword} #{node.condition.source}\n", "\nend")
|
|
37
|
+
corrector.remove(node.if_branch.source_range.end.join(node.condition.source_range.end))
|
|
42
38
|
end
|
|
43
39
|
end
|
|
40
|
+
# rubocop:enable Metrics/AbcSize
|
|
44
41
|
end
|
|
45
42
|
end
|
|
46
43
|
end
|
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
|
10
10
|
# `nonzero?` method is allowed by default.
|
|
11
11
|
# These are customizable with `AllowedMethods` option.
|
|
12
12
|
#
|
|
13
|
-
# This cop targets only
|
|
13
|
+
# This cop targets only ``if``s with a single `elsif` or `else` branch. The following
|
|
14
14
|
# code will be allowed, because it has two `elsif` branches:
|
|
15
15
|
#
|
|
16
16
|
# [source,ruby]
|
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
def autocorrect(corrector, node)
|
|
71
|
-
if node.
|
|
71
|
+
if node.post_condition_loop?
|
|
72
72
|
replace_begin_end_with_modifier(corrector, node)
|
|
73
73
|
elsif node.modifier_form?
|
|
74
74
|
replace_source(corrector, node.source_range, modifier_replacement(node))
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
|
-
#
|
|
6
|
+
# Checks for usages of not (`not` or `!`) called on a method
|
|
7
7
|
# when an inverse of that method can be used instead.
|
|
8
8
|
#
|
|
9
9
|
# Methods that can be inverted by a not (`not` or `!`) should be defined
|
|
@@ -46,6 +46,7 @@ module RuboCop
|
|
|
46
46
|
|
|
47
47
|
MSG = 'Use `%<inverse>s` instead of inverting `%<method>s`.'
|
|
48
48
|
CLASS_COMPARISON_METHODS = %i[<= >= < >].freeze
|
|
49
|
+
SAFE_NAVIGATION_INCOMPATIBLE_METHODS = (CLASS_COMPARISON_METHODS + %i[any? none?]).freeze
|
|
49
50
|
EQUALITY_METHODS = %i[== != =~ !~ <= >= < >].freeze
|
|
50
51
|
NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
|
|
51
52
|
CAMEL_CASE = /[A-Z]+[a-z]+/.freeze
|
|
@@ -77,7 +78,7 @@ module RuboCop
|
|
|
77
78
|
def on_send(node)
|
|
78
79
|
inverse_candidate?(node) do |method_call, lhs, method, rhs|
|
|
79
80
|
return unless inverse_methods.key?(method)
|
|
80
|
-
return if negated?(node) ||
|
|
81
|
+
return if negated?(node) || safe_navigation_incompatible?(method_call)
|
|
81
82
|
return if part_of_ignored_node?(node)
|
|
82
83
|
return if possible_class_hierarchy_check?(lhs, rhs, method)
|
|
83
84
|
|
|
@@ -105,6 +106,7 @@ module RuboCop
|
|
|
105
106
|
end
|
|
106
107
|
|
|
107
108
|
alias on_numblock on_block
|
|
109
|
+
alias on_itblock on_block
|
|
108
110
|
|
|
109
111
|
private
|
|
110
112
|
|
|
@@ -154,10 +156,6 @@ module RuboCop
|
|
|
154
156
|
node.parent.respond_to?(:method?) && node.parent.method?(:!)
|
|
155
157
|
end
|
|
156
158
|
|
|
157
|
-
def relational_comparison_with_safe_navigation?(node)
|
|
158
|
-
node.csend_type? && CLASS_COMPARISON_METHODS.include?(node.method_name)
|
|
159
|
-
end
|
|
160
|
-
|
|
161
159
|
def not_to_receiver(node, method_call)
|
|
162
160
|
node.loc.selector.begin.join(method_call.source_range.begin)
|
|
163
161
|
end
|
|
@@ -166,6 +164,12 @@ module RuboCop
|
|
|
166
164
|
method_call.source_range.end.join(node.source_range.end)
|
|
167
165
|
end
|
|
168
166
|
|
|
167
|
+
def safe_navigation_incompatible?(node)
|
|
168
|
+
return false unless node.csend_type?
|
|
169
|
+
|
|
170
|
+
SAFE_NAVIGATION_INCOMPATIBLE_METHODS.include?(node.method_name)
|
|
171
|
+
end
|
|
172
|
+
|
|
169
173
|
# When comparing classes, `!(Integer < Numeric)` is not the same as
|
|
170
174
|
# `Integer > Numeric`.
|
|
171
175
|
def possible_class_hierarchy_check?(lhs, rhs, method)
|
|
@@ -89,8 +89,8 @@ module RuboCop
|
|
|
89
89
|
|
|
90
90
|
def inheritance_check?(node)
|
|
91
91
|
argument = node.first_argument
|
|
92
|
-
node.method?(:<) &&
|
|
93
|
-
|
|
92
|
+
node.method?(:<) && argument.const_type? &&
|
|
93
|
+
argument.short_name.to_s.upcase != argument.short_name.to_s
|
|
94
94
|
end
|
|
95
95
|
|
|
96
96
|
def preferred_condition(node)
|
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
|
32
32
|
|
|
33
33
|
# To try to avoid doing two regex checks on every string,
|
|
34
34
|
# shortcut out if the string does not look like an IP address
|
|
35
|
-
return false unless
|
|
35
|
+
return false unless potential_ip?(contents)
|
|
36
36
|
|
|
37
37
|
::Resolv::IPv4::Regex.match?(contents) || ::Resolv::IPv6::Regex.match?(contents)
|
|
38
38
|
end
|
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
|
52
52
|
Array(allowed_addresses).map(&:downcase)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def
|
|
55
|
+
def potential_ip?(str)
|
|
56
56
|
# If the string is too long, it can't be an IP
|
|
57
57
|
return false if too_long?(str)
|
|
58
58
|
|
|
@@ -3,33 +3,90 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
|
-
# Checks for
|
|
6
|
+
# Checks for local variables and method parameters named `it`,
|
|
7
7
|
# where `it` can refer to the first anonymous parameter as of Ruby 3.4.
|
|
8
|
+
# Use a meaningful variable name instead.
|
|
8
9
|
#
|
|
9
|
-
# Although Ruby allows reassigning `it` in these cases, it could
|
|
10
|
+
# NOTE: Although Ruby allows reassigning `it` in these cases, it could
|
|
10
11
|
# cause confusion if `it` is used as a block parameter elsewhere.
|
|
11
|
-
# For consistency, this also applies to numblocks and blocks with
|
|
12
|
-
# parameters, even though `it` cannot be used in those cases.
|
|
13
12
|
#
|
|
14
13
|
# @example
|
|
15
14
|
# # bad
|
|
16
|
-
#
|
|
17
|
-
# foo { |bar| it = bar }
|
|
18
|
-
# foo { it = _2 }
|
|
15
|
+
# it = 5
|
|
19
16
|
#
|
|
20
|
-
# # good
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
17
|
+
# # good
|
|
18
|
+
# var = 5
|
|
19
|
+
#
|
|
20
|
+
# # bad
|
|
21
|
+
# def foo(it)
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# # good
|
|
25
|
+
# def foo(arg)
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# # bad
|
|
29
|
+
# def foo(it = 5)
|
|
30
|
+
# end
|
|
31
|
+
#
|
|
32
|
+
# # good
|
|
33
|
+
# def foo(arg = 5)
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# # bad
|
|
37
|
+
# def foo(*it)
|
|
38
|
+
# end
|
|
39
|
+
#
|
|
40
|
+
# # good
|
|
41
|
+
# def foo(*args)
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# # bad
|
|
45
|
+
# def foo(it:)
|
|
46
|
+
# end
|
|
47
|
+
#
|
|
48
|
+
# # good
|
|
49
|
+
# def foo(arg:)
|
|
50
|
+
# end
|
|
51
|
+
#
|
|
52
|
+
# # bad
|
|
53
|
+
# def foo(it: 5)
|
|
54
|
+
# end
|
|
55
|
+
#
|
|
56
|
+
# # good
|
|
57
|
+
# def foo(arg: 5)
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# # bad
|
|
61
|
+
# def foo(**it)
|
|
62
|
+
# end
|
|
63
|
+
#
|
|
64
|
+
# # good
|
|
65
|
+
# def foo(**kwargs)
|
|
66
|
+
# end
|
|
67
|
+
#
|
|
68
|
+
# # bad
|
|
69
|
+
# def foo(&it)
|
|
70
|
+
# end
|
|
71
|
+
#
|
|
72
|
+
# # good
|
|
73
|
+
# def foo(&block)
|
|
74
|
+
# end
|
|
24
75
|
class ItAssignment < Base
|
|
25
76
|
MSG = '`it` is the default block parameter; consider another name.'
|
|
26
77
|
|
|
27
78
|
def on_lvasgn(node)
|
|
28
79
|
return unless node.name == :it
|
|
29
|
-
return unless node.each_ancestor(:any_block).any?
|
|
30
80
|
|
|
31
81
|
add_offense(node.loc.name)
|
|
32
82
|
end
|
|
83
|
+
alias on_arg on_lvasgn
|
|
84
|
+
alias on_optarg on_lvasgn
|
|
85
|
+
alias on_restarg on_lvasgn
|
|
86
|
+
alias on_blockarg on_lvasgn
|
|
87
|
+
alias on_kwarg on_lvasgn
|
|
88
|
+
alias on_kwoptarg on_lvasgn
|
|
89
|
+
alias on_kwrestarg on_lvasgn
|
|
33
90
|
end
|
|
34
91
|
end
|
|
35
92
|
end
|