Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive with DeletePrefix #118

Closed
avdv opened this issue May 24, 2020 · 3 comments · Fixed by #119
Closed

False positive with DeletePrefix #118

avdv opened this issue May 24, 2020 · 3 comments · Fixed by #119
Labels
bug Something isn't working

Comments

@avdv
Copy link

avdv commented May 24, 2020

For a multi-line string, I am using gsub(/^ /, '') and I receive a wrong suggestion that I should replace this call with delete_prefix.

Note that ^ matches at the beginning of a line, not only at the beginning of the string.


Expected behavior

No offense being reported.

Actual behavior

lib/colorls/flags.rb:169:23: C: Performance/DeletePrefix: Use delete_prefix instead of gsub.

      puts <<EXAMPLES.gsub(/^  /, '')

                      ^^^^

Steps to reproduce the problem

def show_examples
      puts <<EXAMPLES.gsub(/^  /, '')

  examples:

    * show the given file:

      colorls README.md

    * show matching files and list matching directories:

EXAMPLES
    end

RuboCop version

0.84.0 (using Parser 2.7.1.2, rubocop-ast 0.0.3, running on ruby 2.6.6 x86_64-linux)
@koic koic added the bug Something isn't working label May 25, 2020
koic added a commit to koic/rubocop-performance that referenced this issue May 25, 2020
…Prefix` cops

Fixes rubocop#118.

Fix a false positive for `Performance/DeletePrefix` and `Performance/DeletePrefix` cops
when using heredoc.
@mattbrictson
Copy link

I believe the same bug exists for Performance/StartWith.

# This checks that _any_ line in the output starts with "origin"
`git remote -v`.match?(/^origin/)

#  Performance/StartWith suggests this, which only tests the first line
`git remote -v`.start_with?("origin")

@mattbrictson
Copy link

I think the fix is that both cops should only flag patterns that start with \A (not ^).

@avdv
Copy link
Author

avdv commented Jun 1, 2020

I think the fix is that both cops should only flag patterns that start with \A (not ^).

Yes, same goes for $ and \Z in regard to the delete suffix and end with cops.

koic added a commit to koic/rubocop-performance that referenced this issue Jun 4, 2020
…Prefix` cops

Fixes rubocop#118.

Fix a false positive for `Performance/DeletePrefix`, `Performance/DeletePrefix`,
`Performance/StartWith`, and `Performance/EndWith` cops when receiver is multiline string.
koic added a commit to koic/rubocop-performance that referenced this issue Jun 4, 2020
…Prefix` cops

Fixes rubocop#118.

Fix a false positive for `Performance/DeletePrefix`, `Performance/DeletePrefix`,
`Performance/StartWith`, and `Performance/EndWith` cops when receiver is multiline string.
@koic koic closed this as completed in #119 Jun 5, 2020
koic added a commit that referenced this issue Jun 5, 2020
…x_and_delete_suffix

[Fix #118] Fix a false positive for `DeletePrefix` and `DeletePrefix` cops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants