Skip to content

Commit

Permalink
[Fix rubocop#10760] Fix a false positive for `Lint/NonAtomicFileOpera…
Browse files Browse the repository at this point in the history
…tion`

Fixes rubocop#10760.

This PR fixes a false positive for `Lint/NonAtomicFileOperation` when
using `FileTest.exist?` with `if` condition that has `else` branch.
  • Loading branch information
koic committed Jun 28, 2022
1 parent f5b77c8 commit 1bc758a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10760](https://github.com/rubocop/rubocop/issues/10760): Fix a false positive for `Lint/NonAtomicFileOperation` when using `FileTest.exist?` with `if` condition that has `else` branch. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/lint/non_atomic_file_operation.rb
Expand Up @@ -72,6 +72,7 @@ class NonAtomicFileOperation < Base

def on_send(node)
return unless node.parent&.if_type?
return if node.parent.else_branch
return if explicit_not_force?(node)
return unless (exist_node = send_exist_node(node.parent).first)
return unless exist_node.first_argument == node.first_argument
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb
Expand Up @@ -173,4 +173,14 @@
end
RUBY
end

it 'does not register an offense when using `FileTest.exist?` with `if` condition that has `else` branch' do
expect_no_offenses(<<~RUBY)
if FileTest.exist?(path)
FileUtils.mkdir(path)
else
do_something
end
RUBY
end
end

0 comments on commit 1bc758a

Please sign in to comment.