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

Another Style/FormatStringToken false positive #5630

Closed
mikegee opened this issue Mar 5, 2018 · 7 comments · Fixed by #5634
Closed

Another Style/FormatStringToken false positive #5630

mikegee opened this issue Mar 5, 2018 · 7 comments · Fixed by #5634

Comments

@mikegee
Copy link
Contributor

mikegee commented Mar 5, 2018

Github didn't let @lionel218 open this issue for some reason. See the original report at #5245 (comment)


Expected behavior

no offense from Style/FormatStringToken

Actual behavior

% bundle exec bin/rubocop test/test.rb --only Style/FormatStringToken
Inspecting 1 file
C

Offenses:

test/test.rb:2:75: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
  format('%<day>s %<start>s-%<end>s', day: open_house.starts_at.strftime('%a'),
                                                                          ^^
test/test.rb:4:76: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
                                      end: open_house.ends_at.strftime('%l %p').strip)
                                                                           ^^

1 file inspected, 2 offenses detected

Steps to reproduce the problem

Use this file:

def event_date(open_house)
  format('%<day>s %<start>s-%<end>s', day: open_house.starts_at.strftime('%a'),
                                      start: open_house.starts_at.strftime('%l'),
                                      end: open_house.ends_at.strftime('%l %p').strip)
end

RuboCop version

$ rubocop -V
0.53.0 (using Parser 2.5.0.2, running on ruby 2.5.0 x86_64-darwin16)
koic added a commit to koic/rubocop that referenced this issue Mar 9, 2018
Fixes rubocop#5630.

This PR fixes a false positive for `Style/FormatStringToken` when using
placeholder arguments in `format` method.
bbatsov pushed a commit that referenced this issue Mar 9, 2018
Fixes #5630.

This PR fixes a false positive for `Style/FormatStringToken` when using
placeholder arguments in `format` method.
@lionel218
Copy link

lionel218 commented Mar 12, 2018

Hi @bbatsov, @koic , Did you solve this issue?

This was referenced Mar 21, 2018
This was referenced Mar 21, 2018
@katherine-boost
Copy link

Just upgraded to 0.75 and I'm seeing this error again. Anyone else?

@buehmann
Copy link
Contributor

buehmann commented Oct 1, 2019

@katherine-boost I was working on this cop for the 0.75 release. What error exactly are you seeing?

@katherine-boost
Copy link

katherine-boost commented Oct 1, 2019

Thanks for your reply @buehmann

The error:

Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).

The line of code provoking the error:

formatted_cents = format('%02d', cents % 100)

@buehmann
Copy link
Contributor

buehmann commented Oct 2, 2019

@katherine-boost That is not an error. The cop behaves as designed. With style annotated, which is the default, it suggests to use named format sequences and not positional ones. In your example:

formatted_cents = format('%02<remainder>d', remainder: cents % 100)

Also see https://docs.rubocop.org/en/stable/cops_style/#styleformatstringtoken

@ndbroadbent
Copy link

Hello, just wanted to post another issue in here. When I set this in my rubocop.yml to enforce the template style:

Style/FormatStringToken:
  EnforcedStyle: template

I'm getting this error:

C: Style/FormatStringToken: Prefer template tokens (like %{foo}) over annotated tokens (like %<foo>s).
  format('%<red>02x%<green>02x%<blue>02x', color_values)

I'm not sure if I am able to switch these to the template style, because I believe that is only for strings, while I need to use the %02x to get hex values.

@buehmann
Copy link
Contributor

@ndbroadbent Good point. Would you mind creating a new issue for this? Template style has indeed no way I know to influence the type conversion. By the way, it seems to accept non-strings as an argument, but does not simply do to_s (e.g. for arrays).

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

Successfully merging a pull request may close this issue.

5 participants