From 8007f8e5e2231a03163a856a36c72a5b808375e6 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Sun, 24 May 2020 03:10:44 +0300 Subject: [PATCH] Performance optimization. Use Regexp/String method #match? instead of #match --- lib/rubocop/comment_config.rb | 2 +- .../cop/correctors/lambda_literal_to_method_corrector.rb | 2 +- .../cop/layout/heredoc_argument_closing_parenthesis.rb | 2 +- lib/rubocop/cop/lint/nested_percent_literal.rb | 2 +- lib/rubocop/cop/migration/department_name.rb | 4 ++-- lib/rubocop/cop/mixin/configurable_formatting.rb | 2 +- lib/rubocop/cop/mixin/ignored_pattern.rb | 2 +- lib/rubocop/cop/mixin/line_length_help.rb | 2 +- lib/rubocop/cop/naming/predicate_name.rb | 2 +- lib/rubocop/cop/style/inline_comment.rb | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/rubocop/comment_config.rb b/lib/rubocop/comment_config.rb index 025ef17d4e16..caf6a549cccd 100644 --- a/lib/rubocop/comment_config.rb +++ b/lib/rubocop/comment_config.rb @@ -124,7 +124,7 @@ def each_mentioned_cop end def directive_on_comment_line?(comment) - comment.text[1..-1].match(COMMENT_DIRECTIVE_REGEXP) + comment.text[1..-1].match?(COMMENT_DIRECTIVE_REGEXP) end def each_directive diff --git a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb index b1db3cbb8dc0..70c8432376b7 100644 --- a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +++ b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb @@ -129,7 +129,7 @@ def arg_to_unparenthesized_call? end def separating_space? - block_begin.source_buffer.source[block_begin.begin_pos + 2].match(/\s/) + block_begin.source_buffer.source[block_begin.begin_pos + 2].match?(/\s/) end end end diff --git a/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb b/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb index a85501acb941..cf945c03917e 100644 --- a/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +++ b/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb @@ -190,7 +190,7 @@ def incorrect_parenthesis_removal_begin(node) def safe_to_remove_line_containing_closing_paren?(node) last_line = processed_source[node.loc.end.line - 1] # Safe to remove if last line only contains `)`, `,`, and whitespace. - last_line.match(/^[ ]*\)[ ]{0,20},{0,1}[ ]*$/) + last_line.match?(/^[ ]*\)[ ]{0,20},{0,1}[ ]*$/) end def incorrect_parenthesis_removal_end(node) diff --git a/lib/rubocop/cop/lint/nested_percent_literal.rb b/lib/rubocop/cop/lint/nested_percent_literal.rb index 07826b743e75..cfae9472a231 100644 --- a/lib/rubocop/cop/lint/nested_percent_literal.rb +++ b/lib/rubocop/cop/lint/nested_percent_literal.rb @@ -42,7 +42,7 @@ def on_percent_literal(node) def contains_percent_literals?(node) node.each_child_node.any? do |child| literal = child.children.first.to_s.scrub - REGEXES.any? { |regex| literal.match(regex) } + REGEXES.any? { |regex| literal.match?(regex) } end end end diff --git a/lib/rubocop/cop/migration/department_name.rb b/lib/rubocop/cop/migration/department_name.rb index 9d23c8549132..05356e350db5 100644 --- a/lib/rubocop/cop/migration/department_name.rb +++ b/lib/rubocop/cop/migration/department_name.rb @@ -64,8 +64,8 @@ def check_cop_name(name, comment, offset) end def valid_content_token?(content_token) - !/\W+/.match(content_token).nil? || - !DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil? + /\W+/.match?(content_token) || + DISABLING_COPS_CONTENT_TOKEN.match?(content_token) end def contain_unexpected_character_for_department_name?(name) diff --git a/lib/rubocop/cop/mixin/configurable_formatting.rb b/lib/rubocop/cop/mixin/configurable_formatting.rb index cd3915583f8c..5b270259c00a 100644 --- a/lib/rubocop/cop/mixin/configurable_formatting.rb +++ b/lib/rubocop/cop/mixin/configurable_formatting.rb @@ -23,7 +23,7 @@ def report_opposing_styles(node, name) end def valid_name?(node, name, given_style = style) - name.match(self.class::FORMATS.fetch(given_style)) || + name.match?(self.class::FORMATS.fetch(given_style)) || class_emitter_method?(node, name) end diff --git a/lib/rubocop/cop/mixin/ignored_pattern.rb b/lib/rubocop/cop/mixin/ignored_pattern.rb index 0705dfba3c03..ed8b89eb1767 100644 --- a/lib/rubocop/cop/mixin/ignored_pattern.rb +++ b/lib/rubocop/cop/mixin/ignored_pattern.rb @@ -18,7 +18,7 @@ def ignored_line?(line) end def matches_ignored_pattern?(line) - ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) } + ignored_patterns.any? { |pattern| Regexp.new(pattern).match?(line) } end def ignored_patterns diff --git a/lib/rubocop/cop/mixin/line_length_help.rb b/lib/rubocop/cop/mixin/line_length_help.rb index 066f1f2d9d83..8bfcb35cfff5 100644 --- a/lib/rubocop/cop/mixin/line_length_help.rb +++ b/lib/rubocop/cop/mixin/line_length_help.rb @@ -18,7 +18,7 @@ def directive_on_source_line?(line_index) return false unless comment - comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP) + comment.text.match?(CommentConfig::COMMENT_DIRECTIVE_REGEXP) end def allow_uri? diff --git a/lib/rubocop/cop/naming/predicate_name.rb b/lib/rubocop/cop/naming/predicate_name.rb index a5a17e1db66f..a36b680be4c8 100644 --- a/lib/rubocop/cop/naming/predicate_name.rb +++ b/lib/rubocop/cop/naming/predicate_name.rb @@ -67,7 +67,7 @@ def on_def(node) private def allowed_method_name?(method_name, prefix) - !method_name.match(/^#{prefix}[^0-9]/) || + !method_name.match?(/^#{prefix}[^0-9]/) || method_name == expected_name(method_name, prefix) || method_name.end_with?('=') || allowed_methods.include?(method_name) diff --git a/lib/rubocop/cop/style/inline_comment.rb b/lib/rubocop/cop/style/inline_comment.rb index 1c779de7d116..0c0c96c7b1fa 100644 --- a/lib/rubocop/cop/style/inline_comment.rb +++ b/lib/rubocop/cop/style/inline_comment.rb @@ -23,7 +23,7 @@ class InlineComment < Cop def investigate(processed_source) processed_source.each_comment do |comment| next if comment_line?(processed_source[comment.loc.line - 1]) || - comment.text.match(/\A# rubocop:(enable|disable)/) + comment.text.match?(/\A# rubocop:(enable|disable)/) add_offense(comment) end