From 9fe7006c9dff0b250618dff52c62b98a52f0c2ac Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Mon, 8 Jun 2020 10:44:11 +0300 Subject: [PATCH] Fix #8115. Lint/FormatParameterMismatch. Fix bug with formatting in argument --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/lint/format_parameter_mismatch.rb | 7 ++++++- spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5405090a33e..fa4388f1a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/lint/format_parameter_mismatch.rb b/lib/rubocop/cop/lint/format_parameter_mismatch.rb index e5cf62c0ada..356b06d5175 100644 --- a/lib/rubocop/cop/lint/format_parameter_mismatch.rb +++ b/lib/rubocop/cop/lint/format_parameter_mismatch.rb @@ -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) diff --git a/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb b/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb index c0d9601f837..ffbb82cb7d4 100644 --- a/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb +++ b/spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb @@ -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('%s', t: '%d')}) + end + end + it 'ignores percent right next to format string' do expect_no_offenses('format("%0.1f%% percent", 22.5)') end