Skip to content

Commit fb36825

Browse files
Earlopainbbatsov
authored andcommitted
Fix an error for Style/RedundantFormat with invalid format arguments
This is code that is catched by `Lint/FormatParameterMismatch`
1 parent c72ae6d commit fb36825

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14209](https://github.com/rubocop/rubocop/pull/14209): Fix an error for `Style/RedundantFormat` with invalid format arguments. ([@earlopain][])

lib/rubocop/cop/style/redundant_format.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def detect_unnecessary_fields(node)
121121
def register_all_fields_literal(node, string, arguments)
122122
return unless all_fields_literal?(string, arguments.dup)
123123

124-
formatted_string = format(string, *argument_values(arguments))
124+
format_arguments = argument_values(arguments)
125+
begin
126+
formatted_string = format(string, *format_arguments)
127+
rescue ArgumentError
128+
return
129+
end
125130
replacement = quote(formatted_string, node)
126131

127132
add_offense(node, message: message(node, replacement)) do |corrector|

spec/rubocop/cop/style/redundant_format_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@
368368
RUBY
369369
end
370370
end
371+
372+
context 'with invalid format arguments' do
373+
it 'does not register an offense' do
374+
expect_no_offenses(<<~RUBY)
375+
format('%{y}-%{m}-%{d}', 2015, 1, 1)
376+
RUBY
377+
end
378+
end
371379
end
372380

373381
context 'with constants' do

0 commit comments

Comments
 (0)