From 72daf0c005fec36e0b11e8e373bcd692a5ecce65 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 20 Sep 2021 15:28:53 +0200 Subject: [PATCH] `Style/RedundantFreeze` stop considering `ENV` values as immutable Ref: https://github.com/rubocop/rubocop/pull/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 ``` --- changelog/fix_redundant_freeze_env_values.md | 1 + lib/rubocop/cop/style/redundant_freeze.rb | 1 - spec/rubocop/cop/style/redundant_freeze_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_redundant_freeze_env_values.md diff --git a/changelog/fix_redundant_freeze_env_values.md b/changelog/fix_redundant_freeze_env_values.md new file mode 100644 index 00000000000..a95cb592736 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/style/redundant_freeze.rb b/lib/rubocop/cop/style/redundant_freeze.rb index a8df5517436..f1f21006f99 100644 --- a/lib/rubocop/cop/style/redundant_freeze.rb +++ b/lib/rubocop/cop/style/redundant_freeze.rb @@ -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} ...) ...) } diff --git a/spec/rubocop/cop/style/redundant_freeze_spec.rb b/spec/rubocop/cop/style/redundant_freeze_spec.rb index 70821af3df2..2ffc476a07f 100644 --- a/spec/rubocop/cop/style/redundant_freeze_spec.rb +++ b/spec/rubocop/cop/style/redundant_freeze_spec.rb @@ -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)' @@ -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')