Skip to content

Commit

Permalink
Style/RedundantFreeze stop considering ENV values as immutable
Browse files Browse the repository at this point in the history
Ref: #6784

`ENV["foo"]` returns a mutable string.

```ruby
>> ENV["PATH"].frozen?
=> true
>> ENV["PATH"].object_id
=> 260
>> ENV["PATH"].object_id
=> 280
```

As such it's perfectly legitimate to freeze them in a pattern such as:

```ruby
PATH = ENV["PATH"].freeze
```
  • Loading branch information
byroot committed Sep 22, 2021
1 parent 28ae02a commit 72daf0c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_redundant_freeze_env_values.md
@@ -0,0 +1 @@
* [#10099](https://github.com/rubocop/rubocop/pull/10099): Update`Style/RedundantFreeze` to stop considering `ENV` values as immutable. ([@byroot][])
1 change: 0 additions & 1 deletion lib/rubocop/cop/style/redundant_freeze.rb
Expand Up @@ -59,7 +59,6 @@ def strip_parenthesis(node)
(begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
(begin (send !{(str _) array} {:+ :- :* :** :/ :%} {float int}))
(begin (send _ {:== :=== :!= :<= :>= :< :>} _))
(send (const {nil? cbase} :ENV) :[] _)
(send _ {:count :length :size} ...)
(block (send _ {:count :length :size} ...) ...)
}
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/style/redundant_freeze_spec.rb
Expand Up @@ -20,8 +20,6 @@
it_behaves_like 'immutable objects', '1.5'
it_behaves_like 'immutable objects', ':sym'
it_behaves_like 'immutable objects', ':""'
it_behaves_like 'immutable objects', "ENV['foo']"
it_behaves_like 'immutable objects', "::ENV['foo']"
it_behaves_like 'immutable objects', "'foo'.count"
it_behaves_like 'immutable objects', '(1 + 2)'
it_behaves_like 'immutable objects', '(2 > 1)'
Expand All @@ -44,6 +42,8 @@
it_behaves_like 'mutable objects', "('a' * 20)"
it_behaves_like 'mutable objects', '(a + b)'
it_behaves_like 'mutable objects', '([42] * 42)'
it_behaves_like 'mutable objects', "ENV['foo']"
it_behaves_like 'mutable objects', "::ENV['foo']"

it 'allows .freeze on method call' do
expect_no_offenses('TOP_TEST = Something.new.freeze')
Expand Down

0 comments on commit 72daf0c

Please sign in to comment.