Skip to content

Commit

Permalink
[Fix rubocop#8124] Fix a false positive for `Lint/FormatParameterMism…
Browse files Browse the repository at this point in the history
…atch`

Fixes rubocop#8124.

This PR fixes a false positive for `Lint/FormatParameterMismatch`
using named parameters with escaped `%`.
  • Loading branch information
koic committed Jun 9, 2020
1 parent 1c622de commit c4c43e7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/utils/format_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("%%%<hex>02X", hex: 10)')
end

it 'identifies correctly digits for spacing in format' do
expect_no_offenses('"duration: %10.fms" % 42')
end
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/utils/format_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4c43e7

Please sign in to comment.