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

Autocorrect of Style/FormatString changes semantics #9089

Closed
khiav223577 opened this issue Nov 24, 2020 · 0 comments · Fixed by #9092
Closed

Autocorrect of Style/FormatString changes semantics #9089

khiav223577 opened this issue Nov 24, 2020 · 0 comments · Fixed by #9092
Labels

Comments

@khiav223577
Copy link
Contributor

khiav223577 commented Nov 24, 2020

Expected behavior

Autocorrect should not change the behavior of the code.

Actual behavior

Autocorrect changed the behavior of the code.

Steps to reproduce the problem

Add test.rb:

a = 1
result = sprintf('%04d', a + 3)
p result

Run ruby test.rb, it will print "0004".

Add .rubocop.yml:

Style/FormatString:
  EnforcedStyle: percent
  Enabled: true

Run rubocop --auto-correct test.rb in command line.
The auto-corrected test.rb will be:

a = 1
result = '%04d' % a + 3
p result

It will raise test.rb:2:in '+': no implicit conversion of Integer into String (TypeError) when running it.
Expect the corrected test.rb to be

a = 1
result = '%04d' % (a + 3)
p result

RuboCop version

$ [bundle exec] rubocop -V
1.4.1 (using Parser 2.7.2.0, rubocop-ast 1.1.1, running on ruby 2.5.5 x86_64-linux)
@bbatsov bbatsov added the bug label Nov 24, 2020
koic added a commit to koic/rubocop that referenced this issue Nov 25, 2020
…ing`

Fixes rubocop#9089

This PR fixes an incorrect auto-correct for `Style/FormatString`
when using springf with second argument that uses an operator.
bbatsov pushed a commit that referenced this issue Nov 25, 2020
Fixes #9089

This PR fixes an incorrect auto-correct for `Style/FormatString`
when using springf with second argument that uses an operator.
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