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

UnfreezeString autocorrect incorrectly handles .force_encoding #252

Closed
johnnyshields opened this issue May 25, 2021 · 2 comments · Fixed by #253
Closed

UnfreezeString autocorrect incorrectly handles .force_encoding #252

johnnyshields opened this issue May 25, 2021 · 2 comments · Fixed by #253
Labels
bug Something isn't working

Comments

@johnnyshields
Copy link

johnnyshields commented May 25, 2021

The autocorrect for:

String.new.force_encoding('Ascii')

gave me:

# BAD - raises FrozenError (can't modify frozen String)
+''.force_encoding('Ascii')

Instead it should add parenthesis

# GOOD
(+'').force_encoding('Ascii')

Please check if there are other affected methods... I haven't tested but imagine you are already handling bang (!) methods properly.

@johnnyshields johnnyshields changed the title UnfreezeString autocorrect doesn't handle .force_encoding UnfreezeString autocorrect incorrectly handles .force_encoding May 25, 2021
koic added a commit to koic/rubocop-performance that referenced this issue May 25, 2021
…eezeString`

Fixes rubocop#252.

This PR fixes an incorrect auto-correct for `Performance/UnfreezeString`
when invoking a method after `String.new` with a string.
@koic
Copy link
Member

koic commented May 25, 2021

+'str'.do_something is equivalent to +('str'.do_something), not (+'str').do_something. So, other than force_encoding method may be unintended. I opened #253 that consistent adding parentheses if a method call after an offense code.

@koic koic added the bug Something isn't working label May 25, 2021
@johnnyshields
Copy link
Author

Wonderful, thanks for the prompt response!

@koic koic closed this as completed in #253 May 26, 2021
koic added a commit that referenced this issue May 26, 2021
…rmance_unfreeze_string

[Fix #252] Fix an incorrect auto-correct for `Performance/UnfreezeString`
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.

2 participants