Add new Performance/UnfreezeString cop#4586
Conversation
There was a problem hiding this comment.
I think it would be good to add String.new("") to a bad case. It takes an empty string as an argument.
There was a problem hiding this comment.
It seems like this isn't actually checked by the cop. 🙂
There was a problem hiding this comment.
"Use" with lowercase "u", since it's in the middle of a sentence. 😊
There was a problem hiding this comment.
Hm. Where does the plus go here? 🤔
There was a problem hiding this comment.
Cool! Had no idea this worked. 🙂
There was a problem hiding this comment.
Thanks!
I'll add String.new('') to bad case.
And I'll add checks for String.new, String.new('') and String.new('something').
b015d63 to
1dacee1
Compare
|
I added checks for |
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`.
|
|
||
| def_node_matcher :string_new?, <<-PATTERN | ||
| { | ||
| (send (const nil :String) :new {str dstr}) |
There was a problem hiding this comment.
Good reminder that I should look at adding an "optional" operator. 😅
In Ruby 2.3 or later,
String#+@is available.This method unfreezes a string.
String#dupworks similarly, but+@is faster thandup.See. https://gist.github.com/k0kubun/e3da77cae2c132badd386c96f2de5768
This cop recommends to use
+@instead ofdup.Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).rake spec) are passing.rake internal_investigation.and description in grammatically correct, complete sentences.
rake generate_cops_documentation(required only when you've added a new cop or changed the configuration/documentation of an existing cop).