Rubocop: False positive on sprintf with "asterisk" ('*') width and precision

Created on 6 Jul 2017  Â·  2Comments  Â·  Source: rubocop-hq/rubocop

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.

Example

class RuboCop::Sprintf
  def test
    sprintf("%*.*s\n", 10, 10, 'this is a test')
  end
end

Expected behavior

No error should be generated

Actual behavior

It will complain that argument count is wrong (3 instead of 2)

Steps to reproduce the problem

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 version

⇨ rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-darwin16)
bug

Most helpful comment

I think the automatic closing didn't work because all uppercase FIX in the commit message. 😅

All 2 comments

@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. 😅

Was this page helpful?
0 / 5 - 0 ratings