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

Lint/NonAtomicFileOperation breaks shortcircuits #10786

Closed
lugray opened this issue Jul 4, 2022 · 1 comment · Fixed by #10787
Closed

Lint/NonAtomicFileOperation breaks shortcircuits #10786

lugray opened this issue Jul 4, 2022 · 1 comment · Fixed by #10787
Labels

Comments

@lugray
Copy link

lugray commented Jul 4, 2022

Expected behavior

Allow unlink/rm_rf with an existence check as part of a more complex conditional.

Actual behavior

Prevents useful short circuit behaviour.

$ bundle exec rubocop --debug lib/dev/helpers/unix_socket.rb
For /Users/lisaugray/src/github.com/Shopify/dev: configuration from /Users/lisaugray/src/github.com/Shopify/dev/.rubocop.yml
configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-sorbet-0.6.8/config/default.yml
configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-sorbet-0.6.8/config/default.yml
Default configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-1.31.1/config/default.yml
Inheriting configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-shopify-2.7.1/rubocop-cli.yml
Inheriting configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-shopify-2.7.1/rubocop.yml
Inheriting configuration from /Users/lisaugray/src/github.com/Shopify/dev/.rubocop_performance.yml
configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-performance-1.14.2/config/default.yml
configuration from /Users/lisaugray/.gem/ruby/3.1.2/gems/rubocop-performance-1.14.2/config/default.yml
Inheriting configuration from /Users/lisaugray/src/github.com/Shopify/dev/.rubocop.sorbet.yml
Use parallel by default.
Running parallel inspection
Loading cache from /Users/lisaugray/.cache/rubocop_cache/f2fbedb2a77ffd5d8096f1e7d127397d5e9a8518/6d7a3b621ca1730e04accd938619e4bdab66cfb1/38a938a95baf500276c8728c9f8ffc7539831fb5
Inspecting 1 file
Scanning /Users/lisaugray/src/github.com/Shopify/dev/lib/dev/helpers/unix_socket.rb
Loading cache from /Users/lisaugray/.cache/rubocop_cache/f2fbedb2a77ffd5d8096f1e7d127397d5e9a8518/6d7a3b621ca1730e04accd938619e4bdab66cfb1/38a938a95baf500276c8728c9f8ffc7539831fb5
W

Offenses:

lib/dev/helpers/unix_socket.rb:24:33: W: [Correctable] Lint/NonAtomicFileOperation: Remove unnecessary existence checks File.exist?.
          FileUtils.rm_rf(path) if File.exist?(path) && File.stat(path).socket?
                                ^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable
Finished in 0.24144500028342009 seconds

The existence check is not unnecessary; it short circuits File.stat which will raise for a missing file.

Steps to reproduce the problem

FileUtils.rm_rf(path) if File.exist?(path) && File.stat(path).socket?

RuboCop version

$ bundle exec rubocop -V
1.31.1 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 3.1.2 arm64-darwin21)
  - rubocop-performance 1.14.2
  - rubocop-sorbet 0.6.8
@lugray
Copy link
Author

lugray commented Jul 4, 2022

Furthermore, autocorrect takes it to FileUtils.rm_rf(path) && File.stat(path).socket?, which is extra broken.

@koic koic added the bug label Jul 4, 2022
koic added a commit to koic/rubocop that referenced this issue Jul 5, 2022
…tion`

Fixes rubocop#10786.

This PR fixes a false positive for `Lint/NonAtomicFileOperation`
when using complex conditional.
bbatsov pushed a commit that referenced this issue Jul 5, 2022
Fixes #10786.

This PR fixes a false positive for `Lint/NonAtomicFileOperation`
when using complex conditional.
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