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

[Fix #10760] Fix a false positive for Lint/NonAtomicFileOperation #10761

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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