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

Lint/FormatParameterMismatch false positive for fields with absolute argument numbers #6482

Closed
ghost opened this issue Nov 14, 2018 · 2 comments · Fixed by #6483
Closed

Lint/FormatParameterMismatch false positive for fields with absolute argument numbers #6482

ghost opened this issue Nov 14, 2018 · 2 comments · Fixed by #6483
Labels

Comments

@ghost
Copy link

ghost commented Nov 14, 2018

In the format string for Kernel#format you can specify the absolute argument number before a field using (digit)$ flag. Here are examples from the docs:

sprintf("%1$*2$s %2$d %1$s", "hello", 8)   #=> "   hello 8 hello"
sprintf("%1$*2$s %2$d", "hello", -8)       #=> "hello    -8"

RuboCop currently fails to deal with such situations.


Expected behavior

No offenses.

Actual behavior

Lint/FormatParameterMismatch offense.

Steps to reproduce the problem

Given this minimal example:

# format.rb
format('%1$s %1$s', 'foo')

Run:

$ rubocop format.rb
Inspecting 1 file
W

Offenses:

format.rb:1:1: W: Lint/FormatParameterMismatch: Number of arguments (1) to format doesn't match the number of fields (0).
format('%1$s %1$s', 'foo')
^^^^^^

1 file inspected, 1 offense detected

RuboCop version

$ rubocop -V
0.60.0 (using Parser 2.5.3.0, running on ruby 2.5.1 x86_64-openbsd)
@Drenmi Drenmi added the bug label Nov 14, 2018
koic added a commit to koic/rubocop that referenced this issue Nov 14, 2018
…atch`

Fixes rubocop#6482.

This PR fixes a false positive for `Lint/FormatParameterMismatch` when
using (digit)$ flag.

```ruby
format('%1$s %1$s', 'foo')
```

The following an explanation about (digit)$ flag written in the document.

> Specifies the absolute argument number for this field.
> Absolute and relative argument numbers cannot be mixed in a
> sprintf string.

https://ruby-doc.org/core-2.5.3/Kernel.html#method-i-sprintf

This PR adds a check on the maximum value specified by (digit)$ and
the number of arguments.
@koic
Copy link
Member

koic commented Nov 14, 2018

Thanks for the feedback. I confirmed this reproduction and I opened a PR #6483.

@ghost
Copy link
Author

ghost commented Nov 14, 2018

Whoa, that's fast! Thank you.

koic added a commit to koic/rubocop that referenced this issue Nov 15, 2018
…atch`

Fixes rubocop#6482.

This PR fixes a false positive for `Lint/FormatParameterMismatch` when
using (digit)$ flag.

```ruby
format('%1$s %1$s', 'foo')
```

The following an explanation about (digit)$ flag written in the document.

> Specifies the absolute argument number for this field.
> Absolute and relative argument numbers cannot be mixed in a
> sprintf string.

https://ruby-doc.org/core-2.5.3/Kernel.html#method-i-sprintf

This PR adds a check on the maximum value specified by (digit)$ and
the number of arguments.
koic added a commit that referenced this issue Nov 15, 2018
…rameter_mismatch

[Fix #6482] Fix a false positive for `Lint/FormatParameterMismatch`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants