Skip to content

Add new Performance/UnfreezeString cop#4586

Merged
bbatsov merged 1 commit intorubocop:masterfrom
pocke:str_uplus
Jul 10, 2017
Merged

Add new Performance/UnfreezeString cop#4586
bbatsov merged 1 commit intorubocop:masterfrom
pocke:str_uplus

Conversation

@pocke
Copy link
Copy Markdown
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).
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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. 🙂

Copy link
Copy Markdown
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. 😊

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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. 🙂

Copy link
Copy Markdown
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
Copy Markdown
Collaborator Author

pocke commented Jul 9, 2017

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

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
Copy Markdown
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

4 participants