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 create broken code (else without if) #10760

Closed
ShockwaveNN opened this issue Jun 28, 2022 · 4 comments · Fixed by #10761
Closed

Lint/NonAtomicFileOperation create broken code (else without if) #10760

ShockwaveNN opened this issue Jun 28, 2022 · 4 comments · Fixed by #10761
Labels

Comments

@ShockwaveNN
Copy link
Contributor

@ShockwaveNN ShockwaveNN commented Jun 28, 2022

Steps to reproduce the problem

  1. Create file test.rb:
# frozen_string_literal: true

folder = '/tmp/test'
if Dir.exist?(folder)
  p 'a'
else
  FileUtils.mkdir(folder)
end

  1. run rubocop -A test.rb
  2. In result we got code like this:
# frozen_string_literal: true

folder = '/tmp/test'

  p 'a'
else
  FileUtils.mkdir_p(folder)


This is incorrect ruby code, else is missing it's if

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ [bundle exec] rubocop -V
1.31.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 3.1.2 x86_64-linux)
  - rubocop-rspec 2.11.1

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

Fixes rubocop#10760.

This PR fixes a false positive for `Lint/NonAtomicFileOperation` when
using `FileTest.exist?` with `if` condition that has `else` branch.
bbatsov pushed a commit that referenced this issue Jun 28, 2022
Fixes #10760.

This PR fixes a false positive for `Lint/NonAtomicFileOperation` when
using `FileTest.exist?` with `if` condition that has `else` branch.
@ShockwaveNN ShockwaveNN changed the title Lint/NonAtomicFileOperation create broken code (else without if Lint/NonAtomicFileOperation create broken code (else without if) Jun 28, 2022
@AlexWayfer
Copy link
Contributor

@AlexWayfer AlexWayfer commented Jun 30, 2022

My case:

FileUtils.rm_r project_name if Dir.exist? project_name

Now causes offense.

"Fix" without if causes the error:

Failure/Error: FileUtils.rm_r project_name

Errno::ENOENT:
  No such file or directory @ apply2files - foo_bar

@AlexWayfer
Copy link
Contributor

@AlexWayfer AlexWayfer commented Jun 30, 2022

Should I notify case with FileUtils.rm_r(@filename) if File.exist?(@filename)?

@ShockwaveNN
Copy link
Contributor Author

@ShockwaveNN ShockwaveNN commented Jul 1, 2022

@AlexWayfer Try to add require 'fileutils' to your file, it solve Errno:ENOENT error for FileUtils.rm_rf for me

@AlexWayfer
Copy link
Contributor

@AlexWayfer AlexWayfer commented Jul 13, 2022

@AlexWayfer Try to add require 'fileutils' to your file, it solve Errno:ENOENT error for FileUtils.rm_rf for me

Doesn't help.

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.

3 participants