Skip to content

Commit ab7694a

Browse files
committed
[Fix rubocop#2232] Fix false positive in Lint/FormatParameterMismatch for
argument with splat operator
1 parent 97b97ea commit ab7694a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bug fixes
6+
7+
* [#2232](https://github.com/bbatsov/rubocop/issues/2232): Fix false positive in `Lint/FormatParameterMismatch` for argument with splat operator. ([@dreyks][])
8+
59
## 0.34.1 (09/09/2015)
610

711
### Bug Fixes
@@ -1599,3 +1603,4 @@
15991603
[@imtayadeway]: https://github.com/imtayadeway
16001604
[@mrfoto]: https://github.com/mrfoto
16011605
[@karreiro]: https://github.com/karreiro
1606+
[@dreyks]: https://github.com/dreyks

lib/rubocop/cop/lint/format_parameter_mismatch.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def on_send(node)
3030

3131
def offending_node?(node)
3232
if sprintf?(node) || format?(node) || percent?(node)
33-
if named_mode?(node)
33+
if named_mode?(node) || node_with_splat_args?(node)
3434
false
3535
else
3636
num_of_args_for_format, num_of_expected_fields = count_matches(node)
@@ -57,6 +57,14 @@ def named_mode?(node)
5757
.scan(NAMED_FIELD_REGEX).count > 0
5858
end
5959

60+
def node_with_splat_args?(node)
61+
return false if percent?(node)
62+
63+
_receiver_node, _method_name, *args = *node
64+
65+
args[1..-1].any? { |arg| arg.type == :splat }
66+
end
67+
6068
def heredoc?(node)
6169
_receiver, _name, args = *node
6270

spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@
119119
expect(cop.offenses.size).to eq(1)
120120
end
121121

122+
it 'does not register an offense for sprintf with splat argument' do
123+
inspect_source(cop, 'sprintf("%d%d", *test)')
124+
expect(cop.offenses).to be_empty
125+
end
126+
127+
it 'does not register an offense for format with splat argument' do
128+
inspect_source(cop, 'format("%d%d", *test)')
129+
expect(cop.offenses).to be_empty
130+
end
131+
122132
it 'finds the correct number of fields' do
123133
expect(''.scan(cop.fields_regex).size)
124134
.to eq(0)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy