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

LiteralInInterpolation false positive when interpolating "#{__LINE__}" #1769

Closed
rrosenblum opened this issue Apr 7, 2015 · 3 comments · Fixed by #1772
Closed

LiteralInInterpolation false positive when interpolating "#{__LINE__}" #1769

rrosenblum opened this issue Apr 7, 2015 · 3 comments · Fixed by #1772

Comments

@rrosenblum
Copy link
Contributor

I just upgraded to RuboCop 0.30.0 and ran into a false positive when running LiteralInInterpolation.

Here is a test that exercises the bug.

it 'accepts strings like __LINE__' do
  inspect_source(cop, '"this is #{__LINE__} silly"')
  expect(cop.offenses).to be_empty
end


1) RuboCop::Cop::Lint::LiteralInInterpolation accepts strings like __LINE__
     Failure/Error: expect(cop.offenses).to be_empty
       expected `[W:  1: 12: Literal interpolation detected.].empty?` to return true, got false

I will try to find a fix for the issue, but in the mean time, I wanted to bring exposure to the bug.

@rrosenblum
Copy link
Contributor Author

I am not sure of the proper way to fix this. After looking into the code, the error is caused by "#{__LINE__}" having the type :int and "#{__FILE__}" having the type :str. When I update the method special_string? to account for :int, the tests for "#{1}" begin to fail. I cannot figure out a way to distinguish between "#{__LINE__}" and "#{1}" without looking at the actual source.

@jonas054
Copy link
Collaborator

jonas054 commented Apr 8, 2015

I think you must look at the actual source to sort out this special case.

node.loc.expression.is?('__LINE__')

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 8, 2015

Yeah, that should solve it. Unfortunately its a somewhat expensive check, but I can think of no other alternatives.

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 8, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 9, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 9, 2015
bbatsov added a commit that referenced this issue Apr 9, 2015
[Fix #1769] LiteralInInterpolation registers an offense for __LINE__
crimsonknave pushed a commit to crimsonknave/rubocop that referenced this issue Apr 13, 2015
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.

3 participants