Skip to content

Commit

Permalink
Teach Performance/{Start, End}With cops to look for Regexp#match?
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater authored and koic committed Oct 12, 2018
1 parent 2758fca commit 3027c87
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/rubocop/cop/performance/end_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Performance
#
# @example
# # bad
# 'abc'.match?(/bc\Z/)
# 'abc' =~ /bc\Z/
# 'abc'.match(/bc\Z/)
#
Expand All @@ -19,7 +20,7 @@ class EndWith < Cop
SINGLE_QUOTE = "'".freeze

def_node_matcher :redundant_regex?, <<-PATTERN
{(send $!nil? {:match :=~} (regexp (str $#literal_at_end?) (regopt)))
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt)))
(send (regexp (str $#literal_at_end?) (regopt)) {:match :=~} $_)}
PATTERN

Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/performance/start_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Performance
#
# @example
# # bad
# 'abc'.match?(/\Aab/)
# 'abc' =~ /\Aab/
# 'abc'.match(/\Aab/)
#
Expand All @@ -19,7 +20,7 @@ class StartWith < Cop
SINGLE_QUOTE = "'".freeze

def_node_matcher :redundant_regex?, <<-PATTERN
{(send $!nil? {:match :=~} (regexp (str $#literal_at_start?) (regopt)))
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
(send (regexp (str $#literal_at_start?) (regopt)) {:match :=~} $_)}
PATTERN

Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/cop/performance/end_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
end
end

include_examples('different match methods', '.match?')
include_examples('different match methods', ' =~')
include_examples('different match methods', '.match')

Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/cop/performance/start_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
end
end

include_examples('different match methods', '.match?')
include_examples('different match methods', ' =~')
include_examples('different match methods', '.match')

Expand Down

0 comments on commit 3027c87

Please sign in to comment.