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

Add new `Performance/UnfreezeString` cop #4586

Merged
merged 1 commit into from Jul 10, 2017

Conversation

Projects
None yet
4 participants
@pocke
Member

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).
it 'registers an offense for a heredoc with `.dup`' do
expect_offense(<<-RUBY.strip_indent)
<<TEXT.dup

This comment has been minimized.

@Drenmi

Drenmi Jul 9, 2017

Collaborator

Hm. Where does the plus go here? 🤔

@Drenmi

Drenmi Jul 9, 2017

Collaborator

Hm. Where does the plus go here? 🤔

This comment has been minimized.

@pocke

pocke Jul 9, 2017

Member
+<<TEXT
  foo
  bar
TEXT
@pocke

pocke Jul 9, 2017

Member
+<<TEXT
  foo
  bar
TEXT

This comment has been minimized.

@Drenmi

Drenmi Jul 9, 2017

Collaborator

Cool! Had no idea this worked. 🙂

@Drenmi

Drenmi Jul 9, 2017

Collaborator

Cool! Had no idea this worked. 🙂

@pocke

This comment has been minimized.

Show comment
Hide comment
@pocke

pocke Jul 9, 2017

Member

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

Member

pocke commented Jul 9, 2017

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

Add new `Performance/UnfreezeString` cop
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-hq:master Jul 10, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
def_node_matcher :string_new?, <<-PATTERN
{
(send (const nil :String) :new {str dstr})

This comment has been minimized.

@Drenmi

Drenmi Jul 10, 2017

Collaborator

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

@Drenmi

Drenmi Jul 10, 2017

Collaborator

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