Skip to content

Commit c4af979

Browse files
committed
(FACT-3428) Style/FrozenStringLiteralComment
Add missing frozen string literal. In cases where we have a frozen string literal and want to mutate it, then use the unary plus, which "thaws" a frozen string, such as `+''` In cases where we're passed a frozen string from somewhere else, call String.dup to create a mutable copy. In both cases, the original string encoding is preserved: irb(main):007> str = ''.encode('UTF-16LE') => "" irb(main):008> str.dup.encoding => #<Encoding:UTF-16LE> irb(main):009> (+str).encoding => #<Encoding:UTF-16LE> Unlike String.new irb(main):010> String.new.encoding => #<Encoding:ASCII-8BIT>
1 parent 9cd98bb commit c4af979

File tree

7 files changed

+19
-18
lines changed

7 files changed

+19
-18
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,6 @@ Style/FormatStringToken:
205205
- 'lib/facter/resolvers/amzn/os_release_rpm.rb'
206206
- 'spec/facter/resolvers/amzn/os_release_rpm_spec.rb'
207207

208-
# Offense count: 6
209-
# This cop supports unsafe autocorrection (--autocorrect-all).
210-
# Configuration parameters: EnforcedStyle.
211-
# SupportedStyles: always, always_true, never
212-
Style/FrozenStringLiteralComment:
213-
Exclude:
214-
- 'lib/facter/custom_facts/core/execution/posix.rb'
215-
- 'lib/facter/custom_facts/core/execution/windows.rb'
216-
- 'lib/facter/custom_facts/util/resolution.rb'
217-
- 'spec/custom_facts/core/execution/posix_spec.rb'
218-
- 'spec/custom_facts/core/execution/windows_spec.rb'
219-
- 'spec/custom_facts/util/normalization_spec.rb'
220-
221208
# Offense count: 5
222209
# This cop supports unsafe autocorrection (--autocorrect-all).
223210
Style/GlobalStdStream:

lib/facter/custom_facts/core/execution/posix.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Facter
24
module Core
35
module Execution
@@ -44,7 +46,8 @@ def expand_command(command)
4446

4547
return unless exe && (expanded = which(exe))
4648

47-
expanded = "'#{expanded}'" if /\s/.match?(expanded)
49+
expanded = expanded.dup
50+
expanded = +"'#{expanded}'" if /\s/.match?(expanded)
4851
expanded << " #{args}" if args
4952

5053
expanded

lib/facter/custom_facts/core/execution/windows.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Facter
24
module Core
35
module Execution
@@ -56,7 +58,8 @@ def expand_command(command)
5658

5759
return unless exe && (expanded = which(exe))
5860

59-
expanded = "\"#{expanded}\"" if /\s+/.match?(expanded)
61+
expanded = expanded.dup
62+
expanded = +"\"#{expanded}\"" if /\s+/.match?(expanded)
6063
expanded << " #{args}" if args
6164

6265
expanded

lib/facter/custom_facts/util/resolution.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# This represents a fact resolution. A resolution is a concrete
24
# implementation of a fact. A single fact can have many resolutions and
35
# the correct resolution will be chosen at runtime. Each time
@@ -88,7 +90,7 @@ def resolution_type
8890
# @api private
8991
def evaluate(&block)
9092
if @last_evaluated
91-
msg = "Already evaluated #{@name}"
93+
msg = +"Already evaluated #{@name}"
9294
msg << " at #{@last_evaluated}" if msg.is_a? String
9395
msg << ', reevaluating anyways'
9496
log.warn msg

spec/custom_facts/core/execution/posix_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
describe Facter::Core::Execution::Posix, unless: LegacyFacter::Util::Config.windows? do
24
let(:posix_executor) { Facter::Core::Execution::Posix.new }
35

spec/custom_facts/core/execution/windows_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
describe Facter::Core::Execution::Windows do
24
let(:executor) { Facter::Core::Execution::Windows.new }
35

spec/custom_facts/util/normalization_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
describe LegacyFacter::Util::Normalization do
24
subject(:normalization) { LegacyFacter::Util::Normalization }
35

@@ -44,15 +46,15 @@ def utf8(str)
4446
end
4547

4648
it 'rejects strings that are not UTF-8 and do not match their claimed encoding' do
47-
invalid_shift_jis = "\xFF\x5C!".force_encoding(Encoding::SHIFT_JIS)
49+
invalid_shift_jis = (+"\xFF\x5C!").force_encoding(Encoding::SHIFT_JIS)
4850
expect do
4951
normalization.normalize(invalid_shift_jis)
5052
end.to raise_error(LegacyFacter::Util::Normalization::NormalizationError,
5153
/String encoding Shift_JIS is not UTF-8 and could not be converted to UTF-8/)
5254
end
5355

5456
it "rejects strings that claim to be UTF-8 encoded but aren't" do
55-
str = "\255ay!".force_encoding(Encoding::UTF_8)
57+
str = (+"\255ay!").force_encoding(Encoding::UTF_8)
5658
expect do
5759
normalization.normalize(str)
5860
end.to raise_error(LegacyFacter::Util::Normalization::NormalizationError,

0 commit comments

Comments
 (0)