rubocop 1.69.0 → 1.79.2
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 +23 -19
- data/config/default.yml +290 -65
- data/config/internal_affairs.yml +20 -0
- data/config/obsoletion.yml +8 -3
- data/lib/rubocop/cli/command/execute_runner.rb +3 -3
- data/lib/rubocop/cli/command/show_cops.rb +24 -2
- data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
- data/lib/rubocop/cli.rb +13 -2
- data/lib/rubocop/comment_config.rb +2 -2
- data/lib/rubocop/config.rb +52 -10
- data/lib/rubocop/config_loader.rb +53 -47
- data/lib/rubocop/config_loader_resolver.rb +36 -10
- 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_validator.rb +25 -14
- data/lib/rubocop/cop/autocorrect_logic.rb +44 -39
- data/lib/rubocop/cop/base.rb +6 -0
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- 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/generator.rb +6 -0
- data/lib/rubocop/cop/internal_affairs/cop_enabled.rb +85 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
- data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
- data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +63 -0
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +131 -0
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +231 -0
- data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
- data/lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb +126 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +4 -3
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
- data/lib/rubocop/cop/internal_affairs/operator_keyword.rb +4 -2
- data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +13 -2
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +6 -16
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/argument_alignment.rb +2 -8
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +44 -9
- 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 +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +7 -11
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +35 -4
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
- data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +5 -6
- 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/empty_lines_around_exception_handling_keywords.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -9
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -7
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -7
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -6
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -1
- 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_continuation_leading_space.rb +11 -2
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +7 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/line_length.rb +35 -9
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +25 -0
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +4 -4
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +16 -11
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -5
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
- data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +11 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +7 -1
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +15 -4
- 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_before_comma.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
- 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 +5 -3
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
- data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +118 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -3
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +4 -3
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
- data/lib/rubocop/cop/lint/constant_reassignment.rb +148 -0
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
- data/lib/rubocop/cop/lint/debugger.rb +3 -3
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_set_element.rb +20 -7
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
- data/lib/rubocop/cop/lint/empty_expression.rb +0 -2
- data/lib/rubocop/cop/lint/empty_interpolation.rb +3 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
- data/lib/rubocop/cop/lint/float_comparison.rb +37 -12
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
- data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/literal_as_condition.rb +125 -10
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +24 -6
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/missing_super.rb +2 -2
- data/lib/rubocop/cop/lint/mixed_case_range.rb +3 -3
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +9 -5
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +4 -2
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -3
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +19 -31
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
- data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +2 -2
- 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_string_coercion.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +261 -0
- 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/refinement_import_methods.rb +1 -1
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- 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 +8 -1
- data/lib/rubocop/cop/lint/self_assignment.rb +25 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +76 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +4 -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/unescaped_bracket_in_regexp.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +3 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +52 -2
- data/lib/rubocop/cop/lint/unreachable_loop.rb +6 -6
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +34 -8
- data/lib/rubocop/cop/lint/useless_assignment.rb +3 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +71 -0
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +4 -0
- data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +3 -1
- 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 +5 -5
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
- data/lib/rubocop/cop/lint/void.rb +14 -11
- 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/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +9 -9
- data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +9 -1
- data/lib/rubocop/cop/metrics/module_length.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
- data/lib/rubocop/cop/mixin/alignment.rb +3 -3
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +13 -13
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
- data/lib/rubocop/cop/mixin/comments_help.rb +8 -3
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/dig_help.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
- 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_shorthand_syntax.rb +22 -22
- data/lib/rubocop/cop/mixin/hash_subset.rb +203 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
- data/lib/rubocop/cop/mixin/line_length_help.rb +27 -10
- data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
- 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/percent_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +68 -30
- data/lib/rubocop/cop/mixin/range_help.rb +15 -3
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +8 -3
- data/lib/rubocop/cop/mixin/string_help.rb +2 -2
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +21 -5
- data/lib/rubocop/cop/naming/accessor_method_name.rb +6 -6
- data/lib/rubocop/cop/naming/block_forwarding.rb +19 -15
- 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 +185 -15
- data/lib/rubocop/cop/naming/predicate_method.rb +306 -0
- data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +48 -4
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.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/compound_hash.rb +2 -0
- data/lib/rubocop/cop/security/eval.rb +2 -1
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/security/yaml_load.rb +3 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +66 -15
- data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
- data/lib/rubocop/cop/style/and_or.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +57 -44
- data/lib/rubocop/cop/style/array_first_last.rb +18 -2
- data/lib/rubocop/cop/style/array_intersect.rb +81 -40
- data/lib/rubocop/cop/style/block_delimiters.rb +27 -24
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +52 -11
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +2 -1
- data/lib/rubocop/cop/style/collection_querying.rb +167 -0
- data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
- 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/concat_array_literals.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +23 -7
- 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 +6 -7
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +4 -4
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +4 -7
- data/lib/rubocop/cop/style/each_with_object.rb +2 -3
- data/lib/rubocop/cop/style/empty_else.rb +4 -2
- data/lib/rubocop/cop/style/empty_literal.rb +5 -1
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
- data/lib/rubocop/cop/style/endless_method.rb +163 -18
- data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
- data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -3
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
- data/lib/rubocop/cop/style/explicit_block_argument.rb +16 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -5
- data/lib/rubocop/cop/style/fetch_env_var.rb +34 -7
- data/lib/rubocop/cop/style/file_null.rb +20 -4
- data/lib/rubocop/cop/style/float_division.rb +8 -4
- 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 +6 -8
- data/lib/rubocop/cop/style/hash_except.rb +35 -147
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
- data/lib/rubocop/cop/style/hash_slice.rb +80 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +9 -3
- 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 +25 -6
- data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
- data/lib/rubocop/cop/style/if_unless_modifier.rb +36 -9
- 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 +2 -2
- data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +16 -12
- 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 +93 -0
- data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +14 -8
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/lambda_call.rb +10 -3
- data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
- data/lib/rubocop/cop/style/map_into_array.rb +5 -2
- data/lib/rubocop/cop/style/map_to_hash.rb +13 -4
- data/lib/rubocop/cop/style/map_to_set.rb +4 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +27 -19
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +18 -0
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +3 -2
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
- data/lib/rubocop/cop/style/missing_else.rb +2 -0
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -2
- 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/multiple_comparison.rb +34 -22
- data/lib/rubocop/cop/style/mutable_constant.rb +3 -3
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
- data/lib/rubocop/cop/style/next.rb +44 -0
- data/lib/rubocop/cop/style/object_then.rb +15 -15
- data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
- data/lib/rubocop/cop/style/parallel_assignment.rb +33 -25
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +2 -2
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +14 -12
- data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
- data/lib/rubocop/cop/style/redundant_argument.rb +3 -1
- data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +59 -2
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +16 -5
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
- data/lib/rubocop/cop/style/redundant_each.rb +1 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
- data/lib/rubocop/cop/style/redundant_format.rb +262 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +4 -4
- data/lib/rubocop/cop/style/redundant_initialize.rb +12 -3
- data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +39 -22
- data/lib/rubocop/cop/style/redundant_parentheses.rb +85 -17
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +3 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +10 -6
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +14 -28
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
- 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 +46 -16
- data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +2 -1
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +4 -3
- data/lib/rubocop/cop/style/single_line_methods.rb +13 -11
- data/lib/rubocop/cop/style/slicing_with_range.rb +40 -11
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +68 -101
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +15 -14
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
- data/lib/rubocop/cop/style/super_arguments.rb +66 -19
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +11 -2
- 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/while_until_modifier.rb +0 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +8 -4
- data/lib/rubocop/cop/style/yoda_expression.rb +2 -1
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +12 -5
- data/lib/rubocop/cop/utils/format_string.rb +10 -5
- 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 +10 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
- data/lib/rubocop/cop/variable_force.rb +23 -8
- data/lib/rubocop/cops_documentation_generator.rb +32 -16
- data/lib/rubocop/directive_comment.rb +45 -11
- data/lib/rubocop/ext/regexp_node.rb +0 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- 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 +189 -0
- data/lib/rubocop/lsp/logger.rb +2 -2
- data/lib/rubocop/lsp/routes.rb +10 -26
- data/lib/rubocop/lsp/runtime.rb +18 -50
- data/lib/rubocop/lsp/server.rb +0 -2
- data/lib/rubocop/lsp/stdin_runner.rb +85 -0
- data/lib/rubocop/magic_comment.rb +11 -3
- data/lib/rubocop/options.rb +28 -12
- data/lib/rubocop/path_util.rb +15 -8
- data/lib/rubocop/pending_cops_reporter.rb +56 -0
- data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
- data/lib/rubocop/plugin/load_error.rb +26 -0
- data/lib/rubocop/plugin/loader.rb +100 -0
- data/lib/rubocop/plugin/not_supported_error.rb +29 -0
- data/lib/rubocop/plugin.rb +46 -0
- data/lib/rubocop/rake_task.rb +4 -1
- data/lib/rubocop/result_cache.rb +26 -24
- data/lib/rubocop/rspec/cop_helper.rb +13 -1
- data/lib/rubocop/rspec/expect_offense.rb +15 -5
- data/lib/rubocop/rspec/shared_contexts.rb +38 -1
- data/lib/rubocop/rspec/support.rb +4 -2
- data/lib/rubocop/runner.rb +10 -7
- data/lib/rubocop/server/cache.rb +51 -13
- data/lib/rubocop/server/cli.rb +2 -2
- 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 +7 -2
- data/lib/rubocop/target_ruby.rb +16 -1
- data/lib/rubocop/version.rb +30 -8
- data/lib/rubocop.rb +27 -2
- data/lib/ruby_lsp/rubocop/addon.rb +75 -0
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +65 -0
- metadata +72 -19
- data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -16,21 +16,35 @@ module RuboCop
|
|
16
16
|
#
|
17
17
|
# In Ruby 3.2, anonymous args/kwargs forwarding has been added.
|
18
18
|
#
|
19
|
-
# This cop also identifies places where
|
20
|
-
# replaced by
|
21
|
-
# by setting `UseAnonymousForwarding: false`.
|
19
|
+
# This cop also identifies places where `+use_args(*args)+`/`+use_kwargs(**kwargs)+` can be
|
20
|
+
# replaced by `+use_args(*)+`/`+use_kwargs(**)+`; if desired, this functionality can be
|
21
|
+
# disabled by setting `UseAnonymousForwarding: false`.
|
22
22
|
#
|
23
23
|
# And this cop has `RedundantRestArgumentNames`, `RedundantKeywordRestArgumentNames`,
|
24
24
|
# and `RedundantBlockArgumentNames` options. This configuration is a list of redundant names
|
25
25
|
# that are sufficient for anonymizing meaningless naming.
|
26
26
|
#
|
27
27
|
# Meaningless names that are commonly used can be anonymized by default:
|
28
|
-
# e.g.,
|
28
|
+
# e.g., `+*args+`, `+**options+`, `&block`, and so on.
|
29
29
|
#
|
30
30
|
# Names not on this list are likely to be meaningful and are allowed by default.
|
31
31
|
#
|
32
32
|
# This cop handles not only method forwarding but also forwarding to `super`.
|
33
33
|
#
|
34
|
+
# [NOTE]
|
35
|
+
# ====
|
36
|
+
# Because of a bug in Ruby 3.3.0, when a block is referenced inside of another block,
|
37
|
+
# no offense will be registered until Ruby 3.4:
|
38
|
+
#
|
39
|
+
# [source,ruby]
|
40
|
+
# ----
|
41
|
+
# def foo(&block)
|
42
|
+
# # Using an anonymous block would be a syntax error on Ruby 3.3.0
|
43
|
+
# block_method { bar(&block) }
|
44
|
+
# end
|
45
|
+
# ----
|
46
|
+
# ====
|
47
|
+
#
|
34
48
|
# @example
|
35
49
|
# # bad
|
36
50
|
# def foo(*args, &block)
|
@@ -132,7 +146,6 @@ module RuboCop
|
|
132
146
|
minimum_target_ruby_version 2.7
|
133
147
|
|
134
148
|
FORWARDING_LVAR_TYPES = %i[splat kwsplat block_pass].freeze
|
135
|
-
ADDITIONAL_ARG_TYPES = %i[lvar arg].freeze
|
136
149
|
|
137
150
|
FORWARDING_MSG = 'Use shorthand syntax `...` for arguments forwarding.'
|
138
151
|
ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).'
|
@@ -148,7 +161,7 @@ module RuboCop
|
|
148
161
|
|
149
162
|
restarg, kwrestarg, blockarg = extract_forwardable_args(node.arguments)
|
150
163
|
forwardable_args = redundant_forwardable_named_args(restarg, kwrestarg, blockarg)
|
151
|
-
send_nodes = node.each_descendant(:
|
164
|
+
send_nodes = node.each_descendant(:call, :super, :yield).to_a
|
152
165
|
|
153
166
|
send_classifications = classify_send_nodes(
|
154
167
|
node, send_nodes, non_splat_or_block_pass_lvar_references(node.body), forwardable_args
|
@@ -184,54 +197,54 @@ module RuboCop
|
|
184
197
|
send_classifications.all? { |_, c, _, _| all_classifications.include?(c) }
|
185
198
|
end
|
186
199
|
|
187
|
-
# rubocop:disable Metrics/MethodLength
|
200
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
188
201
|
def add_forward_all_offenses(node, send_classifications, forwardable_args)
|
189
|
-
|
202
|
+
rest_arg, kwrest_arg, block_arg = *forwardable_args
|
190
203
|
registered_block_arg_offense = false
|
191
204
|
|
192
205
|
send_classifications.each do |send_node, c, forward_rest, forward_kwrest, forward_block_arg| # rubocop:disable Layout/LineLength
|
193
206
|
if !forward_rest && !forward_kwrest && c != :all_anonymous
|
194
|
-
|
195
|
-
# in Ruby 3.3.0.
|
196
|
-
if outside_block?(forward_block_arg)
|
207
|
+
if allow_anonymous_forwarding_in_block?(forward_block_arg)
|
197
208
|
register_forward_block_arg_offense(!forward_rest, node.arguments, block_arg)
|
198
209
|
register_forward_block_arg_offense(!forward_rest, send_node, forward_block_arg)
|
199
210
|
end
|
200
211
|
registered_block_arg_offense = true
|
201
212
|
break
|
202
213
|
else
|
203
|
-
|
214
|
+
first_arg = forward_rest || forward_kwrest || forward_all_first_argument(send_node)
|
215
|
+
register_forward_all_offense(send_node, send_node, first_arg)
|
204
216
|
end
|
205
217
|
end
|
206
218
|
|
207
219
|
return if registered_block_arg_offense
|
208
220
|
|
209
|
-
|
210
|
-
|
221
|
+
register_forward_all_offense(node, node.arguments, rest_arg || kwrest_arg)
|
222
|
+
end
|
223
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
224
|
+
|
225
|
+
def forward_all_first_argument(node)
|
226
|
+
node.arguments.reverse_each.find(&:forwarded_restarg_type?)
|
211
227
|
end
|
212
|
-
# rubocop:enable Metrics/MethodLength
|
213
228
|
|
214
229
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
215
230
|
def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
|
216
231
|
return unless use_anonymous_forwarding?
|
217
|
-
return
|
232
|
+
return unless all_forwarding_offenses_correctable?(send_classifications)
|
218
233
|
|
219
234
|
rest_arg, kwrest_arg, block_arg = *forwardable_args
|
220
235
|
|
221
236
|
send_classifications.each do |send_node, _c, forward_rest, forward_kwrest, forward_block_arg| # rubocop:disable Layout/LineLength
|
222
|
-
if
|
237
|
+
if allow_anonymous_forwarding_in_block?(forward_rest)
|
223
238
|
register_forward_args_offense(def_node.arguments, rest_arg)
|
224
239
|
register_forward_args_offense(send_node, forward_rest)
|
225
240
|
end
|
226
241
|
|
227
|
-
if
|
242
|
+
if allow_anonymous_forwarding_in_block?(forward_kwrest)
|
228
243
|
register_forward_kwargs_offense(!forward_rest, def_node.arguments, kwrest_arg)
|
229
244
|
register_forward_kwargs_offense(!forward_rest, send_node, forward_kwrest)
|
230
245
|
end
|
231
246
|
|
232
|
-
|
233
|
-
# in Ruby 3.3.0.
|
234
|
-
if outside_block?(forward_block_arg)
|
247
|
+
if allow_anonymous_forwarding_in_block?(forward_block_arg)
|
235
248
|
register_forward_block_arg_offense(!forward_rest, def_node.arguments, block_arg)
|
236
249
|
register_forward_block_arg_offense(!forward_rest, send_node, forward_block_arg)
|
237
250
|
end
|
@@ -293,10 +306,25 @@ module RuboCop
|
|
293
306
|
redundant_arg_names.include?(arg.source) ? arg : nil
|
294
307
|
end
|
295
308
|
|
296
|
-
|
309
|
+
# Checks if forwarding is uses both in blocks and outside of blocks.
|
310
|
+
# On Ruby 3.3.0, anonymous block forwarding in blocks can be is a syntax
|
311
|
+
# error, so we only want to register an offense if we can change all occurrences.
|
312
|
+
def all_forwarding_offenses_correctable?(send_classifications)
|
313
|
+
return true if target_ruby_version >= 3.4
|
314
|
+
|
315
|
+
send_classifications.none? do |send_node, *|
|
316
|
+
send_node.each_ancestor(:any_block).any?
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
# Ruby 3.3.0 had a bug where accessing an anonymous block argument inside of a block
|
321
|
+
# was a syntax error in unambiguous cases: https://bugs.ruby-lang.org/issues/20090
|
322
|
+
# We disallow this also for earlier Ruby versions so that code is forwards compatible.
|
323
|
+
def allow_anonymous_forwarding_in_block?(node)
|
297
324
|
return false unless node
|
325
|
+
return true if target_ruby_version >= 3.4
|
298
326
|
|
299
|
-
node.each_ancestor(:
|
327
|
+
node.each_ancestor(:any_block).none?
|
300
328
|
end
|
301
329
|
|
302
330
|
def register_forward_args_offense(def_arguments_or_send, rest_arg_or_splat)
|
@@ -336,18 +364,9 @@ module RuboCop
|
|
336
364
|
end
|
337
365
|
end
|
338
366
|
|
339
|
-
# rubocop:disable Metrics/AbcSize
|
340
367
|
def arguments_range(node, first_node)
|
341
|
-
|
342
|
-
next true if ADDITIONAL_ARG_TYPES.include?(arg.type) || arg.variable? || arg.call_type?
|
343
|
-
|
344
|
-
arg.literal? && arg.each_descendant(:kwsplat).none?
|
345
|
-
end
|
346
|
-
|
347
|
-
start_node = first_node || arguments.first
|
348
|
-
start_node.source_range.begin.join(arguments.last.source_range.end)
|
368
|
+
first_node.source_range.begin.join(node.last_argument.source_range.end)
|
349
369
|
end
|
350
|
-
# rubocop:enable Metrics/AbcSize
|
351
370
|
|
352
371
|
def allow_only_rest_arguments?
|
353
372
|
cop_config.fetch('AllowOnlyRestArgument', true)
|
@@ -357,12 +376,6 @@ module RuboCop
|
|
357
376
|
cop_config.fetch('UseAnonymousForwarding', false)
|
358
377
|
end
|
359
378
|
|
360
|
-
def send_inside_block?(send_classifications)
|
361
|
-
send_classifications.any? do |send_node, *|
|
362
|
-
send_node.each_ancestor(:block, :numblock).any?
|
363
|
-
end
|
364
|
-
end
|
365
|
-
|
366
379
|
def add_parens_if_missing(node, corrector)
|
367
380
|
return if parentheses?(node)
|
368
381
|
return if node.send_type? && node.method?(:[])
|
@@ -460,6 +473,9 @@ module RuboCop
|
|
460
473
|
end
|
461
474
|
|
462
475
|
def ruby_32_only_anonymous_forwarding?
|
476
|
+
# A block argument and an anonymous block argument are never passed together.
|
477
|
+
return false if @send_node.each_ancestor(:any_block).any?
|
478
|
+
|
463
479
|
def_all_anonymous_args?(@def_node) && send_all_anonymous_args?(@send_node)
|
464
480
|
end
|
465
481
|
|
@@ -511,7 +527,7 @@ module RuboCop
|
|
511
527
|
end
|
512
528
|
|
513
529
|
def additional_kwargs?
|
514
|
-
@def_node.arguments.any? { |a| a.
|
530
|
+
@def_node.arguments.any? { |a| a.type?(:kwarg, :kwoptarg) }
|
515
531
|
end
|
516
532
|
|
517
533
|
def forward_additional_kwargs?
|
@@ -540,10 +556,7 @@ module RuboCop
|
|
540
556
|
end
|
541
557
|
|
542
558
|
def explicit_block_name?
|
543
|
-
|
544
|
-
return false unless block_forwarding_config['Enabled']
|
545
|
-
|
546
|
-
block_forwarding_config['EnforcedStyle'] == 'explicit'
|
559
|
+
config.for_enabled_cop('Naming/BlockForwarding')['EnforcedStyle'] == 'explicit'
|
547
560
|
end
|
548
561
|
end
|
549
562
|
end
|
@@ -42,14 +42,30 @@ module RuboCop
|
|
42
42
|
return if node.parent && brace_method?(node.parent)
|
43
43
|
|
44
44
|
preferred = (value.zero? ? 'first' : 'last')
|
45
|
-
|
46
|
-
|
45
|
+
offense_range = find_offense_range(node)
|
46
|
+
|
47
|
+
add_offense(offense_range, message: format(MSG, preferred: preferred)) do |corrector|
|
48
|
+
corrector.replace(offense_range, preferred_value(node, preferred))
|
47
49
|
end
|
48
50
|
end
|
49
51
|
# rubocop:enable Metrics/AbcSize
|
52
|
+
alias on_csend on_send
|
50
53
|
|
51
54
|
private
|
52
55
|
|
56
|
+
def preferred_value(node, value)
|
57
|
+
value = ".#{value}" unless node.loc.dot
|
58
|
+
value
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_offense_range(node)
|
62
|
+
if node.loc.dot
|
63
|
+
node.loc.selector.join(node.source_range.end)
|
64
|
+
else
|
65
|
+
node.loc.selector
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
53
69
|
def innermost_braces_node(node)
|
54
70
|
node = node.receiver while node.receiver.send_type? && node.receiver.method?(:[])
|
55
71
|
node
|
@@ -5,11 +5,15 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# In Ruby 3.1, `Array#intersect?` has been added.
|
7
7
|
#
|
8
|
-
# This cop identifies places where
|
9
|
-
# can be replaced by `array1.intersect?(array2)`.
|
8
|
+
# This cop identifies places where:
|
10
9
|
#
|
11
|
-
#
|
12
|
-
# `(array1
|
10
|
+
# * `(array1 & array2).any?`
|
11
|
+
# * `(array1.intersection(array2)).any?`
|
12
|
+
# * `array1.any? { |elem| array2.member?(elem) }`
|
13
|
+
#
|
14
|
+
# can be replaced with `array1.intersect?(array2)`.
|
15
|
+
#
|
16
|
+
# `array1.intersect?(array2)` is faster and more readable.
|
13
17
|
#
|
14
18
|
# In cases like the following, compatibility is not ensured,
|
15
19
|
# so it will not be detected when using block argument.
|
@@ -20,6 +24,10 @@ module RuboCop
|
|
20
24
|
# [1].intersect?([1,2]) { |x| false } # => true
|
21
25
|
# ----
|
22
26
|
#
|
27
|
+
# NOTE: Although `Array#intersection` can take zero or multiple arguments,
|
28
|
+
# only cases where exactly one argument is provided can be replaced with
|
29
|
+
# `Array#intersect?` and are handled by this cop.
|
30
|
+
#
|
23
31
|
# @safety
|
24
32
|
# This cop cannot guarantee that `array1` and `array2` are
|
25
33
|
# actually arrays while method `intersect?` is for arrays only.
|
@@ -30,6 +38,15 @@ module RuboCop
|
|
30
38
|
# (array1 & array2).empty?
|
31
39
|
# (array1 & array2).none?
|
32
40
|
#
|
41
|
+
# # bad
|
42
|
+
# array1.intersection(array2).any?
|
43
|
+
# array1.intersection(array2).empty?
|
44
|
+
# array1.intersection(array2).none?
|
45
|
+
#
|
46
|
+
# # bad
|
47
|
+
# array1.any? { |elem| array2.member?(elem) }
|
48
|
+
# array1.none? { |elem| array2.member?(elem) }
|
49
|
+
#
|
33
50
|
# # good
|
34
51
|
# array1.intersect?(array2)
|
35
52
|
# !array1.intersect?(array2)
|
@@ -53,66 +70,90 @@ module RuboCop
|
|
53
70
|
|
54
71
|
minimum_target_ruby_version 3.1
|
55
72
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
73
|
+
PREDICATES = %i[any? empty? none?].to_set.freeze
|
74
|
+
ACTIVE_SUPPORT_PREDICATES = (PREDICATES + %i[present? blank?]).freeze
|
75
|
+
|
76
|
+
# @!method bad_intersection_check?(node, predicates)
|
77
|
+
def_node_matcher :bad_intersection_check?, <<~PATTERN
|
78
|
+
(call
|
79
|
+
{
|
80
|
+
(begin (send $_ :& $_))
|
81
|
+
(call $_ :intersection $_)
|
82
|
+
}
|
83
|
+
$%1
|
62
84
|
)
|
63
85
|
PATTERN
|
64
86
|
|
65
|
-
# @!method
|
66
|
-
def_node_matcher :
|
67
|
-
|
68
|
-
(
|
69
|
-
(
|
70
|
-
|
71
|
-
|
87
|
+
# @!method any_none_block_intersection(node)
|
88
|
+
def_node_matcher :any_none_block_intersection, <<~PATTERN
|
89
|
+
{
|
90
|
+
(block
|
91
|
+
(call $_receiver ${:any? :none?})
|
92
|
+
(args (arg _key))
|
93
|
+
(send $_argument :member? (lvar _key))
|
94
|
+
)
|
95
|
+
(numblock
|
96
|
+
(call $_receiver ${:any? :none?}) 1
|
97
|
+
(send $_argument :member? (lvar :_1))
|
98
|
+
)
|
99
|
+
(itblock
|
100
|
+
(call $_receiver ${:any? :none?}) :it
|
101
|
+
(send $_argument :member? (lvar :it))
|
102
|
+
)
|
103
|
+
}
|
72
104
|
PATTERN
|
73
105
|
|
74
|
-
MSG = 'Use `%<
|
75
|
-
'instead of `(%<receiver>s & %<argument>s).%<method_name>s`.'
|
106
|
+
MSG = 'Use `%<replacement>s` instead of `%<existing>s`.'
|
76
107
|
STRAIGHT_METHODS = %i[present? any?].freeze
|
77
108
|
NEGATED_METHODS = %i[blank? empty? none?].freeze
|
78
109
|
RESTRICT_ON_SEND = (STRAIGHT_METHODS + NEGATED_METHODS).freeze
|
79
110
|
|
80
111
|
def on_send(node)
|
81
112
|
return if node.block_literal?
|
82
|
-
return unless (receiver, argument, method_name =
|
113
|
+
return unless (receiver, argument, method_name = bad_intersection?(node))
|
83
114
|
|
84
|
-
|
115
|
+
dot = node.loc.dot.source
|
116
|
+
bang = straight?(method_name) ? '' : '!'
|
117
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
85
118
|
|
86
|
-
|
87
|
-
|
119
|
+
register_offense(node, replacement)
|
120
|
+
end
|
121
|
+
alias on_csend on_send
|
88
122
|
|
89
|
-
|
90
|
-
|
123
|
+
def on_block(node)
|
124
|
+
return unless (receiver, method_name, argument = any_none_block_intersection(node))
|
125
|
+
|
126
|
+
dot = node.send_node.loc.dot.source
|
127
|
+
bang = method_name == :any? ? '' : '!'
|
128
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
129
|
+
|
130
|
+
register_offense(node, replacement)
|
91
131
|
end
|
132
|
+
alias on_numblock on_block
|
133
|
+
alias on_itblock on_block
|
92
134
|
|
93
135
|
private
|
94
136
|
|
95
|
-
def
|
96
|
-
if active_support_extensions_enabled?
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
137
|
+
def bad_intersection?(node)
|
138
|
+
predicates = if active_support_extensions_enabled?
|
139
|
+
ACTIVE_SUPPORT_PREDICATES
|
140
|
+
else
|
141
|
+
PREDICATES
|
142
|
+
end
|
143
|
+
|
144
|
+
bad_intersection_check?(node, predicates)
|
101
145
|
end
|
102
146
|
|
103
147
|
def straight?(method_name)
|
104
148
|
STRAIGHT_METHODS.include?(method_name.to_sym)
|
105
149
|
end
|
106
150
|
|
107
|
-
def
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
argument: argument,
|
114
|
-
method_name: method_name
|
115
|
-
)
|
151
|
+
def register_offense(node, replacement)
|
152
|
+
message = format(MSG, replacement: replacement, existing: node.source)
|
153
|
+
|
154
|
+
add_offense(node, message: message) do |corrector|
|
155
|
+
corrector.replace(node, replacement)
|
156
|
+
end
|
116
157
|
end
|
117
158
|
end
|
118
159
|
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
module RuboCop
|
5
5
|
module Cop
|
6
6
|
module Style
|
7
|
-
#
|
7
|
+
# Checks for uses of braces or do/end around single line or
|
8
8
|
# multi-line blocks.
|
9
9
|
#
|
10
10
|
# Methods that can be either procedural or functional and cannot be
|
@@ -183,7 +183,8 @@ module RuboCop
|
|
183
183
|
def on_send(node)
|
184
184
|
return unless node.arguments?
|
185
185
|
return if node.parenthesized?
|
186
|
-
return if node.
|
186
|
+
return if node.assignment_method?
|
187
|
+
return if single_argument_operator_method?(node)
|
187
188
|
|
188
189
|
node.arguments.each do |arg|
|
189
190
|
get_blocks(arg) do |block|
|
@@ -194,6 +195,7 @@ module RuboCop
|
|
194
195
|
end
|
195
196
|
end
|
196
197
|
end
|
198
|
+
alias on_csend on_send
|
197
199
|
|
198
200
|
def on_block(node)
|
199
201
|
return if ignored_node?(node)
|
@@ -206,6 +208,7 @@ module RuboCop
|
|
206
208
|
end
|
207
209
|
|
208
210
|
alias on_numblock on_block
|
211
|
+
alias on_itblock on_block
|
209
212
|
|
210
213
|
private
|
211
214
|
|
@@ -342,16 +345,23 @@ module RuboCop
|
|
342
345
|
node.respond_to?(:block_node) && node.block_node
|
343
346
|
end
|
344
347
|
|
348
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
345
349
|
def get_blocks(node, &block)
|
346
350
|
case node.type
|
347
|
-
when :block, :numblock
|
351
|
+
when :block, :numblock, :itblock
|
348
352
|
yield node
|
349
|
-
when :send
|
353
|
+
when :send, :csend
|
354
|
+
# When a method has an argument which is another method with a block,
|
355
|
+
# that block needs braces, otherwise a syntax error will be introduced
|
356
|
+
# for subsequent arguments.
|
357
|
+
# Additionally, even without additional arguments, changing `{...}` to
|
358
|
+
# `do...end` will change the binding of the block to the outer method.
|
350
359
|
get_blocks(node.receiver, &block) if node.receiver
|
360
|
+
node.arguments.each { |argument| get_blocks(argument, &block) }
|
351
361
|
when :hash
|
352
362
|
# A hash which is passed as method argument may have no braces
|
353
363
|
# In that case, one of the K/V pairs could contain a block node
|
354
|
-
# which could change in meaning if do...end replaced {...}
|
364
|
+
# which could change in meaning if `do...end` is replaced with `{...}`
|
355
365
|
return if node.braces?
|
356
366
|
|
357
367
|
node.each_child_node { |child| get_blocks(child, &block) }
|
@@ -359,10 +369,10 @@ module RuboCop
|
|
359
369
|
node.each_child_node { |child| get_blocks(child, &block) }
|
360
370
|
end
|
361
371
|
end
|
372
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
362
373
|
|
363
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
364
374
|
def proper_block_style?(node)
|
365
|
-
return true if
|
375
|
+
return true if require_do_end?(node)
|
366
376
|
return special_method_proper_block_style?(node) if special_method?(node.method_name)
|
367
377
|
|
368
378
|
case style
|
@@ -372,15 +382,6 @@ module RuboCop
|
|
372
382
|
when :always_braces then braces_style?(node)
|
373
383
|
end
|
374
384
|
end
|
375
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
376
|
-
|
377
|
-
def require_braces?(node)
|
378
|
-
return false unless node.braces?
|
379
|
-
|
380
|
-
node.each_ancestor(:send).any? do |send|
|
381
|
-
send.arithmetic_operation? && node.source_range.end_pos < send.loc.selector.begin_pos
|
382
|
-
end
|
383
|
-
end
|
384
385
|
|
385
386
|
def require_do_end?(node)
|
386
387
|
return false if node.braces? || node.multiline?
|
@@ -474,18 +475,14 @@ module RuboCop
|
|
474
475
|
end
|
475
476
|
|
476
477
|
def return_value_of_scope?(node)
|
477
|
-
return false unless node.parent
|
478
|
-
|
479
|
-
conditional?(node.parent) || array_or_range?(node.parent) ||
|
480
|
-
node.parent.children.last == node
|
481
|
-
end
|
478
|
+
return false unless (parent = node.parent)
|
482
479
|
|
483
|
-
|
484
|
-
|
480
|
+
parent.conditional? || parent.operator_keyword? || array_or_range?(parent) ||
|
481
|
+
parent.children.last == node
|
485
482
|
end
|
486
483
|
|
487
484
|
def array_or_range?(node)
|
488
|
-
node.
|
485
|
+
node.type?(:array, :range)
|
489
486
|
end
|
490
487
|
|
491
488
|
def begin_required?(block_node)
|
@@ -493,6 +490,12 @@ module RuboCop
|
|
493
490
|
# `begin`...`end` when changing `do-end` to `{}`.
|
494
491
|
block_node.each_child_node(:rescue, :ensure).any? && !block_node.single_line?
|
495
492
|
end
|
493
|
+
|
494
|
+
def single_argument_operator_method?(node)
|
495
|
+
return false unless node.operator_method?
|
496
|
+
|
497
|
+
node.arguments.one? && node.first_argument.block_type?
|
498
|
+
end
|
496
499
|
end
|
497
500
|
end
|
498
501
|
end
|