diff --git a/CHANGELOG.md b/CHANGELOG.md index fa4388f1a63..facf7914145 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Bug fixes * [#8115](https://github.com/rubocop-hq/rubocop/issues/8115): Fix false negative for `Lint::FormatParameterMismatch` when argument contains formatting. ([@andrykonchin][]) +* [#8124](https://github.com/rubocop-hq/rubocop/issues/8124): Fix a false positive for `Lint/FormatParameterMismatch` when using named parameters with escaped `%`. ([@koic][]) ## 0.85.1 (2020-06-07) diff --git a/lib/rubocop/cop/utils/format_string.rb b/lib/rubocop/cop/utils/format_string.rb index 383d9e72ddc..dd99d5e27e0 100644 --- a/lib/rubocop/cop/utils/format_string.rb +++ b/lib/rubocop/cop/utils/format_string.rb @@ -120,7 +120,7 @@ def parse end def mixed_formats? - formats = format_sequences.map do |seq| + formats = format_sequences.reject(&:percent?).map do |seq| if seq.name :named elsif seq.max_digit_dollar_num diff --git a/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb b/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb index ffbb82cb7d4..1bd7d642218 100644 --- a/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb +++ b/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb @@ -222,6 +222,10 @@ expect_no_offenses('"foo %{bar} baz" % { bar: 42 }') end + it 'does not register an offense when using named parameters with escaped `%`' do + expect_no_offenses('format("%%%02X", hex: 10)') + end + it 'identifies correctly digits for spacing in format' do expect_no_offenses('"duration: %10.fms" % 42') end diff --git a/spec/rubocop/cop/utils/format_string_spec.rb b/spec/rubocop/cop/utils/format_string_spec.rb index 269f92f50bf..6fbaa787642 100644 --- a/spec/rubocop/cop/utils/format_string_spec.rb +++ b/spec/rubocop/cop/utils/format_string_spec.rb @@ -97,6 +97,11 @@ def format_sequences(string) expect(fs.valid?).to eq true end + it 'returns true when there are only named with escaped `%` formats' do + fs = described_class.new('%%%{foo}d') + expect(fs.valid?).to eq true + end + it 'returns false when there are unnumbered and numbered formats' do fs = described_class.new('%s %1$d') expect(fs.valid?).to eq false