The Lint/FormatParameterMismatch cop generates false positives for format strings with "wildcard" width _and_ precision. From the printf(3) man page:
A field width or precision, or both, may be indicated by an asterisk '*' or an asterisk followed
by one or more decimal digits and a '$' instead of a digit string. In this case, an int
argument supplies the field width or precision. A negative field width is treated as a left
adjustment flag followed by a positive field width; a negative precision is treated as though
it were missing. If a single format directive mixes positional (nn$) and non-positional argu-
ments, the results are undefined.
class RuboCop::Sprintf
def test
sprintf("%*.*s\n", 10, 10, 'this is a test')
end
end
No error should be generated
It will complain that argument count is wrong (3 instead of 2)
Save the above example as rubocop_sprintf.rb
⇨ rubocop ./rubocop_sprintf.rb
Inspecting 1 file
W
Offenses:
rubocop_sprintf.rb:3:5: W: Number of arguments (3) to sprintf doesn't match the number of fields (2).
sprintf("%*.*s\n", 10, 10, 'this is a test')
^^^^^^^
⇨ rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-darwin16)
@bbatsov should this one to be closed because we merged my PR?
I think the automatic closing didn't work because all uppercase FIX in the commit message. 😅
Most helpful comment
I think the automatic closing didn't work because all uppercase
FIXin the commit message. 😅