Skip to content

Commit

Permalink
Merge pull request #12796 from koic/fix_false_positives_for_style_eva…
Browse files Browse the repository at this point in the history
…l_with_location

Fix false positives for `Style/EvalWithLocation`
  • Loading branch information
koic committed Mar 18, 2024
2 parents e682185 + fd30cf6 commit 23ad8e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12796](https://github.com/rubocop/rubocop/pull/12796): Fix false positives for `Style/EvalWithLocation` when using `eval` with a line number from a method call or a variable. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/eval_with_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def check_file(node, file_node)

def check_line(node, code)
line_node = node.last_argument
return if line_node.variable? || (line_node.send_type? && !line_node.method?(:+))

line_diff = line_difference(line_node, code)
if line_diff.zero?
add_offense_for_same_line(node, line_node)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/eval_with_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,22 @@ def self.included(base)
end
RUBY
end

it 'does not register an offense when using eval with a line number from a method call' do
expect_no_offenses(<<~RUBY)
module_eval(<<~CODE, __FILE__, lineno)
do_something
CODE
RUBY
end

it 'does not register an offense when using eval with a line number from a variable' do
expect_no_offenses(<<~RUBY)
lineno = calc
module_eval(<<~CODE, __FILE__, lineno)
do_something
CODE
RUBY
end
end

0 comments on commit 23ad8e1

Please sign in to comment.