Skip to content

Commit

Permalink
Fix rubocop#8115. Lint/FormatParameterMismatch. Fix bug with formatti…
Browse files Browse the repository at this point in the history
…ng in argument
  • Loading branch information
andrykonchin committed Jun 8, 2020
1 parent 273ad3b commit 9fe7006
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@
* [#8111](https://github.com/rubocop-hq/rubocop/pull/8111): Add auto-correct for `Style/StructInheritance`. ([@tejasbubane][])
* [#8113](https://github.com/rubocop-hq/rubocop/pull/8113): Let `expect_offense` templates add variable-length whitespace with `_{foo}`. ([@eugeneius][])

### Bug fixes

* [#8115](https://github.com/rubocop-hq/rubocop/issues/8115): Fix false negative for `Lint::FormatParameterMismatch` when argument contains formatting. ([@andrykonchin][])

## 0.85.1 (2020-06-07)

### Bug fixes
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/lint/format_parameter_mismatch.rb
Expand Up @@ -65,7 +65,12 @@ def format_string?(node)
end

def invalid_format_string?(node)
!RuboCop::Cop::Utils::FormatString.new(node.source).valid?
string = if sprintf?(node) || format?(node)
node.first_argument.source
else
node.receiver.source
end
!RuboCop::Cop::Utils::FormatString.new(string).valid?
end

def offending_node?(node)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb
Expand Up @@ -187,6 +187,14 @@
end
end

# Regression: https://github.com/rubocop-hq/rubocop/issues/8115
context 'when argument itself contains format characters and ' \
'formats in format string and argument are not equal' do
it 'ignores argument formatting' do
expect_no_offenses(%{format('%<t>s', t: '%d')})
end
end

it 'ignores percent right next to format string' do
expect_no_offenses('format("%0.1f%% percent", 22.5)')
end
Expand Down

0 comments on commit 9fe7006

Please sign in to comment.