Skip to content

Add new Performance/UnfreezeString cop #4586

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

Merged
merged 1 commit into from
Jul 10, 2017
Merged

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Jul 9, 2017

In Ruby 2.3 or later, String#+@ is available.
This method unfreezes a string.

str = 'foo'.freeze
p str.frozen?    # => true
p (+str).frozen? # => false

String#dup works similarly, but +@ is faster than dup.
See. https://gist.github.com/k0kubun/e3da77cae2c132badd386c96f2de5768

This cop recommends to use +@ instead of dup.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

Sorry, something went wrong.

# @example
# # bad
# "string".dup
# String.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to add String.new("") to a bad case. It takes an empty string as an argument.

# @example
# # bad
# "string".dup
# String.new
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this isn't actually checked by the cop. 🙂

module RuboCop
module Cop
module Performance
# In Ruby 2.3 or later, Use unary plus operator to unfreeze a string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Use" with lowercase "u", since it's in the middle of a sentence. 😊


it 'registers an offense for a heredoc with `.dup`' do
expect_offense(<<-RUBY.strip_indent)
<<TEXT.dup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Where does the plus go here? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+<<TEXT
  foo
  bar
TEXT

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Had no idea this worked. 🙂

# @example
# # bad
# "string".dup
# String.new
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I'll add String.new('') to bad case.
And I'll add checks for String.new, String.new('') and String.new('something').

@pocke pocke force-pushed the str_uplus branch 2 times, most recently from b015d63 to 1dacee1 Compare July 9, 2017 13:54
@pocke
Copy link
Collaborator Author

pocke commented Jul 9, 2017

I added checks for String.new, and updated the documentation.

Verified

This commit was signed with the committer’s verified signature. The key has expired.
pocke Masataka Pocke Kuwabara
In Ruby 2.3 or later, `String#+@` is available.
This method unfreezes a string.

```ruby
str = 'foo'.freeze
p str.frozen?    # => true
p (+str).frozen? # => false
```

`String#dup` works similarly, but `+@` is faster than `dup`.
See. https://gist.github.com/k0kubun/e3da77cae2c132badd386c96f2de5768

This cop recommends to use `+@` instead of `dup`.
@bbatsov bbatsov merged commit d87cea2 into rubocop:master Jul 10, 2017

def_node_matcher :string_new?, <<-PATTERN
{
(send (const nil :String) :new {str dstr})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good reminder that I should look at adding an "optional" operator. 😅

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 this pull request may close these issues.

None yet

4 participants