Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive on sprintf with "asterisk" ('*') width and precision #4578

Closed
cybercode opened this issue Jul 6, 2017 · 2 comments
Closed
Labels

Comments

@cybercode
Copy link

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)
@cybercode cybercode changed the title False positive on sprintf with width or precision = * False positive on sprintf with width and precision = * Jul 6, 2017
@cybercode cybercode changed the title False positive on sprintf with width and precision = * False positive on sprintf with "asterisk" ('*') width and precision Jul 6, 2017
@bbatsov bbatsov added the bug label Jul 7, 2017
@smakagon
Copy link
Contributor

@bbatsov should this one to be closed because we merged my PR?

@Drenmi
Copy link
Collaborator

Drenmi commented Jul 13, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants