Skip to content

Commit 12929bc

Browse files
committed
Do not register an offense for FormatParameterMismatch on variables
1 parent f3a0cd7 commit 12929bc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [#2218](https://github.com/bbatsov/rubocop/issues/2218): Fix loading config error when safe yaml is only partially loaded. ([@maxjacobson][])
1414
* [#2161](https://github.com/bbatsov/rubocop/issues/2161): Allow an explicit receiver (except `Kernel`) in `Style/SignalException`. ([@lumeet][])
1515
* [#2237](https://github.com/bbatsov/rubocop/pull/2237): Allow `Lint/FormatParameterMismatch` to be called using `Kernel.format` and `Kernel.sprintf`. ([@rrosenblum][])
16+
* [#2234](https://github.com/bbatsov/rubocop/issues/2234): Do not register an offense for `Lint/FormatParameterMismatch` when the format string is a variable. ([@rrosenblum][])
1617

1718
## 0.34.0 (05/09/2015)
1819

lib/rubocop/cop/lint/format_parameter_mismatch.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def on_send(node)
3030
private
3131

3232
def offending_node?(node)
33+
return false unless called_on_string?(node)
34+
3335
if sprintf?(node) || format?(node) || percent?(node)
3436
if named_mode?(node)
3537
false
@@ -42,6 +44,15 @@ def offending_node?(node)
4244
end
4345
end
4446

47+
def called_on_string?(node)
48+
receiver_node, _method, format_string, = *node
49+
if receiver_node.nil? || receiver_node.const_type?
50+
format_string && format_string.str_type?
51+
else
52+
receiver_node.str_type?
53+
end
54+
end
55+
4556
def named_mode?(node)
4657
receiver_node, _method_name, *args = *node
4758

spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
describe RuboCop::Cop::Lint::FormatParameterMismatch do
66
subject(:cop) { described_class.new }
77

8+
shared_examples 'variables' do |variable|
9+
it 'does not register an offense for % called on a variable' do
10+
inspect_source(cop, ["#{variable} = '%s'",
11+
"#{variable} % [foo]"])
12+
13+
expect(cop.messages).to be_empty
14+
end
15+
16+
it 'does not register an offense for format called on a variable' do
17+
inspect_source(cop, ["#{variable} = '%s'",
18+
"format(#{variable}, foo)"])
19+
20+
expect(cop.messages).to be_empty
21+
end
22+
23+
it 'does not register an offense for format called on a variable' do
24+
inspect_source(cop, ["#{variable} = '%s'",
25+
"sprintf(#{variable}, foo)"])
26+
27+
expect(cop.messages).to be_empty
28+
end
29+
end
30+
31+
it_behaves_like 'variables', 'CONST'
32+
it_behaves_like 'variables', 'var'
33+
it_behaves_like 'variables', '@var'
34+
it_behaves_like 'variables', '@@var'
35+
it_behaves_like 'variables', '$var'
36+
837
it 'registers an offense when calling Kernel.format ' \
938
'and the fields do not match' do
1039
inspect_source(cop, 'Kernel.format("%s %s", 1)')

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