diff --git a/CHANGELOG.md b/CHANGELOG.md index 6560e8f168..4567368dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#111](/~https://github.com/rubocop-hq/rubocop-performance/issues/111): Fix an error for `Performance/DeletePrefix` and `Performance/DeleteSuffix` cops when using autocorrection with RuboCop 0.81 or lower. ([@koic][]) +* [#118](/~https://github.com/rubocop-hq/rubocop-performance/issues/118): Fix a false positive for `Performance/DeletePrefix` and `Performance/DeleteSuffix` cops when using heredoc. ([@koic][]) ## 1.6.0 (2020-05-22) diff --git a/lib/rubocop/cop/performance/delete_prefix.rb b/lib/rubocop/cop/performance/delete_prefix.rb index 9ec809ff04..cab3bdb070 100644 --- a/lib/rubocop/cop/performance/delete_prefix.rb +++ b/lib/rubocop/cop/performance/delete_prefix.rb @@ -43,6 +43,7 @@ class DeletePrefix < Cop def on_send(node) gsub_method?(node) do |_, bad_method, _, replace_string| return unless replace_string.blank? + return if node.receiver.respond_to?(:heredoc?) && node.receiver.heredoc? good_method = PREFERRED_METHODS[bad_method] diff --git a/lib/rubocop/cop/performance/delete_suffix.rb b/lib/rubocop/cop/performance/delete_suffix.rb index 2b3c3d4554..fb97d4f5ab 100644 --- a/lib/rubocop/cop/performance/delete_suffix.rb +++ b/lib/rubocop/cop/performance/delete_suffix.rb @@ -43,6 +43,7 @@ class DeleteSuffix < Cop def on_send(node) gsub_method?(node) do |_, bad_method, _, replace_string| return unless replace_string.blank? + return if node.receiver.respond_to?(:heredoc?) && node.receiver.heredoc? good_method = PREFERRED_METHODS[bad_method] diff --git a/spec/rubocop/cop/performance/delete_prefix_spec.rb b/spec/rubocop/cop/performance/delete_prefix_spec.rb index 8a3f020c96..b785f420ee 100644 --- a/spec/rubocop/cop/performance/delete_prefix_spec.rb +++ b/spec/rubocop/cop/performance/delete_prefix_spec.rb @@ -94,6 +94,26 @@ end end + context 'when using heredoc' do + it 'does not register an offense when using `\A` as starting pattern' do + expect_no_offenses(<<~RUBY) + puts <<~HEREDOC.gsub(/\\A /, '') + foo + bar + HEREDOC + RUBY + end + + it 'does not register an offense when using `^` as starting pattern' do + expect_no_offenses(<<~RUBY) + puts <<~HEREDOC.gsub(/^ /, '') + foo + bar + HEREDOC + RUBY + end + end + context 'when using a non-blank string as replacement string' do it 'does not register an offense and corrects when using `gsub`' do expect_no_offenses(<<~RUBY) diff --git a/spec/rubocop/cop/performance/delete_suffix_spec.rb b/spec/rubocop/cop/performance/delete_suffix_spec.rb index 5eee67f59e..ea4921247e 100644 --- a/spec/rubocop/cop/performance/delete_suffix_spec.rb +++ b/spec/rubocop/cop/performance/delete_suffix_spec.rb @@ -94,6 +94,26 @@ end end + context 'when using heredoc' do + it 'does not register an offense when using `\z` as ending pattern' do + expect_no_offenses(<<~RUBY) + puts <<~HEREDOC.gsub(/ \\z/, '') + foo + bar + HEREDOC + RUBY + end + + it 'does not register an offense when using `$` as ending pattern' do + expect_no_offenses(<<~RUBY) + puts <<~HEREDOC.gsub(/ $/, '') + foo + bar + HEREDOC + RUBY + end + end + context 'when using a non-blank string as replacement string' do it 'does not register an offense and corrects when using `gsub`' do expect_no_offenses(<<~RUBY)