diff --git a/changelog/change_interpolated_string_literals_are_no.md b/changelog/change_interpolated_string_literals_are_no.md new file mode 100644 index 00000000000..ae931e91ee3 --- /dev/null +++ b/changelog/change_interpolated_string_literals_are_no.md @@ -0,0 +1 @@ +* [#10006](https://github.com/rubocop/rubocop/pull/10006): Interpolated string literals are no longer frozen since Ruby 3.0. ([@splattael][]) diff --git a/lib/rubocop/cop/mixin/frozen_string_literal.rb b/lib/rubocop/cop/mixin/frozen_string_literal.rb index 137ffb0aa2b..02101b0cc5b 100644 --- a/lib/rubocop/cop/mixin/frozen_string_literal.rb +++ b/lib/rubocop/cop/mixin/frozen_string_literal.rb @@ -8,9 +8,10 @@ module FrozenStringLiteral FROZEN_STRING_LITERAL = '# frozen_string_literal:' FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true' - FROZEN_STRING_LITERAL_TYPES = %i[str dstr].freeze + FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze + FROZEN_STRING_LITERAL_TYPES_RUBY30 = %i[str].freeze - private_constant :FROZEN_STRING_LITERAL_TYPES + private_constant :FROZEN_STRING_LITERAL_TYPES_RUBY27, :FROZEN_STRING_LITERAL_TYPES_RUBY30 def frozen_string_literal_comment_exists? leading_comment_lines.any? { |line| MagicComment.parse(line).valid_literal_value? } @@ -19,7 +20,13 @@ def frozen_string_literal_comment_exists? private def frozen_string_literal?(node) - FROZEN_STRING_LITERAL_TYPES.include?(node.type) && frozen_string_literals_enabled? + literal_types = if target_ruby_version >= 3.0 + FROZEN_STRING_LITERAL_TYPES_RUBY30 + else + FROZEN_STRING_LITERAL_TYPES_RUBY27 + end + + literal_types.include?(node.type) && frozen_string_literals_enabled? end def frozen_string_literals_enabled? diff --git a/lib/rubocop/cop/style/mutable_constant.rb b/lib/rubocop/cop/style/mutable_constant.rb index 7dca62cf4a4..3801ce3cc30 100644 --- a/lib/rubocop/cop/style/mutable_constant.rb +++ b/lib/rubocop/cop/style/mutable_constant.rb @@ -21,6 +21,9 @@ module Style # # NOTE: Regexp and Range literals are frozen objects since Ruby 3.0. # + # NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated + # string literals when `# frozen-string-literal: true` is used. + # # @example EnforcedStyle: literals (default) # # bad # CONST = [1, 2, 3] diff --git a/lib/rubocop/cop/style/redundant_freeze.rb b/lib/rubocop/cop/style/redundant_freeze.rb index 8d945eac85a..a8df5517436 100644 --- a/lib/rubocop/cop/style/redundant_freeze.rb +++ b/lib/rubocop/cop/style/redundant_freeze.rb @@ -7,6 +7,9 @@ module Style # # NOTE: Regexp and Range literals are frozen objects since Ruby 3.0. # + # NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated + # string literals when `# frozen-string-literal: true` is used. + # # @example # # bad # CONST = 1.freeze diff --git a/spec/rubocop/cop/style/mutable_constant_spec.rb b/spec/rubocop/cop/style/mutable_constant_spec.rb index 4c7cec74e4f..a5073b9e225 100644 --- a/spec/rubocop/cop/style/mutable_constant_spec.rb +++ b/spec/rubocop/cop/style/mutable_constant_spec.rb @@ -106,20 +106,40 @@ end end - context 'when the frozen string literal comment is missing' do - it_behaves_like 'mutable objects', '"#{a}"' - end + context 'Ruby 3.0 or higher', :ruby30 do + context 'when the frozen string literal comment is missing' do + it_behaves_like 'mutable objects', '"#{a}"' + end - context 'when the frozen string literal comment is true' do - let(:prefix) { '# frozen_string_literal: true' } + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } - it_behaves_like 'immutable objects', '"#{a}"' + it_behaves_like 'mutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } + + it_behaves_like 'mutable objects', '"#{a}"' + end end - context 'when the frozen string literal comment is false' do - let(:prefix) { '# frozen_string_literal: false' } + context 'Ruby 2.7 or lower', :ruby27 do + context 'when the frozen string literal comment is missing' do + it_behaves_like 'mutable objects', '"#{a}"' + end - it_behaves_like 'mutable objects', '"#{a}"' + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } + + it_behaves_like 'immutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } + + it_behaves_like 'mutable objects', '"#{a}"' + end end end diff --git a/spec/rubocop/cop/style/redundant_freeze_spec.rb b/spec/rubocop/cop/style/redundant_freeze_spec.rb index 9595dc434f2..70821af3df2 100644 --- a/spec/rubocop/cop/style/redundant_freeze_spec.rb +++ b/spec/rubocop/cop/style/redundant_freeze_spec.rb @@ -75,20 +75,40 @@ end end - context 'when the frozen string literal comment is missing' do - it_behaves_like 'mutable objects', '"#{a}"' - end + context 'Ruby 3.0 or higher', :ruby30 do + context 'when the frozen string literal comment is missing' do + it_behaves_like 'mutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } - context 'when the frozen string literal comment is true' do - let(:prefix) { '# frozen_string_literal: true' } + it_behaves_like 'mutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } - it_behaves_like 'immutable objects', '"#{a}"' + it_behaves_like 'mutable objects', '"#{a}"' + end end - context 'when the frozen string literal comment is false' do - let(:prefix) { '# frozen_string_literal: false' } + context 'Ruby 2.7 or lower', :ruby27 do + context 'when the frozen string literal comment is missing' do + it_behaves_like 'mutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } + + it_behaves_like 'immutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } - it_behaves_like 'mutable objects', '"#{a}"' + it_behaves_like 'mutable objects', '"#{a}"' + end end describe 'Regexp and Range literals' do