From 5e03e9be65f3568c1ec1278a2fe395a8fb644ab1 Mon Sep 17 00:00:00 2001 From: Bill Ruddock Date: Mon, 6 Jul 2020 05:27:29 +0100 Subject: [PATCH] Allow top-level const in MutableConst Allow top-level `::ENV`, `::Struct` in Style/MutableConstant cop. --- lib/rubocop/cop/style/mutable_constant.rb | 8 ++++---- spec/rubocop/cop/style/mutable_constant_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/rubocop/cop/style/mutable_constant.rb b/lib/rubocop/cop/style/mutable_constant.rb index e3e512859ee..d4c310e38c9 100644 --- a/lib/rubocop/cop/style/mutable_constant.rb +++ b/lib/rubocop/cop/style/mutable_constant.rb @@ -150,14 +150,14 @@ def correct_splat_expansion(corrector, expr, splat_value) def_node_matcher :operation_produces_immutable_object?, <<~PATTERN { (const _ _) - (send (const nil? :Struct) :new ...) - (block (send (const nil? :Struct) :new ...) ...) + (send (const {nil? cbase} :Struct) :new ...) + (block (send (const {nil? cbase} :Struct) :new ...) ...) (send _ :freeze) (send {float int} {:+ :- :* :** :/ :% :<<} _) (send _ {:+ :- :* :** :/ :%} {float int}) (send _ {:== :=== :!= :<= :>= :< :>} _) - (send (const nil? :ENV) :[] _) - (or (send (const nil? :ENV) :[] _) _) + (send (const {nil? cbase} :ENV) :[] _) + (or (send (const {nil? cbase} :ENV) :[] _) _) (send _ {:count :length :size} ...) (block (send _ {:count :length :size} ...) ...) } diff --git a/spec/rubocop/cop/style/mutable_constant_spec.rb b/spec/rubocop/cop/style/mutable_constant_spec.rb index ca503ffae45..eb372db00ec 100644 --- a/spec/rubocop/cop/style/mutable_constant_spec.rb +++ b/spec/rubocop/cop/style/mutable_constant_spec.rb @@ -61,6 +61,7 @@ it_behaves_like 'immutable objects', 'FOO - BAR' it_behaves_like 'immutable objects', "'foo' + 'bar'" it_behaves_like 'immutable objects', "ENV['foo']" + it_behaves_like 'immutable objects', "::ENV['foo']" it 'allows method call assignments' do expect_no_offenses('TOP_TEST = Something.new') @@ -192,9 +193,13 @@ it_behaves_like 'immutable objects', '1.5' it_behaves_like 'immutable objects', ':sym' it_behaves_like 'immutable objects', "ENV['foo']" + it_behaves_like 'immutable objects', "::ENV['foo']" it_behaves_like 'immutable objects', 'OTHER_CONST' + it_behaves_like 'immutable objects', '::OTHER_CONST' it_behaves_like 'immutable objects', 'Namespace::OTHER_CONST' + it_behaves_like 'immutable objects', '::Namespace::OTHER_CONST' it_behaves_like 'immutable objects', 'Struct.new' + it_behaves_like 'immutable objects', '::Struct.new' it_behaves_like 'immutable objects', 'Struct.new(:a, :b)' it_behaves_like 'immutable objects', <<~RUBY Struct.new(:node) do @@ -303,6 +308,9 @@ def assignment? expect_no_offenses(<<~RUBY) CONST = ENV['foo'] || 'foo' RUBY + expect_no_offenses(<<~RUBY) + CONST = ::ENV['foo'] || 'foo' + RUBY end it 'accepts operating on a constant and an interger' do