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

EndWith/StartWith autocorrects are not nil-safe #50

Closed
dduugg opened this issue May 6, 2019 · 1 comment
Closed

EndWith/StartWith autocorrects are not nil-safe #50

dduugg opened this issue May 6, 2019 · 1 comment

Comments

@dduugg
Copy link
Contributor

dduugg commented May 6, 2019

The EndWith/StartWith autocorrects do not contemplate nil. I propose modifying them to behave like RegexpMatch (which corrects do_something if pattern =~ param into do_something if pattern&.match?(path)

Expected behavior

do_something if var =~ /\A#/ should autocorrect to do_something if var&.start_with?('#')

Since it appears rubocop will soon end support for pre-2.3 ruby, I propose only fixing this for ruby 2.3 and above (where safe navigation is available).

Actual behavior

do_something if var =~ /\A#/ autocorrects to do_something if var.start_with?('#'). The correction will now raise a NoMethodError when var is nil, where previously the condition would simply evaluate to nil.

Steps to reproduce the problem

Invoke rubocop -a --only Performance/StartWith on a file containing do_something if var =~ /\A#/, with a .rubocop.yml file containing:

require:
  - rubocop-performance
Performance/StartWith:
  AutoCorrect: true

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

$ bundle exec rubocop -V
0.68.0 (using Parser 2.6.0.0, running on ruby 2.4.5 x86_64-darwin17)
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 7, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 9, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 10, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 14, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 20, 2019
@koic
Copy link
Member

koic commented Apr 24, 2021

This cop is marked SafeAutoCorrect: false and unmarked AutoCorrect: false. So I will close this issue. Thank you.

@koic koic closed this as completed Apr 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants